Difference Between Abstraction and Interface in C++
Abstraction
Abstraction provides users with a straightforward way to communicate with items by hiding their execution specifics. A computer scientist avoids all the configuration information which the end-user isn't obligated to know with the assistance of abstraction. Suppose you are operating a vehicle. Do you ever consider what occurs within the combustion chamber as you boost its speed? Do you also understand how the transmission functions when you change gears? If you require this information, it may be a more appropriate query.
This also is true of numerous daily activities. When we want to turn on our televisions, we click on an icon on the control panel, relying on it to function as intended and not caring how it's performing.
C++'s abstract class
A class in C++ is considered abstract if it has a minimum of a single pure abstract function and can't be independently created. The concept of abstraction gave rise to Abstract Classes. Imagine a situation where you could apply this idea before we go into the details.
A class explicitly intended to be utilized as an initial class is known as an abstract class. There is a minimum of one pure virtual method within an abstract class. In defining a virtual member's function within the class's affirmation, you designate a virtual function solely virtual with the help of the pure specifier (= 0).
Example Code:
#include <iostream>
using namespace std;
class AbstractionExample {
private: string x, y;
public:
void set(string a, string b) {
x = a;
y = b;
}
void print() {
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
};
int main() {
AbstractionExample A1;
A1.set("Abstract", "Example");
A1.print();
return 0;
}
Output:
![Difference Between Abstraction and Interface in C++](https://static.tutorialandexample.com/cpp/difference-between-abstraction-and-interface-in-cpp1.png)
Rules for Abstract Classes
In C++, there are several limitations on abstract classes. What follows may not be done using abstract classes:
- Member information or variables
- Types of arguments
- The types that the function returns
- explicit transformations in several forms.
In C++, the abstraction class constructors can invoke other member operations, but the outcome is uncertain when they either invoke or indirectly call a pure virtual function.
But hold on! What exactly is a pure virtual function? First, Let's examine virtual operations to comprehend the pure virtual function.
Any member function that is defined inside an initial class and overruled by an extension class is referred to as a virtual method. Now, one will be able to invoke a virtual function that exists for an object and carry out the derivative class's instance of that specific method whenever you access an object belonging to an extracted class implementing a pointer to the root class.
Why Is It Difficult to Create Abstract Class Objects?
The "abstract" nature of C++'s class structures prevents them from being implemented. Somebody told you to design a creature but needed to specify which one. Only if they ask you to sketch a cat, dog, or other animal since an abstract object is what you can paint. The same is true for abstract classes in C++. Therefore, we can only generate pointers for these classes rather than actual objects.
C++ interfaces
Occasionally it is sufficient to give the user a range of offerings in various interface alternatives rather than revealing all the technical aspects (details) of your endeavor.
For instance, in an automated teller machine, a list of alternatives supplied by the bank, such as withdrawal and deposit, will be displayed; however, the installation of the underlying code for those features won't be. This concealing of implementation information is called an interface when necessary.
Point to keep in mind with abstract classes
- If you attempt to utilize or construct an instance of an abstract class, a compilation time error will occur; instead, the abstract type is only utilized as an interface.
- When a child class carries out an original abstract class, the class it inherits from must declare all its parent class's pure virtual methods before the child class is permitted to establish and operate on objects.
Example Code:
#include <iostream>
class InterfaceExample {
public:
virtual void FirstFunction() = 0; // Pure virtual function
virtual int SecondFunction() = 0;
// Virtual destructor
virtual ~InterfaceExample() {}
};
class Derived : public InterfaceExample {
public:
void FirstFunction() override {
std::cout << "Derived::FirstFunction() called" << std::endl;
}
int SecondFunction() override {
std::cout << "Derived::SecondFunction() called" << std::endl;
return 42;
}
};
int main() {
InterfaceExample* obj = new Derived();
obj->FirstFunction();
int result = obj->SecondFunction();
delete obj;
return 0;
}
Output:
![Difference Between Abstraction and Interface in C++](https://static.tutorialandexample.com/cpp/difference-between-abstraction-and-interface-in-cpp2.png)
The purpose of interfaces
- An abstract base class in the OOP design framework will offer a single generalized interface suitable and usable for third-party use cases of the same type.
- Following the development of a software framework has been developed, interface mechanisms allow programs to quickly add fresh software upgrades alongside additional adjustments to the current system.
Difference between Abstraction and Interface
Abstraction
- implemented to offer overridable attributes and techniques for derivable classes.
- The technique may be applied to the creation of an interface.
- Generally expressed using the term abstract.
- It may involve both non-abstract and abstract procedures.
- An undefined class is capable of extending one other class.
- Members may be secret, protected, or public users.
Interface
- implemented to give classes contracts and behavior.
- Incapable of being utilized for implementing an abstract class.
- Often defined using a keyword-based interface.
- It may only include abstract techniques.
- The class can branch out from several interfaces.
- They usually, by default, make their employees public.