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 : 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 : Automaton distributed in the class

    Write a code in a c++/java  for the automaton distributed in the class which accepts keywords(cat,bat,cab). Create an input file with these words may be two or three copies of these words scattered in a paragraph and show that your program does accept these words and gives an output to that

  • Q : State the term multi threading State

    State the term multi threading.

  • Q : What is Round robin allocation Round

    Round robin allocation: It is an allocation of time slices which repeatedly cycles regarding a set of eligible threads in the fixed order.

  • Q : Recursive Matlab function Write a

    Write a recursive Matlab function TriUVRCol that solves a virtually upper triangular system of equations accessing the matrix by column. Write another recursive Matlab function LUPivRec that recursively computes the LU decomposition of a matrix using partial pivoting.

  • Q : XPATH selector Normal 0 false false

    Normal 0 false false

  • Q : Procedural and object-oriented programs

    Illustrate the difference between the procedural and object-oriented programs in brief.

  • Q : Template class and class template in

    Illustrate the difference between a template class and class template in the programming?

  • Q : What is Filter stream Filter stream :

    Filter stream: It is an input-output class which filters or manipulates its stream of input- or output-data in some manner. Two illustrations are: DataInputStream and DataOutputStream.

  • Q : What is an Anonymous class Anonymous

    Anonymous class: It is a class formed without a class name. Such a class will be a sub class or an implementation of an interface, and is generally formed as an actual argument or returned as a method outcome. For example:

    Discover Q & A

    Leading Solution Library
    Avail More Than 1422561 Solved problems, classrooms assignments, textbook's solutions, for quick Downloads
    No hassle, Instant Access
    Start Discovering

    18,76,764

    1934515
    Asked

    3,689

    Active Tutors

    1422561

    Questions
    Answered

    Start Excelling in your courses, Ask an Expert and get answers for your homework and assignments!!

    Submit Assignment

    ©TutorsGlobe All rights reserved 2022-2023.