CodesJava

Easy learning with example program codes

CPP Interfaces


Describing the behavior or capabilities of a class is the main purpose served by an interface in CPP. The CPP interface however, does not commit to any particular implementation of a class. To implement an interface in CPP, abstract classes are used. A abstract class is completely different from the concept of data abstraction. A class in CPP is said to be an abstract class if at least one of its function is declared as a pure virtual function (by placing “= 0” in its declaration).

Example:

#include <iostream.h>  
using namespace std;  
class Flower    
{    
public:   
virtual void color()=0;    
};    
class Lily : Flower	 
{    
public:  
void color()    
{    
cout << "White Lily!!" << endl;    
}    
};    
class Rose : Flower	 
{    
public:  
void color()    
{    
cout <<"Red Rose!!" << endl;    
}    
};    
int main( ) {  
Lily lly;  
Rose rse;  
lly.color();    
rse.color();   
return 0;  
}

Output

White Lily!!        
Red Rose!!
Please follow and like us:
Posted in CPP   


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki