1 / 21

CMSC 202

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

Download Presentation

CMSC 202

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 202 Computer Science II for Majors

  2. Topics • Inheritance • Polymorphism

  3. 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

  4. Inheritance … cont General Form Vehicle Specialization Generalization Car Truck Special Form

  5. 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

  6. Inheritance … cont A A • Types B B C D Single Hierarchical A B A B C C Multilevel Multiple

  7. 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

  8. 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

  9. 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

  10. Inheritance … cont Class A Private Not inheritable Protected Public Class B : public A Private Protected Public Public Inheritance

  11. 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

  12. 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

  13. 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

  14. 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

  15. Polymorphism … cont Polymorphism Compile Time Run Time Virtual Functions Operator overloading Function overloading

  16. 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

  17. 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;

  18. Polymorphism … cont • Consider the following class hierarchy • A function GetArea() is used to display area of the particular shape Shape Rectangle Triangle

  19. 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 }

  20. 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 }

  21. 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; }

More Related