GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 9 of 203    Print  
What is polymorphism? Explain with an example?
 
"Poly" means "many" and "morph" means "form". Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of object.
Example: function overloading, function overriding, virtual functions.
Another example can be a plus ‘+’ sign, used for adding two integers or for using it to concatenate two strings.
 


  
Total Answers and Comments: 4 Last Update: September 16, 2008   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Zaid
 
An example would be: 
A "vehicle" class can be a part of a inheritance heirachy where derived classes are "sedan" and "SUV". Derived from "sedan" could be "Corolla", and derived from "SUV" could be "Pathfinder". 
 
CAR 
/  
SUV SEDAN 
/  
PATHFINDER COROLLA 
 
 
Now, using such an example, it is true that any object below in a heirarchy is also something that is directly up in the heirarchy. Hence, Pathfinder "is a" SUV, and SUV "is a" CAR. Also, Pathfinder "is a" CAR. 
Hence, using pointers of Base classes (higher in an inheritance heirarchy) can be assigned to objects of derived classes and can be used in a unified manner with the use of virtual functions. Hence, Polymorphism. 
 
(The plus "+" operator example used above would not be correct, as that is actually an overloaded operator (in the case the last poster presumed) and not precisely polymorphism).

Above answer was rated as good by the following members:
anellona
March 27, 2005 10:19:42   #1  
Zaid        

RE: What is polymorphism? Explain with an example?  
An example would be:
A vehicle class can be a part of a inheritance heirachy where derived classes are sedan and SUV . Derived from sedan could be Corolla and derived from SUV could be Pathfinder .

CAR
/
SUV SEDAN
/
PATHFINDER COROLLA


Now using such an example it is true that any object below in a heirarchy is also something that is directly up in the heirarchy. Hence Pathfinder is a SUV and SUV is a CAR. Also Pathfinder is a CAR.
Hence using pointers of Base classes (higher in an inheritance heirarchy) can be assigned to objects of derived classes and can be used in a unified manner with the use of virtual functions. Hence Polymorphism.

(The plus + operator example used above would not be correct as that is actually an overloaded operator (in the case the last poster presumed) and not precisely polymorphism).

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
February 16, 2006 12:12:59   #2  
sanneboyina        

RE: What is polymorphism? Explain with an e...
A
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
April 15, 2006 05:14:50   #3  
nitin_chawda Member Since: April 2006   Contribution: 4    

RE: What is polymorphism? Explain with an e...
Polymorphism is acquiring many forms . as it has been stated that c++ has the property of polymorphism.Like as someone said the eg of vehicle...it can be said that the particular operation can acquire one or many forms
 
Is this answer useful? Yes | No
September 16, 2008 07:33:46   #4  
vsridevi Member Since: September 2008   Contribution: 1    

RE: What is polymorphism? Explain with an example?  
C++ enables polymorphism - The ability of obejcts of different classes related by inheritance respond differntly to the same function call.
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape