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 is coupling in oop?

795


Write a program to sort the number with different sorts in one program ??

2132


What is byval and byref? What are differences between them?

1974


What is destructor oops?

872


What is stream in oop?

1063


What is polymorphism and why is it important?

807


can we make game by using c

3759


What is a null tree?

890


write a program that takes input in digits and display the result in words from 1 to 1000

2209


What is encapsulation and abstraction? How are they implemented in C++?

886


design a c++ class for the chess board,provide a c++ class definition for such class(only class definition is required)

6405


Is this job good for future? can do this job post grduate student?

1936


What is polymorphism and example?

800


Can a destructor be called directly?

823


What is oops and its features?

856