Professional Documents
Culture Documents
class Rectangle{
Polygon private:
int numVertices;
float *xCoord, *yCoord;
public:
Rectangle Triangle void set(float *x, float *y, int nV);
float area();
};
class Rectangle{
protected:
class Rectangle : public Polygon{ int numVertices;
public: float *xCoord, float *yCoord;
public:
float area();
void set(float *x, float *y, int nV);
}; float area();
2
};
Inheritance Concept
class Polygon{
protected:
Polygon
int numVertices;
float *xCoord, float *yCoord;
public:
void set(float *x, float *y, int nV);
Rectangle };
Triangle
class Triangle{
protected:
class Triangle : public Polygon{ int numVertices;
public: float *xCoord, float *yCoord;
public:
float area();
void set(float *x, float *y, int nV);
}; float area();
3
};
Inheritance Concept
x
class Point{
Point y protected:
int x, y;
public:
Circle 3D-Point void set (int a, int b);
x x
};
y y
r z
real
RealNumbe ImaginaryNumber imag 5
r
Why Inheritance ?
6
Define a Class Hierarchy
• Syntax:
class DerivedClassName : access-level BaseClassName
where
– access-level specifies the type of derivation
• private by default, or
• public
• Any class can serve as a base class
– Thus a derived class can also be a base class
7
Class Derivation
Point class Point{
protected:
int x, y;
3D-Point public:
void set (int a, int b);
};
Sphere
class 3D-Point : public Point{ class Sphere : public 3D-Point{
private: private:
double z; double r;
…… ……
}; };
Point is the base class of 3D-Point, while 3D-Point is the base class of Sphere
8
What to inherit?
9
Access Control Over the Members
• Two levels of access control
b a s e c la s s / s u p e r c la s s / over class members
p a r e n t c la s s – class definition
– inheritance type
derive from
members goes to
class Point{
protected: int x, y;
public: void set(int a, int b);
d e r iv e d c la s s / s u b c la s s / };
c h ild c la s s class Circle : public Point{
……
}; 10
Access Rights of Derived Classes
Type of Inheritance
private protected public
for Members
Access Control
private - - -
protected private protected protected
public private protected public
11
Class Derivation
class mother{ class grandDaughter : public daughter {
protected: int mProc; private: double gPriv;
public: int mPubl; public: void gFoo ( );
private: int mPriv; };
};
private/protected/public
int main() {
class daughter : --------- mother{
/*….*/
private: double dPriv;
}
public: void dFoo
mFoo((););
};
output: A:default
B test(1); B 14
Constructor Rules for Derived Classes
You can also specify an constructor of the
base class other than the default constructor
DerivedClassCon ( derivedClass args ) : BaseClassCon ( baseClass
args )
{ DerivedClass constructor body }
class A {
class B : public A {
protected:
public:
int x, y;
void print ()
public:
{cout<<“From B”<<endl;}
void print ()
};
{cout<<“From A”<<endl;}
};
17
Access a Method
class Point{
class Circle : public Point{
protected:
private: double r;
int x, y;
public:
public:
void set (int a, int b, double c) {
void set(int a, int b)
Point :: set(a, b); //same name function call
{x=a; y=b;}
r = c;
void foo ();
}
void print();
void print(); };
};
Circle C;
Point A; C.set(10,10,100); // from class Circle
A.set(30,50); // from base class Point C.foo (); // from base class Point
A.print(); // from base class Point C.print(); // from class Circle
18
Putting Them Together
• Time is the base class
• ExtTime is the derived class with
Time public inheritance
• The derived class can
– inherit all members from the base
class, except the constructor
– access all public and protected
ExtTime members of the base class
– define its private data member
– provide its own constructor
– define its public member functions
– override functions inherited from
the base class
19
class Time Specification
// SPECIFICATION FILE ( 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 hrs ;
int mins ;
int secs ;
};
20
Class Interface Diagram
Time class
Set
Protected data:
Increment
hrs
Write
mins
Time secs
Time
21
Derived Class ExtTime
// SPECIFICATION FILE ( exttime.h)
#include “time.h”
enum ZoneType {EST, CST, MST, PST, EDT, CDT, MDT, PDT } ;
Set Set
Default Constructor
Time :: Write ( ) ;
cout <<‘ ‘<<zoneString[zone]<<endl;
} 26
Working with ExtTime
#include “exttime.h”
……
int main()
{
ExtTime thisTime ( 8, 35, 0, PST ) ;
ExtTime thatTime ; // default constructor called
thatTime.Write( ) ; // outputs 00:00:00 EST
thatTime.Set (16, 49, 23, CDT) ;
thatTime.Write( ) ; // outputs 16:49:23 CDT
thisTime.Increment ( ) ;
thisTime.Increment ( ) ;
thisTime.Write ( ) ; // outputs 08:35:02 PST
}
27
Take Home Message
• Inheritance is a mechanism for defining new
class types to be a specialization or an
augmentation of existing types.
28