Download presentation
Presentation is loading. Please wait.
1
Polymorphism Lec 21-22
2
Types of Polymorphism Compile time Uses static or early binding.
Example: Function overloading and Operator overloading. Run time Uses dynamic or late binding. Virtual functions.
3
Run-time Polymorphism
4
Pointers to Derived Classes
Base class pointers can point to derived class objects. Example: Let there be two classes, class base { … }; class derived : public base { … }; Then, base *p1; derived d_obj; p1 = &d_obj;
5
Contd… A base class pointer pointing to a derived class object
Has knowledge only of the base class. Knows nothing about the members added by the derived class. Thus, Members of the derived object that were inherited from the base class can only be accessed.
6
Example #include<iostream> using namespace std; class base {
public: void show() { cout << "base\n"; } }; class derived : public base { { cout << "derived\n"; } int main() { base b1; b1.show(); // base derived d1; d1.show(); // derived base *pb = &b1; pb->show(); // base pb = &d1; }
7
Virtual Functions A member function declared within a base class are redefined by a derived class. (Overriding) Implement the "one interface, multiple methods" philosophy that underlies polymorphism. The keyword virtual is used to designate a member function as virtual. Support run-time polymorphism with the help of base class pointers.
8
Contd… While redefining a virtual function in a derived class,
The function signature must match the original function present in the base class. The keyword virtual is not needed (but can be specified). The "virtual"-ity of the member function continues along the inheritance chain. A class that contains a virtual function is referred to as a polymorphic class.
9
Example – 1 #include<iostream> using namespace std; class base {
public: virtual void show() { cout << "base\n"; } }; class derived : public base { void show() { cout << "derived\n"; } int main() { base b1; b1.show(); // base derived d1; d1.show(); // derived base *pb = &b1; pb->show(); // base pb = &d1; pb->show(); // derived }
10
Example – 2 int main() { base *pb; d1 od1; d2 od2; int n;
#include<iostream> using namespace std; class base { public: virtual void show() { cout << "base\n"; } }; class d1 : public base { void show() { cout << "derived – 1\n"; } }; class d2 : public base { { cout << "derived – 2\n"; } }; int main() { base *pb; d1 od1; d2 od2; int n; cin >> n; if (n % 2) pb = &od1; else pb = &od2; pb->show(); return 0; }
11
Virtual Destructors Constructors cannot be virtual, but destructors can be virtual. It ensures that the derived class destructor is called when a base class pointer is used while deleting a dynamically created derived class object.
12
Example (Non-virtual Destructor)
#include<iostream> using namespace std; class base { public: ~base() { cout << "destructing base\n"; } }; class derived : public base { public: ~derived() { cout << "destructing derived\n"; } }; int main() { base *p = new derived; delete p; return 0; } Output destructing base
13
Example (Virtual Destructor)
#include<iostream> using namespace std; class base { public: virtual ~base() { cout << "destructing base\n"; } }; class derived : public base { public: ~derived() { cout << "destructing derived\n"; } }; int main() { base *p = new derived; delete p; return 0; } Output destructing derived destructing base
14
More About Virtual Functions
To omit the body of a virtual function in a base class, use pure virtual functions. virtual returnType functionName(paramList) = 0; This makes a class an abstract class, i.e. cannot create any objects of such classes. Derived classes should override such functions , otherwise they become abstract too. Pointer to an abstract class can be still be created.
15
Abstract Class &Pure Virtual Function
class A \\ Abstract Class { virtual void disp() = 0; \\Pure virtual function }; Not possible- A obj;
16
Derived class should contain definition of the pure virtual function, else it would also become an abstract class class A { virtual void disp()= 0; }; class B: public class A { virtual void disp() = 0;
17
#include <iostream> using namespace std; class A { public: virtual void disp()= 0; }; class B: public A virtual void disp() = 0; class C: public B { public: void disp() {cout<<"Welcome";} }; int main() { C obj; obj.disp(); return 0; }
18
Pointer to Abstract Class
#include <iostream> using namespace std; class A { public: virtual void disp()= 0; }; class C: public A void disp() {cout<<"Welcome";} int main() { A *p; C obj; p= &obj; p->disp(); return 0; } OUTPUT Welcome
19
Applying Polymorphism
Early binding Normal functions, overloaded functions. Nonvirtual member and friend functions. Resolved at compile time. Very efficient. But lacks flexibility. Late binding Virtual functions accessed via a base class pointer. Resolved at run-time. Quite flexible during run-time. But has run-time overhead; slows down program execution.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.