Lecture 4
Lecture 4
class Rectangle{
Polygon
Rectangle
private:
int numVertices;
float *xCoord, *yCoord;
public:
void set(float *x, float *y, int nV);
float area();
Triangle
};
class Polygon{
private:
int numVertices;
float *xCoord, *yCoord;
public:
void set(float *x, float *y, int nV);
};
class Triangle{
private:
int numVertices;
float *xCoord, *yCoord;
public:
void set(float *x, float *y, int nV);
float area();
};
1
Inheritance Concept
Polygon
Rectangle
Triangle
class Polygon{
protected:
int numVertices;
float *xCoord, float *yCoord;
public:
void set(float *x, float *y, int nV);
};
class Rectangle{
protected:
int numVertices;
float *xCoord, float *yCoord;
public:
void set(float *x, float *y, int nV);
float area();
2
};
Inheritance Concept
Polygon
Rectangle
Triangle
class Polygon{
protected:
int numVertices;
float *xCoord, float *yCoord;
public:
void set(float *x, float *y, int nV);
};
class Triangle{
protected:
int numVertices;
float *xCoord, float *yCoord;
public:
void set(float *x, float *y, int nV);
float area();
3
};
Inheritance Concept
x
y
Point
Circle
3D-Point
x
y
r
x
y
z
class Point{
protected:
int x, y;
public:
void set (int a, int b);
};
Inheritance Concept
Augmenting the original class
Polygon
Rectangle
Point
Circle
Triangle
3D-Point
real
RealNumber
real
imag
ImaginaryNumber
imag
Why Inheritance ?
Inheritance is a mechanism for
building class types from existing class types
defining new class types to be a
specialization
augmentation
of existing types
Class Derivation
Point
3D-Point
Sphere
class 3D-Point : public Point{
private:
double z;
};
class Point{
protected:
int x, y;
public:
void set (int a, int b);
};
class Sphere : public 3D-Point{
private:
double r;
};
Point is the base class of 3D-Point, while 3D-Point is the base class of Sphere
What to inherit?
In principle, every member of a base class
is inherited by a derived class
just with different access permission
members goes to
derive from
b a s e c la s s / s u p e r c la s s /
p a r e n t c la s s
d e r iv e d c la s s / s u b c la s s /
c h ild c la s s
};
10
private
protected
public
private
protected
private
protected
protected
public
private
protected
public
11
Class Derivation
class mother{
protected: int mProc;
public: int mPubl;
private: int mPriv;
};
private/protected/public
class daughter : --------- mother{
private: double dPriv;
public: void dFoo
mFoo((););
};
void daughter :: dFoo ( ){
mPriv = 10; //error
mProc = 20;
};
12
What to inherit?
In principle, every member of a base class is
inherited by a derived class
just with different access permission
class B : public A
{
public:
B (int a)
{cout<<B<<endl;}
};
output:
B test(1);
A:default
B
14
class A {
public:
A( )
{cout<< A:default<<endl;}
A (int a)
{cout<<A:parameter<<endl;}
};
class C : public A {
public:
C (int a) : A(a)
{cout<<C<<endl;}
};
output:
C test(1);
A:parameter
C
15
Point
x
y
r
x
y
Circle
class Point{
protected:
int x, y;
public:
void set(int a, int b);
};
class Circle{
protected:
int x, y;
private:
double r;
public:
void set(int a, int b);
void set_r(double c);
};
16
Even more
A derived class can override methods defined in its parent
class. With overriding,
the method in the subclass has the identical signature to the
method in the base class.
a subclass implements its own version of a base class method.
class A {
protected:
int x, y;
public:
void print ()
{cout<<From A<<endl;}
};
class B : public A {
public:
void print ()
{cout<<From B<<endl;}
};
17
Access a Method
class Point{
protected:
int x, y;
public:
void set(int a, int b)
{x=a; y=b;}
void foo ();
void print();
};
Point A;
A.set(30,50); // from base class Point
A.print(); // from base class Point
Circle C;
C.set(10,10,100); // from class Circle
C.foo (); // from base class Point
C.print(); // from class Circle
18
ExtTime
19
( time.h)
class Time{
public :
void Set ( int h, int m, int s ) ;
void Increment ( ) ;
void Write ( ) const ;
Time ( int initH, int initM, int initS ) ; // constructor
Time ( ) ;
// default constructor
protected :
int
int
int
hrs ;
mins ;
secs ;
};
20
Protected data:
hrs
mins
secs
Time
21
( exttime.h)
#include time.h
enum ZoneType {EST, CST, MST, PST, EDT, CDT, MDT, PDT } ;
class ExtTime : public Time
{
public :
void
Set ( int h, int m, int s, ZoneType timeZone ) ;
void
Write ( ) const; //overridden
ExtTime (int initH, int initM, int initS, ZoneType initZone ) ;
ExtTime (); // default constructor
private :
ZoneType zone ;
};
22
Set
Increment
Write
Set
Increment
Write
Protected data:
hrs
mins
ExtTime
ExtTime
Time
secs
Time
Private
data:
zone
23
Implementation of ExtTime
Default Constructor
ExtTime :: ExtTime ( )
{
zone = EST ;
}
ExtTime et1;
et1
hrs = 0
mins = 0
secs = 0
zone = EST
24
Implementation of ExtTime
Another Constructor
ExtTime :: ExtTime (int initH, int initM, int initS, ZoneType initZone)
: Time (initH, initM, initS)
// constructor initializer
{
zone = initZone ;
}
ExtTime *et2 =
new ExtTime(8,30,0,EST);
et2
6000
5000
???
5000
hrs = 8
mins = 30
secs = 0
zone = EST
25
Implementation of ExtTime
void ExtTime :: Set (int h, int m, int s, ZoneType timeZone)
{
Time :: Set (hours, minutes, seconds); // same name function call
zone = timeZone ;
}
void ExtTime :: Write ( ) const // function overriding
{
string zoneString[8] =
{EST, CST, MST, PST, EDT, CDT, MDT, PDT} ;
Time :: Write ( ) ;
cout << <<zoneString[zone]<<endl;
}
26
int main()
{
ExtTime
ExtTime
thatTime.Write( ) ;
thisTime.Increment ( ) ;
thisTime.Increment ( ) ;
thisTime.Write ( ) ;
27