Presentation is loading. Please wait.

Presentation is loading. Please wait.

Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.

Similar presentations


Presentation on theme: "Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07."— Presentation transcript:

1 Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07

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 Polymorphism and its different types Early vs. late binding Early vs. late binding Virtual functions Virtual functions Abstract base classes and pure virtual functions Abstract base classes and pure virtual functions Virtual base class Virtual base class Virtual destructors Virtual destructors

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

5 Binding Connecting a function call to a function body is called 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 When a function is called, which function definition to invoke, when there are more than one definitions Types of binding Types of binding Static or Early binding Static or Early binding Dynamic or Late 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 At compile time, it is known which function definition will be invoked upon function call Can be achieved through function overloading 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; cout << “Integer value is: ” << val;} void Function(char val) // function with char as parameter { cout << “Character value is: ” << val; cout << “Character value is: ” << val;} void main() { Function(5); Function(5); Function(‘C’); Function(‘C’);}

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

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

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

11 Virtual Functions A means to achieve dynamic polymorphism 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 can have a definition in base class, called in case derived class does not override that function A virtual function declaration contains virtual keyword A virtual function declaration contains virtual keyword

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

13 Pure Virtual Functions Pure virtual function ensures that all derived classes must override it 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() 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 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 A class becomes Abstract if it contains at least one pure virtual function

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

15 Example: Pure virtual Functions / Abstract Class class Shape { public: public: virtual void Draw() = 0; // pure virtual function }; class Line : public Shape { public: 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 // Shape obj; // error as Shape class is abstract Line lineObj; // creating a derived class object Line lineObj; // creating a derived class object Shape* ptr; // creating a base class pointer Shape* ptr; // creating a base class pointer ptr = &linedObj; // assigning address of derived class // object to base class pointer ptr = &linedObj; // assigning address of derived class // object to base class pointer ptr->Draw(); // derived class draw() will be called ptr->Draw(); // derived class draw() will be called}

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

18 Example: Virtual Base Class class Base { protected: protected: int m_BaseData; Base() { m_BaseData = 1; // initializing m_BaseData 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: public: int GetBaseData() { return m_BaseData; return m_BaseData; } // only one copy of parent }; void main() { Derived12 obj; Derived12 obj; cout << obj.GetBaseData(); cout << obj.GetBaseData();}

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

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

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

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


Download ppt "Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07."

Similar presentations


Ads by Google