210 likes | 232 Views
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
E N D
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 • Thus supports the concept of hierarchical classification
Inheritance … cont General Form Vehicle Specialization Generalization Car Truck Special Form
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
Inheritance … cont A A • Types B B C D Single Hierarchical A B A B C C Multilevel Multiple
Inheritance … cont • Defining Derived Class class derived-class : visibility-modebase-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
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
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
Inheritance … cont Class A Private Not inheritable Protected Public Class B : public A Private Protected Public Public Inheritance
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
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
Polymorphism … cont • Consider following hierarchy class 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
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
Polymorphism … cont Polymorphism Compile Time Run Time Virtual Functions Operator overloading Function overloading
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
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;
Polymorphism … cont • Consider the following class hierarchy • A function GetArea() is used to display area of the particular shape Shape Rectangle Triangle
Polymorphism … cont // Shape Class class Shape { public: virtual double GetArea() const = 0; // Pure virtual function }; // Rectangle Class class Rectangle : public Shape // Public Inheritance { public : Rectangle(int length=0, int breadth=0); // Constructor virtual double GetArea() const; // Overriding Shape :: GetArea() private : int m_length; int m_breadth; }; double Rectangle :: GetArea() const { // code for calculating area }
Polymorphism … cont // Triangle Class class Triangle : public Shape // Public Inheritance { public : Triangle(int base=0, int height=0); // Constructor virtual double GetArea() const; private : int m_base; int m_height; }; double Triangle :: GetArea() const { // code for calculating area }
Polymorphism … cont int 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; }