Print Friendly and PDF
In object-oriented programming, polymorphism (from the Greek meaning "having multiple forms") is the characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form
In object-oriented programming, polymorphism (from the Greek meaning "having multiple forms") is the characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form

Program:

#include <iostream> 
using namespace std; 
class cBase //4 Bytes 
{ 
private: 
int baseVar; 
public: 
cBase() 
{ 
baseVar = 0; } 
virtual void Output(){cout<<"Base Class Method";}; 
}; 
class cDerived1 : public cBase //8 bytes 
{ 
private: 
int derVar; 
int derVar2; 
public: 
cDerived1 () 
{
derVar = 0; derVar2 = 0;
}; 
void Output() {cout<<"Ancestor1 Class called!";
}; 
}; 
class cDerived2 : public cBase 
{ 
public: 
void Output(){cout<<"Ancestor2 Class called!";
}; 
}; 
void TestPoly(cBase * TestObj) 
{ 
TestObj->Output(); 
} 
//now I'm declaring instances in main() 
int main() 
{ 
cDerived1 *obj1 = new cDerived1; 
cDerived2 *obj2 = new cDerived2; 
TestPoly(obj1); 
cout<<'\n'; 
TestPoly(obj2); 
cout<<'\n'; 
char a = cin.get(); 
return 0; 
}
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: