11267 Inheritance (1)
-
Upload
rinky-sinha -
Category
Documents
-
view
227 -
download
0
Transcript of 11267 Inheritance (1)
-
8/3/2019 11267 Inheritance (1)
1/31
Inheritance
-
8/3/2019 11267 Inheritance (1)
2/31
R eusability --building new components by utilizing
existing components- is yet another important aspect of
OO paradigm. It is always good/ productive if we are able to reuse
something that is already exists rather than creating the
same all over again. This is achieve by creating new classes, reusing the
properties of existing classes.
Inheritance: Introduction
-
8/3/2019 11267 Inheritance (1)
3/31
Inheritance: Introduction
This mechanism of deriving a new classfrom existing/old class is
called inheritance.The old class is known as
base class, superclass or parent class;and the new class is
known as sub class,derived class, or childclass.
Parent
Inheritedcapability
Child
-
8/3/2019 11267 Inheritance (1)
4/31
class Rectangle{ private:
int numVertices;float *xCoord, *yCoord;
public:
void set(float *x, float *y, int nV);float area();
};
Inheritance Concept
Rectangle Triangle
Polygon
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();
};
-
8/3/2019 11267 Inheritance (1)
5/31
-
8/3/2019 11267 Inheritance (1)
6/31
RectangleTriangle
Polygonclass Polygon{
protected:int numVertices;float *xCoord, float *yCoord;
public:void set(float *x, float *y, int nV);
};
class Triangle : public Polygon{ public:
float area();};
class Triangle{ protected:
int numVertices;float *xCoord, float *yCoord;
public:void set(float *x, float *y, int nV);float area();
};
Inheritance Concept
-
8/3/2019 11267 Inheritance (1)
7/31
Inheritance Concept
Point
Circle 3D-Point
class Point{ protected:
int x, y; public:
void set (int a, int b);};
class Circle : public Point{ private:
double r;};
class 3D-Point: public Point{ private:
int z;};
x
y
x
yr
x
yz
-
8/3/2019 11267 Inheritance (1)
8/31
A ugmenting the original class
S pecializing the original class
Inheritance Concept
RealNumber
ComplexNumber
ImaginaryNumber
R ectangleTriangle
Polygon Point
Circle
realimag
realimag
3D-Point
-
8/3/2019 11267 Inheritance (1)
9/31
W hy Inheritance ?
Inheritance is a mechanism for
building class types from existing class types
defining new class types to be a specialization
augmentationof existing types
-
8/3/2019 11267 Inheritance (1)
10/31
Define a Class Hierarchy
S yntax:
class DerivedClassName : access-level BaseClassName
where access-level specifies the type of derivation
private by default, or public
A ny class can serve as a base class Thus a derived class can also be a base class
-
8/3/2019 11267 Inheritance (1)
11/31
Class DerivationPoint
3D-Point
class Point{ protected:
int x, y; public:
void set (int a, int b);};
class 3D-Point : public Point{
private:double z;
};
class S phere : public 3D-Point{
private:double r;
};
S phere
Point is the base class of 3D-Point , while 3D-Point is the base class of S phere
-
8/3/2019 11267 Inheritance (1)
12/31
What to inherit?
In principle , every member of a base class isinherited by a derived class just with different access permission
-
8/3/2019 11267 Inheritance (1)
13/31
A ccess Control Over the MembersTwo levels of access controlover class members class definition
inheritance type
base class/ superclass/parent class
derived class/ subclass/child class
d e r i v e
f r o m
m e m b e r s g o e s t o
class Point{ protected: int x, y; public: void set(int a, int b);
};
class Circle : public Point{
};
-
8/3/2019 11267 Inheritance (1)
14/31
The type of inheritance defines the access level for the
members of derived class that are inherited from the baseclass
A ccess Rights of Derived Classes
private protected public
private - - -
protected private protected protected public private protected public
Type of Inheritance
A c c e s s C on t r ol
f or M
e m b e r s
-
8/3/2019 11267 Inheritance (1)
15/31
class daughter : --------- mother{ private: double dPriv; public: void mFoo ( );
};
Class Derivationclass mother{
protected: int mProc; public: int mPubl; private: int mPriv;
};
class daughter : --------- mother{ private: double dPriv; public: void dFoo ( );
};
void daughter :: dFoo ( ){mPriv = 10; //error mProc = 20;
};
private/protected/publicint main() {
/*.*/}
class grandDaughter : public daughter { private: double gPriv; public: void gFoo ( );
};
-
8/3/2019 11267 Inheritance (1)
16/31
-
8/3/2019 11267 Inheritance (1)
17/31
Constructor Rules for Derived ClassesThe default constructor and the destructor of the
base class are always called when a new objectof a derived class is created or destroyed.
class A { public:
A ( ){cout
-
8/3/2019 11267 Inheritance (1)
18/31
Constructor Rules for Derived ClassesYou can also specify an constructor of the
base class other than the default constructor
class A { public:
A ( ){cout
-
8/3/2019 11267 Inheritance (1)
19/31
Define its Own Members
Point
Circle
class Point{ protected:
int x, y; public:
void set(int a, int b);
};
class Circle : public Point{ private:
double r; public:
void set_r(double c);};
x
y
x
yr
class Circle{ protected:
int x, y;
private:double r;
public:void set(int a, int b);void set_r(double c);
};
T he derived class can also defineits own members, in addition to
the members inherited from thebase class
-
8/3/2019 11267 Inheritance (1)
20/31
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
-
8/3/2019 11267 Inheritance (1)
21/31
class Point{ protected:
int x, y; public:
void set(int a, int b){x=a; y=b;}
void foo ();void print ();
};
class Circle : public Point{ private: double r; public:
void set (int a, int b, double c) {
Point :: set(a, b); //same name function callr = c;
}void print (); };
A ccess a Method
Circle C;C.set(10,10,100); // from class CircleC.foo (); // from base class PointC. print (); // from class Circle
Point A ;A .set(30,50); // from base class PointA . print (); // from base class Point
-
8/3/2019 11267 Inheritance (1)
22/31
Pu tting T hem T ogetherTime is the base class
E xtTime is the derived class with public inheritance
The derived class can inherit all members from the base
class, except the constructor access all public and protected
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
E xtTime
Time
-
8/3/2019 11267 Inheritance (1)
23/31
class Time S pecification
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 ;
} ;
// SPECIFIC A TION FILE ( time.h)
-
8/3/2019 11267 Inheritance (1)
24/31
Class Interface D iagram
Protected data:
hrs
mins
secs
Set
Increment
Write
Time
Time
Time class
-
8/3/2019 11267 Inheritance (1)
25/31
Derived Class Ext Time
// SPECIFIC A TION FILE ( e x ttime.h)
#include time.h
enum ZoneType { ES T, C S T, M S T, P S T, E DT, CDT, MDT, PDT } ;
class Ex
tTime : public Time// Time is the base class and use public inheritance{
public :
void Set ( int h, int m, int s, ZoneType timeZone ) ;void Write ( ) const; //overridden
E x tTime (int initH, int initM, int initS, ZoneType initZone ) ;E x tTime (); // default constructor
private :ZoneType zone ; // added data member
} ;
-
8/3/2019 11267 Inheritance (1)
26/31
Class Interface D iagram
Protected data:
hrs
mins
secs
Ext Time class
Set
Increment
Write
Time
Time
Set
Increment
Write
E x tTime
E x tTime
Private data:zone
-
8/3/2019 11267 Inheritance (1)
27/31
Implementation of Ext Time
Default Constructor
E x tTime :: E x tTime ( )
{zone = EST ;
}
The default constructor of base class, Time(), isautomatically called, when anE xtTime object is created.
E xtTime et1;
hrs = 0
mins = 0secs = 0zone = ES T
et1
-
8/3/2019 11267 Inheritance (1)
28/31
Implementation of Ext Time
A nother Constructor
E x tTime :: E x tTime (int initH, int initM, int initS, ZoneType initZone): Time (initH, initM, initS)// constructor initializer
{zone = initZone ;
}
E xtTime *et2 =new E xtTime(8,30,0, ES T);
hrs = 8mins = 30secs = 0
zone =ES
T
et2
5000
???
6000
5000
-
8/3/2019 11267 Inheritance (1)
29/31
Implementation of Ext Time
void E x tTime :: Set (int h, int m, int s, ZoneType timeZone){
Time :: Set (hours, minutes, seconds); // same name function call
zone = timeZone ;
}
void E x tTime :: Write ( ) const // function overriding{
string zone S tring[8] ={ ES T, C S T, M S T, P S T, E DT, CDT, MDT, PDT} ;
Time :: Write ( ) ;cout
-
8/3/2019 11267 Inheritance (1)
30/31
Working with Ext Time
#include e x ttime.h
int main(){
E x tTime thisTime ( 8, 35, 0, PST ) ;E x tTime thatTime ; // default constructor called
thatTime.Write( ) ; // outputs 00:00:00 ES T
thatTime.Set (16, 49, 23, C D T) ;
thatTime.Write( ) ; // outputs 16:49:23 CDTthisTime.Increment ( ) ;thisTime.Increment ( ) ;thisTime.Write ( ) ; // outputs 08:35:02 P S T
}
-
8/3/2019 11267 Inheritance (1)
31/31
T ake Home Message
Inheritance is a mechanism for defining newclass types to be a specialization or anaugmentation of existing types.
In principle, every member of a base class is
inherited by a derived class with differentaccess permissions, except for the constructors