Interfaces in C++
Interfaces
On this page we will discuss about Interfaces in C++. It describes the behavior of a class without actually disclosing the implementation(code) of that class. For eg. In an ATM machine, you will be shown the list of options provided by that Bank that is withdrawal, deposit but not the implementation of back-end code of those servicesInterfaces in C++
- Sometimes you need not disclose all the details(implementation) of your project to the user i.e just need to present him the set of services in the form of options in an interface.
- For example, in an ATM machine, you will be shown the list of options provided by that Bank that is withdrawal, deposit but not the implementation of backend code of those services
- This
Hiding of implememntataion details
wherever required is called as an interface
Syntax
Interfaces are implemented using abstract classes, a class is made abstract by declaring at least one of its functions as a pure virtual function, the pure virtual function must be declared under public access
class class_name
{
public:
// pure virtual function
virtual return-type func_name() = 0;
};
A pure virtual function is specified by using (=0 ) in its declaration
Example program demonstrating interfaces in C++
Consider the following example where parent class provides an interface to the child class to implement a function called getArea()
#include<iostream>
using namespace std;
// parent class
class Shape
{
public:
// pure virtual function providing interface framework.
virtual int getArea() = 0;
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// child classes for implementation
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
// child classes for implementation
class Triangle: public Shape {
public:
int getArea() {
return (width * height)/2;
}
};
int main()
{
Rectangle Rect;
Triangle Tri;
Rect.setWidth();
Rect.setHeight(4);
// display the area of the object.
cout << "Total Rectangle area: " << Rect.getArea() << endl;
Tri.setWidth(8);
Tri.setHeight(4);
// Print the area of the object.
cout << "Total Triangle area: " << Tri.getArea() << endl;
return 0;
}
0/P
Total Rectangle area: 32
Total Triangle area: 16
You can see how an abstract class defined as an interface in terms of getArea() and two other classes implemented the same function but with a different set of code to calculate the area specific to the shape.
Point to remember about abstract classes
- An abstract class cannot be used and not allowed to create objects, just it is used as an interface if you try to create an object of an abstract class that it was a compile time error
- A child class that is implementing a parent abstract class must define all the pure virtual functions of parent class in the child then only child class can be allowed to create objects and use them
Why interfaces
- In OOP’s design system an abstract base class will provide a common generalized interface that is appropriate and can be used for all external applications of similar type
- Interface mechanism allows applications to add new software updates and other changes to the existing system easily even after a software system has been defined
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment