Upload
dieter-eaton
View
26
Download
1
Embed Size (px)
DESCRIPTION
CMSC 202. Computer Science II for Majors. Topics. Inheritance Polymorphism. Inheritance – Extending Classes. Inheritance It is a mechanism of deriving a new class from an old class It promotes Code Reusability It implements the “Is a” relationship between objects - PowerPoint PPT Presentation
Citation preview
CMSC 202 UMBC
Inheritance – Extending Classes
Inheritance It is a mechanism of deriving a new class
from an old class It promotes Code Reusability It implements the “Is a” relationship
between objects Thus supports the concept of hierarchical
classification
CMSC 202 UMBC
Inheritance … cont
Vehicle
Car Truck
General Form
Special Form
Sp
ecia
liza
tion
Ge
ner
aliz
atio
n
CMSC 202 UMBC
Inheritance … cont
Terminology General class (Vehicle) is called as base
class or super class Specialized class (Car, Truck) is called as
derived class or sub class Thus a derived class inherits all attributes
and behavior of base class and may use, extend, modify or replace the base class behavior
CMSC 202 UMBC
Inheritance … cont
Types
Single Hierarchical
Multiple
A A
A
B B
B
C
C
D
Multilevel
A
B
C
CMSC 202 UMBC
Inheritance … cont Defining Derived Class
class derived-class : visibility-mode base-class{
..
.. // derived class members};e.g.class Car : public Vehicle{
..};
Visibility mode specifies whether features of base class are derivedpublicly or privately
We will deal only with Public inheritance
CMSC 202 UMBC
Inheritance … cont
Public Inheritance class ABC : public XYZ {
// members of ABC };
Public members of base class become public members of derived class
Private members of base class are not inherited
CMSC 202 UMBC
Inheritance … cont
Inheriting Private members of Base class What if private data needs to be inherited ? Making that data public would violate data
hiding Use third access specifier : protected A protected data member in base class is accessible
within base class and any class immediately derived from it
CMSC 202 UMBC
Inheritance … cont
Private
ProtectedPublic
Private
ProtectedPublic
Class A
Not inheritable
Class B : public A
Public Inheritance
CMSC 202 UMBC
Inheritance … cont
Extending Classes Derived class can add / modify base class
data and member functions extending functionality of base class
Function overriding : Derives class can override an inherited method of base class by
having same signature as base class method
CMSC 202 UMBC
Polymorphism
Polymorphism – 'One name, multiple forms' Have we seen this before ? Function overloading : A function is selected
for invoking by matching signature This information is available to compiler at
compile time – early binding or static binding
Thus object is bound to function call at compile time
CMSC 202 UMBC
Polymorphism … cont
Consider following hierarchyclass A{
...public :
void show();};class B : public A // public inheritance{
...public :
void show();};
How would the appropriate function be selected at run time ? – Runtime Polymorphism
CMSC 202 UMBC
Polymorphism … cont
Runtime Polymorphism C++ supports runtime polymorphism using
virtual functions Selection of appropriate function done at run
time – late binding or dynamic binding This mechanism used pointers or
references to objects
CMSC 202 UMBC
Polymorphism … cont
Polymorphism
Compile Time Run Time
Function overloading
Operator overloading
Virtual Functions
CMSC 202 UMBC
Polymorphism … cont
Virtual Functions When we have same function name in base
and derived classes, function in base class is declared virtual
Which function to use at run time depends on type of object pointed to by the base pointer
Thus runtime polymorphism is achieved when a virtual function is accessed through a pointer to base class
CMSC 202 UMBC
Polymorphism … cont
Pure virtual functions A virtual function declared in base class, but
having no definition relative to base class It serves as a place holder Derived class has to provide definition for it A class containing pure virtual functions
cannot be used to declare objects of its own e.g. : virtual void show() = 0;
CMSC 202 UMBC
Polymorphism … cont
Consider the following class hierarchy
A function GetArea() is used to display area of the particular shape
Shape
Rectangle Triangle
CMSC 202 UMBC
Polymorphism … cont// Shape Classclass Shape { public:
virtual double GetArea() const = 0; // Pure virtual function};// Rectangle Classclass Rectangle : public Shape // Public Inheritance{ public :
Rectangle(int length=0, int breadth=0); // Constructorvirtual double GetArea() const; // Overriding Shape :: GetArea()
private :int m_length;int m_breadth;
};double Rectangle :: GetArea() const{
// code for calculating area}
CMSC 202 UMBC
Polymorphism … cont// Triangle Classclass Triangle : public Shape // Public Inheritance{ public :
Triangle(int base=0, int height=0); // Constructorvirtual double GetArea() const;
private :int m_base;int m_height;
};
double Triangle :: GetArea() const{
// code for calculating area}
CMSC 202 UMBC
Polymorphism … contint main(){ Rectangle rectangle1(12,10); // Create a rectangle object Triangle triangle1(2,3); // Create a triangle object Shape *ptr; // Pointer to base class
ptr = &rectangle1; // Base class pointer points to derived // class object
ptr -> GetArea(); // Will invoke function in Rectangle class
ptr = &triangle1; ptr -> GetArea(); // Will invoke function in Triangle class
return 0;
}