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
What is coupling in oop?
Write a program to sort the number with different sorts in one program ??
What is byval and byref? What are differences between them?
What is destructor oops?
What is stream in oop?
What is polymorphism and why is it important?
can we make game by using c
What is a null tree?
write a program that takes input in digits and display the result in words from 1 to 1000
What is encapsulation and abstraction? How are they implemented in C++?
design a c++ class for the chess board,provide a c++ class definition for such class(only class definition is required)
Is this job good for future? can do this job post grduate student?
What is polymorphism and example?
Can a destructor be called directly?
What is oops and its features?