Polymorphism and virtue keyword

Posted on December 11, 2013. Filed under: C++ | Tags: |


The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. To apply the polymorphism principle to OOP, the language need to provide some way to allow parent class to contain the blue print of the specie and leave the implementation to subclasses.
In Java, polymorphism is supported by abstract methods/abstract class and interface. While abstract class allow partially implemented methods, the interface contains only method declarations. The implementation flexibility is left to the subclass. Unique to java, a class can implement multiple interfaces.
In C++, polymorphism is supported by virtue function/virtue class. C++ have no interface, however, C++ virtue keyword together with pointer, can act as interface.
Here is how the keyword virtue works:
When a function is prefixed with keyword virtue, the function refer to the function implementation in the subclass (if defined) intend of default implementation in parent class.
Virtue keyword is unique to C++, through pointer of the virtue function, parent class can access the function defined later in its subclasses (which is somehow the java interface try to accomplish.)
pure virtue function: a virtue function without default implementation.
virtue class: a class contains pure virtue function. virtue class can not be instantiated. When a virtue class contains only pure virtue functions, it is somehow the equivalent of java interface.
#include
using namespace std;

class Enemy {
public:
//virtual void attack()=0; //pure virtue function
virtual void attack() {
cout<<“enemy attack!”<<endl;
}
void defend() {
cout<<“ememy defend.”<<endl;
}

};

class Ninjia: public Enemy {
public:
void attack() {
cout << “ninja attack!” <<endl;
}
void defend(){
cout<<“ninjia defend.”<<endl;
}
int main () {
Ninjia n;
Enemy *enemy = &n //polymorphism, we can use the parent class’s pointer to reference many types of subclasses’ address
enemy.attack(); //due to “virtue” keyword, result is “ninja attack”
enemy.defend(); //normal function, the result is “enemy defend.”
n.defend(); //normal function, the result is “ninjia defend.”
}

Advertisements

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

2 Responses to “Polymorphism and virtue keyword”

RSS Feed for IT Certifications Comments RSS Feed

I visited multiple blogs however the audio quality
for audio songs existing at this site is really wonderful.

Hi there, its good post ɑbout media print, wе all
understand media is a fantastic source ߋf factѕ.


Where's The Comment Form?

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: