0% found this document useful (0 votes)
12 views11 pages

29-Dynamic Polymorphism-15-04-2024

Uploaded by

Shantanu Nanda
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)
12 views11 pages

29-Dynamic Polymorphism-15-04-2024

Uploaded by

Shantanu Nanda
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/ 11

DYNAMIC

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

Input Code and Price for item: 11


100
Input Code and Price for item: 22
200

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.

The selection of function to be executed is decided dynamically at


runtime is called Virtual function. The process is called Late Binding
or Dynamic Polymorphism.

The selection of function to use at run-time is based on the type of object


pointed to by the base pointer, rather than the type of pointer.

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.

A class containing pure virtual function cannot be used to declare any


objects of its own, such classes are called abstract class or abstract
base class.

The main objective of an abstract class is to provide some traits to the


derived classes and to create a base class pointer to achieve runtime
polymorphism.
9
#include<iostream> Pure Virtual Function Ex. 05
using namespace std;
class BaseClass
{
public:
virtual void Display1()=0; int main()
virtual void Display2()=0; {
void Display3() DerivedClass D;
{
cout<<"\n\tDisplay3() method of Base Class"; D.Display1();
} }; D.Display2();
D.Display3();
class DerivedClass : public BaseClass return 0;
{ }
public:
void Display1()
{
cout<<"\n\tDisplay1() method of Derived Class";
}
void Display2()
{
cout<<"\n\tDisplay2() method of Derived Class";
} }; 10
Abstract Classes
An abstract class is a class that is designed to be specifically used as a
base class.
It is designed to only act as a base class
Object creation is not possible through abstract class
It is a design concept in program development and provides a base
upon which other classes may be built

Example:
class AB // abstract class
{
public:
virtual void f() = 0;
};

11

You might also like