Home >>C++ Tutorial >C++ Interface
Interfaces in C++ are basically the abstract classes that are used to achieve the abstraction in C++. It can be described as the process that is used to show the functionality only and hide the internal details. Generally there are two methods in which abstraction can be achieved:
The above mentioned methods can possess the abstract methods that are necessary for abstraction.
A class in C++ is made to be abstract just by declaring one of its functions at least as <>strong> pure virtual function. A pure virtual function is generally specified by placing “=0” in its declaration. The derived class should have the implementation delivered by them.
Here is an example of the abstract class in C++ that consists of one abstract method and the implementation has been delivered by the classes, this example will help you understand the physical aspect of the abstract class:
#includeusing namespace std; //Parent class class Shapes { public: virtual int Area() = 0; void setWid(int x) { width = x; } void setHei(int y) { height = y; } protected: int width; int height; }; // Child classes class Rect: public Shapes { public: int Area() { return (width * height); } }; class Tri: public Shapes { public: int Area() { return (width * height)/2; } }; int main(void) { Rect obj; Tri obj1; obj.setWid(10); obj.setHei(15); cout << "Total Rect area = " << obj.Area() << endl; obj1.setWid(10); obj1.setHei(15); // Print the area of the object. cout << "Total Trian area = " < Output : Total Rect area = 150 Total Trian area = 75