Updated April 3, 2023
Introduction to C++ interface
C++ interface is defined as a way to describe the behavior of a class without taking the implementation of that class or in layman terms; we say that the C++ interface is a pure virtual function. An interface or abstract class is the same. The ability of an interface is they inherit functions from any base interface provided they are public member functions. All interface is abstract, so we cannot create an instance of an Object.
Syntax
A good style of interface is given as:
Interface IAddition
{
void display ();
void Add ();
}
Using Abstract class in C++
class Interface name
{
public:
virtual type function name () =0;
virtual type function name(type)=0;
~Interface name ();
}
An interface contains only public functions along with the constructor definition. For example, a pure Virtual function is defined with a keyword virtual and has =0.
How interface work in C++?
With the help of Abstract classes (simulating them), we can implement interfaces in C++, so they are named an abstract base. That interface uses Classes and Objects, and they don’t contain any methods or variables; in need of these to the code, they must be declared by implementing classes. Any class that contains an interface is assumed to be only a pure virtual function and no other members. So, the keyword we will use throughout this article is a method and a function.
- method (): It performs the routine task in the program.
- function (): This function specified in the code may or may not be a member of a class.
Working Rule
I’m going to take away three points here
- The interface can only be declared cannot be defined.
- We can create a pointer referencing the base abstract class.
- The instance of a class cannot be done.
Interface class is purely a definition part, and no implementation is provided in that class. Interfaces are important when we need to define the functionality as a derived class must do implement, but neglecting how the derived class does the implementation. Let’s see the sample Interface class part to go with.
class ITCP
{
public:
virtual bool openConnection(const char *filename) = 0;
virtual bool closeConnection() = 0;
virtual bool readMessage(const char *read) = 0;
virtual ~ITCP() {}
In the above code, we have used an interface ITCP with three definition functions, any class that inherits the interface should provide implementations of all the three functions declared. Thus, we can read the content once the connection is established. Let us understand this with a few examples in the next section.
Examples of C++ Interface
Given below are the examples of C++ Interface:
Example #1
Simple Example with Class Abstract
Code:
#include <iostream>
using namespace std;
class Geo
{
public:
virtual void angle()=0;
};
class Acute: Geo
{
public:
void angle ()
{
cout <<"making it less than 90 degree" <<endl;
}
};
class Right: Geo
{
public:
void angle ()
{
cout <<"Making exactly 90 degree" <<endl;
}
};
int main () {
Acute r;
Right rir;
r.angle();
rir.angle();
return 0;
}
Explanation:
This example has one abstract method angle (). This method’s implementation is specified in the class Acute and right, which has different implementation processes. Therefore the output is shown as:
Output:
Example #2
Taking Structure concept, Let’s illustrate with the interface concept
Code:
#include <iostream>
using namespace std;
struct A{
virtual void indemo() = 0;
};
struct B: public A
{
void indemo()
{ std::cout << "Hi welcome";
};
};
struct D: public B
{
void indemo()
{
std::cout <<"This is my home Page";
};
};
main() {
D de;
B& x = de;
A& y = x;
y.indemo();
};
Explanation:
Let’s consider a class A, B D that provides methods, and the first initiate would be to use a public interface in the method implementation.
Output:
Example #3
With constructors
Code:
#include<iostream>
using namespace std;
class store
{
protected:
int m;
public:
virtual void item() = 0;
store(int a) { m = a; }
};
class sub: public store
{
int n;
public:
sub(int a, int b):store(a)
{ n = b; }
void item() { cout << "m = " << m << ", n = " << n; }
};
int main(void)
{
sub e(6, 8);
e.item();
return 0;
}
Explanation:
Using constructors and their automatically created objects, we have got the result like:
Output:
Example #4
Code:
#include <iostream>
struct Apple {
virtual ~Apple() {};
virtual float ae(float) = 0;
};
struct Bag {
virtual ~Bag() {};
virtual float be(float) = 0;
};
struct Call {
~Call() {}
float ce;
virtual float getc(float) { return ce; }
};
struct Div : public Apple {
~Div() {}
float de;
float ae(float) { return de; }
};
struct Eat : public Apple, public Bag{
~Eat() {}
float ef;
float ae(float) { return ef; }
float be(float) { return ef; }
};
int main() {
Eat ef; Div de; Call ce;
std::cout << "A : " << sizeof(Apple) << "\n";
std::cout << "B : " << sizeof(Bag) << "\n";
std::cout << "C : " << sizeof(Call) << "\n";
std::cout << "D : " << sizeof(Div) << "\n";
std::cout << "E : " << sizeof(Eat) << "\n";
}
Explanation:
In the above code, we have used four Interfaces to Determine the Value of the type declared under the function. Class Call is not an interface but has a virtual function to go with.
Output:
Example #5
Using Virtual function of Interface with Pointers
Code:
#include<iostream>
using namespace std;
class Super
{
public:
virtual void view() = 0;
};
class Sub: public Super
{
public:
void view() { cout << "This is in Sub-class \n"; }
};
int main(void)
{
Super *sp = new Sub();
sp->view();
return 0;
}
Explanation:
The code works well by having pointers and their reference to the class of Interface we created.
Output:
Conclusion
But interface classes have become widely benefitted as they are easy to use, and many new technologies making use of these interface classes for not making an abstract function. The interface keyword is much used in Java and C# programming. So, to Summarize in this article, we have seen How interface works alternately as Abstract in C++ with their working and examples.
Recommended Articles
This is a guide to C++ Interface. Here we discuss How the interface works in C++ with Working and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –