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

Can we create object of abstract class?

661


i=20;k=0; for(j=1;k-i;k+=j<10?4:3) { cout<

1526


What are the important components of cohesion?

640


What is overloading in oop?

653


What is difference between class and object with example?

674






What is advantage of inheritance?

778


What is polymorphism in oops?

637


What type of loop is a for loop?

777


Why do while loop is used?

660


What are different oops concepts?

670


Why can't we have instance(stack) of a class as a member of the same class like eg.Class A{A obj;} as we can have self refential pointer

1751


is there any choice in opting subjects like 4 out of 7

1822


Question In a class, there is a reference or pointer of an object of another class embedded, and the memory is either allocated or assigned to the new object created for this class. In the constructor, parameters are passed to initialize the data members and the embedded object reference to get inialized. What measures or design change should be advised for proper destruction and avioding memory leaks, getting pointers dangling for the embedded object memory allocation? Please suggest. Question Submitted By :: Sunil Kumar I also faced this Question!! Rank Answer Posted By Re: In a class, there is a reference or pointer of an object of another class embedded, and the memory is either allocated or assigned to the new object created for this class. In the constructor, parameters are passed to initialize the data members and the embedded object reference to get inialized. What measures or design change should be advised for proper destruction and avioding memory leaks, getting pointers dangling for the embedded object memory allocation? Please suggest. Answer # 1 use copy constructors 0 Shanthila There is something to be taken care in destructor, in copy constructor, suppose the memory is assigned to the embedded member object pointer with the parameter passed value, but if some other objects of different class also are pointing to this memory, then if some one deletes the object then this class member pointer object will become dangling, or if the object is not deleted properly then there will be memory leak. Please suggest the design change required to handle or avoid this situation

1745


What is the diamond problem in inheritance?

674


What does sksksk mean in text slang?

1688