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

  • Q : ASP.NET Session handling How to set

    How to set Session time out in ASP.NET. I tries changing in config file but still facing issue. Please help..

  • Q : Define Encapsulation Encapsulation :

    Encapsulation: Safeguarding the state of objects by stating its attributes as private and channeling entrance to them via accessor and mutator techniques.

  • Q : What is Carriage return Carriage return

    Carriage return: The \r character. It is also used as a synonym for the `Return' or `Enter' key employed to terminate a line of text and the name derives from the carriage on mechanical typewriter.

  • Q : Define the term Toggle Define the term

    Define the term Toggle: To alternate among two values, like true and false, on and off, or 1 & 0.

  • Q : Transaction processing monitor or TP

    What is a transaction processing (TP) monitor?

  • Q : Define HyperText Markup Language

    HyperText Markup Language: The HyperText Markup Language (abbreviated as HTML) is a simple presentation language employed to markup the content of the Web pages. Its tags appear frequently in pairs to mark sections of text which must be represented in

  • Q : What is Public interface Public

    Public interface: The members of a class prefixed with public access modifier. All these members are visible to each and every class in a program.

  • Q : How would you extract an exact

    How would you extract an exact attribute by using XSLT, from an element into an XML document?

  • Q : Define the term Scheduler Define the

    Define the term Scheduler: The portion of the Java Virtual Machine (abbreviated as JVM) which is responsible for managing the threads.

  • Q : Describe Overriding for restriction

    Overriding for restriction: It is a form of method overriding in which the sub-class version of a method calls the super-class version first of all and then employs or manipulates the outcome or consequences of that call in some manner.

©TutorsGlobe All rights reserved 2022-2023.