Polymorphism and types of polymorphism

Q. What do you mean by polymorphism? Explain all types of polymorphism.

                                            OR

What is polymorphism? Give an example to illustrate that public member data of base class member becomes the private member of data to derived class and the base class member of the data cannot be accessed in the main function (C++ should be used).  

Ans.

 

Polymorphism: The term polymorphism is derived from the Greek meaning that is many forms. It allows a single name to be used for more than a related purpose which is technically different. Polymorphism allows the programmer to create high level reusable components that may be tailored to fit distinct applications by altering their low level parts. It is a feature of Object oriented programming language. It merely means one name, multiple forms. It is done by overloading. There are three type of overloading:

 

1.      Operator  overloading

 

2.      Function  overloading

3.      Virtual function overloading

The overloaded member functions are selected for invoking by matching arguments, both type and number. This information is known to the compiler at the compile time and compiler is able to select the appropriate function for the particular part call at compile time itself. This is knows as early binding or static linking or static binding. Also called as compile time polymorphism, early binding just means that an object is bound to its function call at compile time. Now let us consider a situation where the function name and prototype is same in both the base and derived classes.

Example

Class Arun

{

Int x;

Void show ()

};

Class Lokesh : public Arun

{

Int y;

Public :

Void show ()

};

Here function shows its overloaded condition. It would be nice if the appropriate member function could be selected while the programme is running, this is known as run time polymorphism.

Example

Class A {

Public :

Int X;

Private :

Int Y;

Public :

Void show () {

Cout << "it is base class";

}

 Class B public A

{

Private

Int X;

Public :

     Void show () {

Cout << "it is derived class";

}

};

Void main ();

A a;

B b;

b. show ();

a. show ();

}

   Related Questions in Programming Languages

©TutorsGlobe All rights reserved 2022-2023.