Professional Documents
Culture Documents
• Historical notes
• C and C++
• Objects and Classes
• Pointers to objects
• Pointers and references
• Inheritance
• Polymorphism - virtual functions
• Operator Overloading
• Templates
1
Historical Notes
• Procedural programming
• Modular programming
• Data abstraction
• Object-oriented programming
2
C and C++
for { }
if () { } else if () { } else { }
do { } while ()
while () { }
3
Function definition (ANSI standard - standard C definition
NOT supported) is the same :
#include <iostream.h>
void main()
{
cout << "Printing the 7 times table \n";
for (int i=1; i<=12; i++)
cout << i << " * 7 = " << func(i,7) << "\n";
}
Note the use of the stream i/o operator and the declaration
of i.
4
5
Objects and Classes
Example
class Point{
private:
int x,y;
public:
Point(int,int);
void shift(int,int);
int getx(){return x;}
int gety(){return y;}
};
6
Notes
7
A more succint definition is possible :
Point aPoint(5,3);
cout << "x,y pos. <<aPoint.getx()<<aPoint.gety();
aPoint.shift(6,5);
cout << "new x,y pos. "<<aPoint.getx()<< aPoint.gety();
8
Classes - Implementation, Interface
Point
private :
Implementation
int x,y;
public :
Point() {...}
shift() {...}
Interface
getx() {return x;}
gety() {return y;}
9
The implementation might change but the interface
must stay the same :
Point
private : Implementation
float r,theta;
public :
Point() {...}
shift() {...}
getx() {return r*cos(theta);} Interface
gety() {return r*sin(theta);}
10
Friends
class Point
{
public :
friend float distance(Point, Point);
};
11
Pointers to Objects
main()
{
Point aPoint1(5,2);
f(aPoint1);
}
main()
{
12
Point aPoint1(5,2);
f(aPoint1); // aPoint1 now updated
}
An alternative is to use the de-referencing operator * as
in C :
main()
{
Point aPoint1(5,2);
f(&aPoint1); // aPoint1 now updated
}
13
Pointers and References
Pointers References
x px x,rx
2 2
14
Dynamic allocation of memory
main()
{
Point* p;
p = new Point(2,5);
p->shift(3,2);
delete p;
}
15
this
Point aPoint
public:
shift()
{
.
this
.
}
Point p(2,3);
16
Point p1=p.shift(2,2);
17
Inheritance
Shape
Point Circle
18
Circle
Shape
private :
int x,y;
protected :
Shape() {...}
public :
plot() {...}
getx() {...}
gety() {...}
shift() {...}
private :
int radius ;
public :
Circle() {...}
getRadius() {...}
plot() {...}
19
class Shape{
private:
int x,y;
public:
void shift(int,int);
void plot(){ cout << "Not implemented \n";}
int getx{return x;}
int gety{return y;}
};
20
class Point : public Shape {
public:
Point(int,int);
void plot();
};
private:
int radius;
public:
Circle(int,int,int);
int getRadius{ return radius;}
void plot();
};
21
The constructors for Shape, Point and Circle are as
follows :
22
Polymorphism - Virtual functions
23
Example
Circle aCircle;
Point aPoint;
Shape *s1,*s2;
s1 = &aPoint; // OK - a point is a shape
s2 = &aCircle;// OK - a circle is a shape
s1->plot(); // Which plot()?
s2->plot(); // Which plot()?
24
We can solve the problem by making plot() a virtual
function :
class Shape{
private:
int x,y;
public:
void shift(int,int);
25
We can now overload the Shape pointers to point to
either a Circle or Point object and the correct plot()
function is called :
Circle aCircle;
Point aPoint;
Shape *s1,*s2;
s1 = &aPoint; // OK - a point is a shape
s2 = &aCircle;// OK - a circle is a shape
s1->plot(); // Point::plot()
s2->plot(); // Circle::plot()
26
Runtime overloading allows a common user interface to
a large hierarchy of classes to be established. The
required interface function is invoked at runtime.
Example
Implementation of a GUI based graphics utility.
plotCallback()
ShapeList
27
The implementation of plotCallback() might be as
follows :
plotCallback(ShapeList* sl)
{
.
.
Shape* s = sl->getSelectedShape();
.
s->plot(); // runtime overloaded to
// required plot function
.
.
}
28
Operator Overloading
class complex
{
private :
double re, im;
public :
complex(double r,double i) { re = r; im = i;}
29
The implementations are as follows :
30
The overload function can be made a member function
as follows :
class complex
{
private :
double re, im;
public :
complex(double r,double i) { re = r; im = i;}
31
The implementations are as follows :
32
The overload functions can be used as follows :
void main()
{
complex z1(3.0,2.0),z2(3.0,-5.0),z3,z4;
33
Templates
34
We can define a simple template array data structure,
Vector, which allows arrays of any arbitrary type to be
constructed :
public:
Vector(int) ; // Constructor
int r_size( ) { return size; }
T& operator[] (int ); // overloaded []
};
35
int main()
{
Vector<int> v_int(100); // Vector of 100 ints
Vector<Circle> v_circle(100); // Vector of 100 circles
Circle c(5,4,10);
v_int[10]=5;
cout << v_int[10];
v_circle[5]=c;
v_circle[5].plot();
}
36
Example
int size=v.r_size();
for (int i=0; i<size-1; i++)
for (int j=size; i<j; j--)
if (v[j]<v[j-1]) // swap v[j] & v[i]
{
T t = v[j];
v[j]=v[j-1];
v[j-1] = t;
}
}
37
Other points to note about templates
38