810 likes | 2.19k Views
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
E N D
Unit - 07 PolymorphismandVirtual 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 • Early vs. late binding • Virtual functions • Abstract base classes and pure virtual functions • Virtual base class • Virtual destructors
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
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
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
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’); }
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
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”; } };
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 }
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
Example: Virtual Functions class MyClass { public: virtual void MyFunction() { cout << “virtual function”; } };
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
Abstract Class • A class that can not be instantiated • Used to have a common interface or functionality for all its derived classes
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”; // … } };
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 }
Virtual Base Class • The derived class is virtually inherited from base class • Derived class shares a single common instance of the (same) base class
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
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(); }
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
Example: Virtual Destructor class Base { public: virtual ~Base() // virtual destructor { cout << "Base Destructor"; } }; class Derived : public Base { public: ~Derived() // virtual destructor { cout << "Derived Destructor"; } };
Example: Virtual Destructor (contd.) void main() { Base* bp = new Derived(); // upcast delete bp; }
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