What are the different forms of polymorphism??

Answer Posted / amit

There are two types of polymorphism:-
1.Compile time polymorphism
This is achieved by:
- Function overloading
- Operator overloading
2.Run time polymorphism
This is achieved through inheritance and virtual functions.
In this, base class has one or more virtual functions which
are overridden in the derived class. And then base class
pointer is used to access base or derived class virtual
function.

Example:-

class base {
public:
virtual void func() {
cout << "In base class" << endl;
}
};

class derived {
public:
void func() {
cout << "In derived class" << endl;
}
};

int main(){
base *bp, b;
derived d;

bp = &b;
bp->func(); // base class func() will be called

bp = &d;
bp->func(); // derived class func() will be called
return 0;
}

Here, the decision to call base or derived class func() is
taken at run time.

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What does enum stand for?

870


What are classes oop?

795


What is the significance of classes in oop?

806


How to hide the base class functionality in Inheritance?

860


What is overloading in oop?

760


2. Give the different notations for the class.\

1811


What is destructor give example?

814


Can private class be inherited?

907


What is encapsulation in oop?

788


What is the difference between a constructor and a destructor?

855


What is abstraction and encapsulation?

777


What is overriding in oop?

793


What is object and example?

881


What is an example of genetic polymorphism?

873


to find out the minimum of two integer number of two different classes using friend function

1872