1 / 23

Polymorphism and Virtual Functions

Unit - 07. Polymorphism and Virtual Functions. One name many shapes behaviour. Unit Introduction. This unit covers some basic concept of polymorphism and virtual functions. Unit Objectives. After covering this unit you will understand… Polymorphism and its different types

bjorn
Download Presentation

Polymorphism and Virtual Functions

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. Unit - 07 PolymorphismandVirtual Functions One name many shapes behaviour

  2. Unit Introduction This unit covers some basic concept of polymorphism and virtual functions

  3. Unit Objectives After covering this unit you will understand… • Polymorphism and its different types • Early vs. late binding • Virtual functions • Abstract base classes and pure virtual functions • Virtual base class • Virtual destructors

  4. Concept of Polymorphism • One of the essential features of an object-oriented programming language • Polymorphism means many shapes • Types of polymorphism • static polymorphism • dynamic polymorphism

  5. Binding • Connecting a function call to a function body is called binding • When a function is called, which function definition to invoke, when there are more than one definitions • Types of binding • Static or Early binding • Dynamic or Late binding

  6. Static or Early Binding • At compile time, it is known which function definition will be invoked upon function call • Can be achieved through function overloading

  7. Example: Early Binding / Static Polymorphism void Function(int val) // function with an int as parameter { cout << “Integer value is: ” << val; } void Function(char val) // function with char as parameter { cout << “Character value is: ” << val; } void main() { Function(5); Function(‘C’); }

  8. Dynamic or Late Binding • Which function definition to invoke is deferred and decided late at run time • Decision is made based upon which address is stored in base class’s pointer • Achieved when functions are overridden by derived classes • Overridden functions should be declared as virtual in base class

  9. Example: Late Binding / Dynamic Polymorphism class Base { public: virtual void Display() { cout << “Base Class”; } }; class Derived : public Base { public: //override interface function void Display() { cout << “Derived Class”; } };

  10. Example: Late Binding / Dynamic Polymorphism (contd.) void main() { Derived derivedObj; // creating a derived class object Base* ptr; // creating a base class pointer ptr = &derivedObj; // assigning address of derived class// object to base class pointer ptr->Display(); // derived class display() will be called }

  11. Virtual Functions • A means to achieve dynamic polymorphism • A virtual function can have a definition in base class, called in case derived class does not override that function • A virtual function declaration contains virtual keyword

  12. Example: Virtual Functions class MyClass { public: virtual void MyFunction() { cout << “virtual function”; } };

  13. Pure Virtual Functions • Pure virtual function ensures that all derived classes must override it • A pure virtual function,Vf, can have (optional) definition in base class, can only be called using Base::Vf() • Pure virtual function declaration end with = 0; an indication that it is a pure virtual function • A class becomes Abstract if it contains at least one pure virtual function

  14. Abstract Class • A class that can not be instantiated • Used to have a common interface or functionality for all its derived classes

  15. Example: Pure virtual Functions / Abstract Class class Shape { public: virtual void Draw() = 0; // pure virtual function }; class Line : public Shape { public: //override interface function void Draw() // also virtual in derived class { cout << “Line is being drawn”; // … } };

  16. Example: Pure virtual Functions / Abstract Class (contd.) void main() { // Shape obj; // error as Shape class is abstract Line lineObj; // creating a derived class object Shape* ptr; // creating a base class pointer ptr = &linedObj; // assigning address of derived class// object to base class pointer ptr->Draw(); // derived class draw() will be called }

  17. Virtual Base Class • The derived class is virtually inherited from base class • Derived class shares a single common instance of the (same) base class

  18. Example: Virtual Base Class class Base { protected: int m_BaseData; Base() { m_BaseData = 1; // initializing m_BaseData } }; // virtually derived from base class class Derived1 : virtual public Base // shares single copy of {}; // parent // virtually derived from base class class Derived2 : virtual public Base // shares single copy of {}; // parent

  19. Example: Virtual Base Class (contd.) // multiply inherited from both Derived1 and Derived2 classes class Derived12 : public Derived1, Derived2 { public: int GetBaseData() { return m_BaseData; } // only one copy of parent }; void main() { Derived12 obj; cout << obj.GetBaseData(); }

  20. Virtual Destructor • Virtual destructor mechanism allows the right destructor invocation of derived class, when dealing with dynamic polymorphism • A means to avoid memory leak • Use of virtual keyword with base class destructor • Automatically make all derived class destructors virtual

  21. Example: Virtual Destructor class Base { public: virtual ~Base() // virtual destructor { cout << "Base Destructor"; } }; class Derived : public Base { public: ~Derived() // virtual destructor { cout << "Derived Destructor"; } };

  22. Example: Virtual Destructor (contd.) void main() { Base* bp = new Derived(); // upcast delete bp; }

  23. Unit Summary In this unit you have covered … • How to implement polymorphism using virtual functions • Different types of polymorphism • Difference between early and late binding • What are abstract classes and pure virtual function • Concept of Virtual destructors

More Related