Assignment operator
Classname& operator=(const Classname& right)
cfw_
.
.
return *this;
X = y = z = a = b;
Default parameters
In our example, if we wanted to overload the constructor to allow following calls:
Coordinate p1, p2(2.3), p3(8.9, 7.1);
Insert for Chain
Locate the kth node
Two cases:
Either we are inserting first node or we are inserting some node in the
middle
See pictures and code.
Doubly linked list
DoubleNode
Data of type T
Prev, next pointers to DoubleList<T>
Look at cnode.h and
Exceptions (continued)
Exceptions are implemented using three keywords throw, try, catch
throw is used to throw a variable
throw dividebyzero(); throws a variables created by the constructor of the class
dividebyzero.
try is used to identify the block
Average time requirements for linear searching will require us to consider the
prob(target is in the array), and prob(target is not in the array)
This will give us an equation such as:
c1*n + c2
Big Oh notations
Search algorithm
Best case: 1 = O(1)
Wors
Both the class definition and the member function definitions have to be preceded by
the line
template<class T>
The member functions cannot be written as
LinearList LinearList:Insert(.)
Because LinearList is a template not a class
The correct form is
Delete(k,x)
If(k < 1 | k > length) throw OutOfBounds();
Three cases:
If(k = 1)
cfw_
p = first;
first = firsts next;
if(first != NULL) firsts prev = NULL;
else last = NULL;
Else If(k=length)
cfw_
p = last
last = lasts prev;
lasts next = NULL;
Els
Let us analyze InsertionSort
Outer loop is excuted n-1 times
n 1
n * (n 1)
= 0.5n 2 0.5n = O(n 2 )
2
i =1
Inner loop is executed I times in the worst case
A typical question could be as follows:
For the following algorithms provide the expression for
Instead of coming up with function names such as Add, it is better if our
types have operators such as + or +=
Currency c1, c2, c3;
c3 = c1 + c2;
This can be achieved by overloading the operator +;
class Currency
cfw_
private:
long amount;
public:
Curre
Loops that are incremented or decremented by one, can be easily
translated to a summation
for(int j = 1; j <= n; j+) cout < a[j];
n
1 = n
j =1
for(int j = 0; j < n; j+) cout < a[j];
n 1
1 = n
j =0
for(int j = 0; j < n; j+)
for(int k = 0; k < n; k+)
cou
Office MN 117, Phone 420-5798
E-mail: [email protected]
Office Hours: 2:30 to 4:00 pm (Mon-Thu)
http:/cs.stmarys.ca/faculty/home_pages/pawan/
or
http:/cs.stmarys.ca and follow the link for faculty followed by P.Lingras
Introduction to C+
A simple
Time and Space Requirements
There are a lot of problems which cannot be solved using a computer
We restrict ourselves to problems that can be solved
We can write a program that will produce desired results for all
possible inputs, provided
it has as mu
C+ provides built-in types such as char, int, double, etc.
These allow us to define simple variables to store single values
Arrays can be used to store several values of a given type
int ai[90];
Arrays are homogeneous collection of values. All values are
Time complexity
n
time
i
frequencyi
i =1
Problem with using the above formula is that the calculations are tedious
They dont apply to multiprogramming environment
The time requirements only apply to a particular CPU
Generally, we restrict ourselves to t