Home | | Object Oriented Programming and Data Structures | Polymorphism and Different Types of Polymorphism

Chapter: Object Oriented Programming and Data Structure : Inheritance and Polymorphism

Polymorphism and Different Types of Polymorphism

Define polymorphism. Explain the different types of polymorphism.

 

Polymorphism

 

Run time compile time Or

 

Dynamic binding static binding Or

late binding early binding

Virtual fuctions operator overloading function overloading

Linking a function call to the function definition is called as binding.

  If the binding is done at compile time then it is called as compile time polymorphism. •Compile time polymorphism is achieved by using function overloading and operator

overloading.

• If the binding is done at run time then it is called as run time polymorphism. It is achieved by using virtual functions.

 

VIRTUAL FUNCTIONS:

 

• When the same function is used in both the base class and derived class and if a pointer of base class is used to access the members of the derived class , then the members appropriate to the base class is called.

Example:

#include

 

#include

class base

{

public:

 

void show()

{

 

cout <<”\n This is a base class\n”;

 

}

};

class derived : public base

{

public:

 

void show()

{

 

cout <<”\n This is a derived class\n”;

 

}

};

 

void main()

{

 

base b,*bptr;

bptr=&b;

bptr->show();

derived d;

bptr=&d;

bptr->show();

getch();

}

 

o/p

 

This is a base class

This is a base class

This can be overcome by using virtual functions.

The base class member functions should be preceeded by a keyword virtual.

 

Example:

#include

 

#include

class base

{

public:

virtual void show()

{

 

cout <<”\n This is a base class\n”;

 

}

};

 

class derived : public base

{

public:

 

void show()

{

 

cout <<”\n This is a derived class\n”;

 

}

};

 

void main()

{

 

base b,*bptr;

bptr=&b;

bptr->show();

derived d;

bptr=&d;

bptr->show();

getch();

 

}

 

 

o/p

 

This is a base class

This is a derived class

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Object Oriented Programming and Data Structure : Inheritance and Polymorphism : Polymorphism and Different Types of Polymorphism |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.