29-Dynamic Polymorphism-15-04-2024
29-Dynamic Polymorphism-15-04-2024
POLYMORPHISM
1
Introduction
Consider a Situation,
Class A {
int x;
public:
void show() {…}
};
Class B : public A {
int x;
public:
void show() {…}
};
2
Ex. 01
Pointers to Objects int main()
{
#include <iostream> item *p = new item [10];
using namespace std; item *d = p;
class item int x, i;
{ float y;
int code; for(i=0;i<size;i++)
float price; { cout << "Input Code and Price for
public: item: " << i+1;
void getdata(int a, float b) cin >> x >>y;
{ p->getdata(x,y);
code = a; p++;
price = b; }
} for(i=0;i<size;i++)
void show() {
{ cout << "Item: " << i+1 << "\n";
cout<<"Code: " << code << "\n"; d->show();
cout<<"Price: " << price << "\n"; d++;
} }
}; return 0;
const int size = 2; }
3
Pointers to Objects
Output
Item: 1
Code: 1
Price: 100
Item: 2
Code: 2
Price: 200
4
Why Virtual Function?
An essential requirement of polymorphism is ability to refer to objects
without any regard to the classes. This necessitates the use of single
pointer variable to refer to the objects of different classes.
However, a base class pointer which contains the derived object, cant
access the derived class function. To overcome this issue, we are going
for virtual function.
5
#include <iostream> Need for Virtual Function Ex. 02
using namespace std;
class b int main()
{ {
public: b B;
void display() b *ptr;
{ cout<<"\n\t P points to base:\n" ;
cout<<"\n Displaying base class...." ; ptr=&B;
} ptr->display();
void show() ptr->show();
{ cout<<"\n\n\t P points to derived:\n";
cout<<"\n Showing base class...."; d D;
} }; ptr=&D;
class d:public b ptr->display();
{ ptr->show();
public: }
void display() {
Output
cout<<"\n Displaying derived class....";
P points to base:
}
Displaying base class....
void show() {
Showing base class....
cout<<"\n Showing derived class....";
P points to derive:
}
Displaying base class.... 6
};
Showing base class....
#include <iostream> Example for Virtual Function Ex. 03
using namespace std;
class b int main()
{ {
public: b B;
void display() b *ptr;
{ cout<<"\n\t P points to base:\n" ;
cout<<"\n Displaying base class...." ; ptr=&B;
} ptr->display();
virtual void show() ptr->show();
{ cout<<"\n\n\t P points to derived:\n";
cout<<"\n Showing base class...."; d D;
} }; ptr=&D;
class d:public b ptr->display();
{ ptr->show();
public: }
void display() { Output
cout<<"\n Displaying derived class...."; P points to base:
} Displaying base class....
void show() { Showing base class....
cout<<"\n Showing derived class...."; P points to drive:
} Displaying base class....
}; Showing derived class.... 7
Rules of Virtual Function
• Virtual functions must be members of some class.
• Virtual functions cannot be static members.
• They are accessed through object pointers.
• They can be a friend of another class.
•A virtual function must be defined in the base class, even though it is
not used.
• The prototypes of a virtual function of the base class and all the
derived classes must be identical. If the two functions with the same
name but different prototypes, C++ will consider them as the
overloaded functions.
• We cannot have a virtual constructor, but we can have a virtual
destructor
8
Pure Virtual Function
• A pure virtual function is a function declared in a base class that has
no definition relative to the base class.
• Here, the compiler requires each derived class to either define the
function or re-declare it as a pure virtual function.
Example: Virtual void display( ) = 0;
Such a function is also called as “do-nothing” function.
Example:
class AB // abstract class
{
public:
virtual void f() = 0;
};
11