0% found this document useful (0 votes)
48 views19 pages

Lecture 21-22

Polymorphism in C++ can occur at compile-time or run-time. Compile-time polymorphism includes function overloading and operator overloading, while run-time polymorphism uses virtual functions. Base class pointers can point to derived class objects and invoke virtual functions polymorphically at run-time based on the actual object. Virtual functions support the "one interface, multiple methods" philosophy and must have matching signatures in derived classes. Destructors can also be virtual to ensure the proper derived class destructor is called when deleting polymorphic objects.

Uploaded by

K S Nagill
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views19 pages

Lecture 21-22

Polymorphism in C++ can occur at compile-time or run-time. Compile-time polymorphism includes function overloading and operator overloading, while run-time polymorphism uses virtual functions. Base class pointers can point to derived class objects and invoke virtual functions polymorphically at run-time based on the actual object. Virtual functions support the "one interface, multiple methods" philosophy and must have matching signatures in derived classes. Destructors can also be virtual to ensure the proper derived class destructor is called when deleting polymorphic objects.

Uploaded by

K S Nagill
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Polymorphism

Lec 21-22
Types of Polymorphism
• Compile time
– Uses static or early binding.
– Example:
✓ Function overloading and Operator overloading.
• Run time
– Uses dynamic or late binding.
– Example:
✓ Virtual functions.
Run-time Polymorphism
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;
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.
Example
1. #include<iostream> 13. int main()
2. using namespace std; 14. { base b1;
3. class base { 15. b1.show(); // base
4. public: 16. derived d1;
5. void show() 17. d1.show(); // derived
6. { cout << "base\n"; } 18. base *pb = &b1;
7. }; 19. pb->show(); // base
8. class derived : public base { 20. pb = &d1;
9. public: 21. pb->show(); // base
10. void show() 22. }
11. { cout << "derived\n"; }
12. };
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.
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.
Example – 1
1. #include<iostream> 13. int main()
2. using namespace std; 14. { base b1;
3. class base { 15. b1.show(); // base
4. public: 16. derived d1;
5. virtual void show() 17. d1.show(); // derived
6. { cout << "base\n"; } 18. base *pb = &b1;
7. }; 19. pb->show(); // base
8. class derived : public base { 20. pb = &d1;
9. public: 21. pb->show(); // derived
10. void show() 22. }
11. { cout << "derived\n"; }
12. };
Example – 2
1. #include<iostream> 15. int main()
2. using namespace std; 16. { base *pb;
3. class base {
17. d1 od1;
4. public:
5. virtual void show() 18. d2 od2;
6. { cout << "base\n"; } }; 19. int n;
7. class d1 : public base { 20. cin >> n;
8. public: 21. if (n % 2)
9. void show() 22. pb =
10. { cout << "derived – 1\n"; } };
11. class d2 : public base { &od1;
12. public: 23. else
13. void show() 24. pb =
14. { cout << "derived – 2\n"; } }; &od2;
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.
Example (Non-virtual Destructor)
1. #include<iostream> Output
2. using namespace std;
3. class base destructing base
4. { public:
5. ~base()
6. { cout << "destructing base\n"; } };
7. class derived : public base
8. { public:
9. ~derived()
10. { cout << "destructing derived\n"; } };
11. int main()
12. { base *p = new derived;
13. delete p;
14. return 0; }
Example (Virtual Destructor)
1. #include<iostream> Output
2. using namespace std;
3. class base destructing
4. { public: derived
5. virtual ~base() destructing base
6. { cout << "destructing base\n"; } };
7. class derived : public base
8. { public:
9. ~derived()
10. { cout << "destructing derived\n"; } };
11. int main()
12. { base *p = new derived;
13. delete p;
14. return 0; }
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.
Abstract Class &Pure Virtual Function

class A \\ Abstract Class


{ virtual void disp() = 0; \\Pure virtual function
};

Not possible- A obj;


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;
};
#include <iostream> class C: public B
using namespace std; { public:
void disp()
class A {cout<<"Welcome";}
{ public: };
virtual void disp()= 0;
}; int main()
class B: public A { C obj;
{ public: obj.disp();
virtual void disp() = 0; return 0;
}; }
Pointer to Abstract Class
#include <iostream>
using namespace std; int main()
{ A *p;
class A C obj;
{ public: p= &obj;
virtual void disp()= 0; p->disp();
}; return 0;
class C: public A }
{ public:
void disp()
{cout<<"Welcome";} OUTPUT
}; Welcome
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.

You might also like