Access Rights of Derived Classes The type of inheritance defines the access level for the members of derived class that are inherited from the base class -protected private protected p
Trang 1UNIT-IV
Trang 5x y r
x y z
Trang 6Inheritance Concept
6
RealNumbe r
ComplexNumbe r
3D-Point
Trang 7Why Inheritance ?
7
Inheritance is a mechanism for
Trang 8Define a Class Hierarchy
Thus a derived class can also be a base class
Trang 9class Sphere : public 3D-Point{
private:
double r;
… … };
Sphere
Point is the base class of 3D-Point , while 3D-Point is the base class of Sphere
Trang 11Access Control Over the Members
Trang 12Access Rights of Derived
Classes
The type of inheritance defines the access level for the members of derived class that are inherited from the base class
-protected private protected protected
Trang 13Class Derivation
13
class daughter :
-mother{
private: double dPriv;
public: void mFoo ( );
};
class mother{
protected: int mProc;
public: int mPubl;
private: int mPriv;
};
class daughter :
-mother{
private: double dPriv;
public: void dFoo ( );
private: double gPriv;
public: void gFoo ( );
};
Trang 14What to inherit?
In principle , every member of a base class is inherited by a derived class
just with different access permission
constructor and destructor
friends
Since all these functions are class-specific
Trang 15Constructor Rules for Derived
Classes
15
The default constructor and the destructor of the base class are always called when a new object of a derived class is created or destroyed.
class A {
public:
A ( ) {cout<<
“A:default”<<endl;}
A (int a)
{cout<<“A:parameter”<<endl
;}
};
class B : public A {
public:
B (int a)
{cout<<“B”<<endl;} };
B test(1); output: A:defaultB
Trang 16Constructor Rules for Derived Classes
You can also specify an constructor of the base class other than the default constructor
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;}
};
C test(1); output: A:parameterC
DerivedClassCon ( derivedClass args ) : BaseClassCon ( baseClass args )
{ DerivedClass constructor body }
Trang 17Define its Own Members
};
x y
The derived class can also define
its own members, in addition to
the members inherited from the
base class
Trang 18class B : public A { public:
void print () {cout<<“From B”<<endl;}
};
Trang 19r = c;
} void print (); };
Access a Method
Circle C;
C set (10,10,100); // from class Circle
C foo (); // from base class Point
C print (); // from class Circle
Trang 20 Time is the base class
ExtTime 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
Putting Them Together
ExtTime
Time
Trang 21class Time Specification
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 ;
} ;
Trang 22Class Interface Diagram
Protected data:
hrs mins secs
Set Increment Write Time Time
Time class
Trang 23Derived Class ExtTime
#include “time.h”
enum ZoneType {EST, CST, MST, PST, EDT, CDT, MDT, PDT } ;
class ExtTime : public Time
// Time is the base class and use public inheritance{
Trang 24Class Interface Diagram
Protected data:
hrs mins secs
ExtTime class
Set Increment Write Time Time
Set Increment Write ExtTime
ExtTime
Private data:
zone
Trang 25The default constructor of base
class, Time(), is automatically
called, when an ExtTime object is
created
ExtTime et1;
hrs = 0mins = 0secs = 0zone = ESTet1
Trang 26Implementation of ExtTime
Another Constructor
ExtTime :: ExtTime (int initH, int initM, int initS, ZoneType initZone)
: Time (initH, initM, initS)
5000
???
6000 5000
Trang 27void ExtTime :: Write ( ) const // function overriding
Trang 28Working with ExtTime
thatTime.Write( ) ; // outputs 16:49:23 CDT thisTime.Increment ( ) ;
thisTime.Increment ( ) ; thisTime.Write ( ) ; // outputs 08:35:02 PST }
Trang 29existing classes is called inheritance.
derived class.
in a hierarchical form.
Trang 30DERIVED CLASS DECLARATION
Trang 31Three possible styles of
Three possible styles of derivation :
1.class D:public B // public derivation
{
// members of D };
2.class D:private B // private derivation
Trang 32MULTIPLE INHERITANCE
called multiple inheritance.
C
Trang 33Constructor in a multiple inherited class with default invocation
Trang 35// Time :: write()
Trang 36Static Binding
When the type of a formal parameter is a parent class, the
argument used can be:
the same type as the formal parameter, or,
any derived class type.
for a particular object based on the type of the formal
parameter
When pass-by-value is used, static binding occurs
Trang 37// Time :: write()
Trang 38Polymorphism – An Introduction
Also Known as “ Late Binding ”
Trang 39Dynamic Binding
Is the run-time determination of which function to call for a particular object of a derived class based on the type of the argument
Declaring a member function to be virtual instructs the
compiler to generate code that guarantees dynamic binding
Dynamic binding requires pass-by-reference
Trang 40Virtual Member Function
// SPECIFICATION FILE ( time.h )
class Time
{
public :
.
virtual void Write ( ) ; // for dynamic binding
virtual ~Time(); // destructor
Trang 41ExtTime::write()
Trang 42Virtual Functions
Virtual Functions overcome the problem of run time object
determination
delay the object interpretation
Virtual function in base class stays virtual in all the derived classes
It can be overridden in the derived classes
But, a derived class is not required to re-implement a virtual
function If it does not, the base class version is used
Trang 43Polymorphism Summary:
When you use virtual functions, compiler store additional information about the types of object available and created
Important :
virtual functions work only with pointers/references
Not with objects even if the function is virtual
If a class declares any virtual methods, the destructor of the class should be declared as virtual as well
Trang 44Abstract Classes & Pure Virtual
Functions
Some classes exist logically but not physically
Shape s; // Legal but silly !! : “Shapeless shape”
Shape makes sense only as a base of some classes derived from it Serves as a “category”
Hence instantiation of such a class must be prevented
{
//Pure virtual Function
Trang 45Example
Shape virtual void draw()
Circle public void draw()
Triangle public void draw()
Trang 46A pure virtual function not defined in the derived class remains a pure virtual function.
class Circle : public Shape { //No draw() - Abstract
void draw(){ // Override Shape::draw()
cout << “Drawing Rectangle” << endl;
}
Rectangle r; // Valid
Circle c; // error : variable of an abstract class
Trang 47Pure virtual functions : Summary
Trang 48Summary continued
the base class
the derived classes, but a common piece of code can be kept there
to facilitate reuse
}
Trang 49Virtual Function
In C++,it indicates the form of amember function that can be changed at runtime.such member functions
are called virtual function.
Syntax
virtual returntype functionname(arguments)
{
……
……
}
Virtual function should be declared in public section.
Trang 50Pure virtual function
The virtual functions are defined with a null body,it has no definition.such functions in the base class are similar to do- nothing or dummy functions and in C++,they are called as
pure virtual function
Pure virtual function is declared as a virtual function with its declaration followed by=0.
A class containing pure virtual functions cannot be used to define any objects of its own and hence such classes are called pure abstract classes
All other classes without pure virtual functions and which are instantiated are called as concrete classes
Trang 51 RTTI means Run time type identification
It provides a mechanism for safely determing the type
pointed at by a base class pointer at run time.
It involves dynamic_cast, an operator on a base class pointer.
It involves typeid , an operator for determing the type of
an object.
It involves type_info , a structure providing run-time information for the associated type.
Trang 52 The dynamic_cast opertor has the form
dynamic_cast <type> (v)
type refers to pointer or reference to a class type.
v refes to corresponding pointer value or reference value.
Dynamic_cast is used with classes having virtual functions.
In cast converting process, zero or NULL pointer is returned.Then it
is known as down_cast
Operator typeid() can be applied to atype_name or to an expression to determine the exact type of the argument.