Académique Documents
Professionnel Documents
Culture Documents
C++
Fakhar Lodhi
Software Design
Quality
• What is good design?
– Is it efficient code?
– compact implementation?
– Most maintainable?
• For most large, long life time software systems,
maintenance cost normally exceeds development
cost by factors ranging from 2 to 3.
• Boehm (1975) quotes a pathological case where
the development cost of an avionics system was
$30 per line of code but the maintenance cost
was $4000 per instruction
Maintainable Design
• Cost of system changes is minimal
• readily adaptable to modify
existing functionality and enhance
functionality
• design is understandable
• changes should be local in effect
• Design should be modular
Abstraction
• Abstraction is a technique in which we construct a
model of an entity based upon its essential
characteristics while ignoring the inessential details.
• The principle of abstraction also helps in handling the
inherent complexity of a system by allowing looking
at its important external characteristic, and hiding its
inner complexity at the same time.
• Hiding the internal details is called encapsulation.
• Engineers of all fields, including computer science,
have been practicing abstraction for mastering
complexity.
• Code and Data abstraction
void selectionSort(int a[], int size)
{
int i, j, min, temp;
for(i = 0; i < size –1; i++)
{
// find minimum from i to size - 1
min = i;
for (j = i; j < size; j++)
{
if (a[j] < a[min])
min = j;
}
int i, *pi;
float f, *pf;
i = 1024;
pi = &i;
pf = (float *) pi;
f = *pf;
cout << " i = " << i << " f = " << f << "\n \n";
f = i;
cout << " i = " << i << " f = " << f << "\n \n";
return 0;
}
WHY?
Abstraction VS.
Implementation
Two dimensional array
1 5 3 6
3 2 38 64
22 76 82 99
0 106 345 54
1 5 3 6 3 2 38 64 22 76 82 99 0 106 345 54
System’s view
(implementation)
ADTs and C++ Classes
• A class is used to define (and implement) an
ADT in C++.
• A class consists of data and functions that
operate on that data.
• A class in C++ has two parts – public and
private (let’s ignore the protected members
for now).
• The data and functions defined in the class
are called the members of the class.
ADTs and C++ Classes
• Users of the class can only access and
manipulate the class state through the
public members of the class.
• Private members can only be used by
other members of the class (let’s also
ignore the friends for now).
• Data encapsulation is achieved by
declaring all data members of a class to
be private.
• The interface to the class is provided
through the use of public member
functions.
Data Structures
• The primary objective of programming is to
efficiently process the input to generate the
desired output.
• We can achieve this objective in an efficient
and neat style if the input data is organized in
a way to help us meet our goal.
• Data Structures is nothing but ways and
means of organizing data so that it can be
processed easily and efficiently.
• Data structures dictate the manner in which
the data can be processed. In other words, the
choice of an algorithm depends upon the
underlying data organization.
Problem: Determine if a key is present in a collection of 10
integers
Algorithm
found = false;
if (key = = A0) found = true;
else if (key = = A1) found = true;
else if (key = = A2) found = true;
else if (key = = A3) found = true;
else if (key = = A4) found = true;
else if (key = = A5) found = true;
else if (key = = A6) found = true;
else if (key = = A7) found = true;
else if (key = = A8) found = true;
else if (key = = A9) found = true;
Problem: Determine if a key is present in a collection of 10
integers
Organization 2: Data are stored in an array A of 10
elements
Algorithm
found = false;
for (int i = 0; i < 10; i++)
if (A[i] = = key)
{
found = true;
break;
}
• Average number of comparisons in both cases is the
same so both algorithms are equally efficient (or in
efficient)
• Organization 2 is better because it yields an algorithms
which is more maintainable. For example, if the
collection contains 100 elements. In general, number of
elements could be N.
Problem: Determine if a key is present in a collection of 10
integers
Organization 3: Data are stored in an array A in
ascending order
Algorithm
found = false; low = 0; high = N – 1;
while (( ! found) && ( low <= high))
{
mid = (low + high)/2;
if (A[mid] = = key) found = true;
else if (A[mid] > key) high = mid – 1;
else low = mid + 1;
}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2 3 5 7 10 12 15 22 28 29 32 47 48 50 55 73
key = 29
Back
Performance Comparison
• NADRA database: ~80,000,000 records
• Computer which can perform 10,000
comparisons per second
– Linear search: ~2.22 hours
– Binary search: ~0.005 seconds
• Why?
Performance Analysis
1. Does the program efficiently use
primary and secondary storage?
2. Is the program’s running time
acceptable for the task?
3. Space Complexity:
• The space complexity of a program is the
measure of the amount of memory that it
needs to run to completion.
4. Time Complexity:
• The time complexity of a program is the
measure of the amount of computer time it
needs to run to completion.
Performance Estimation
2500
2000
1500
growth
1000
500
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
n
Big Oh (O)
// O(n)
void swap(int &a, int &b)
{
int temp=a;
a = b;
b = temp;
}
// O(1)
Big Oh (O)
void selection_sort(int list[], int size)
{
int i,j;
for (i=0; i<size-1; i++) {
// O(Size) or O(n)
j= search_min(list, i+1, size-1);
//O(n).O(n)=O(n2)
swap (list[i], list[j]);
//O(1).O(n)=O(n)
}
// O(n)+O(n2)+O(n)+O(n)=O(n2)
}
Big Oh (O)
• O(1) - constant
• O(log n) - logarithmic
• O(n) - linear
• O(n log n) - log linear
• O(n2) - quadratic
• O(n3) - cubic
• O(2n) - exponential