Advantages of a C ++ interface?

Look at this code

#include<iostream> using namespace std; //Shape is an Interface Class. No data and everything pure virtual class Shape { public: virtual void Area(int length, int breadth) = 0; virtual void Perimeter(int length, int breadth) = 0; //Note, no data }; //Derived class - Inherits Shape as Public class Rectangle : public Shape { public: void Area(int length, int breadth); void Perimeter(int length, int breadth); private: int someData; }; //Derived class - Inherits Shape as Public class Triangle : public Shape { public: void Area(int length, int breadth); void Perimeter(int length, int breadth); private: int someData; }; int main() { Rectangle r; Triangle t; cout<<"\n\n"; r.Area(3,4); r.Perimeter(3,4); t.Area(3,4); t.Perimeter(3,4); cout<<"\n\n"; return 0; } void Rectangle::Area(int length, int breadth) { cout<<"\nThe Area of Rectangle for length = "<<length<<" and\ breadth = "<<breadth<<" is "<<(length * breadth)<<endl; } void Rectangle::Perimeter(int length, int breadth) { cout<<"\nThe Perimeter of Rectangle for length = "<<length<<" and\ breadth = "<<breadth<<" is "<<2 * (length + breadth)<<endl; } void Triangle::Area(int length, int breadth) { cout<<"\nThe Area of Triangle for length = "<<length<<" and\ breadth = "<<breadth<<" is "<<(length * breadth)/2<<endl; } void Triangle::Perimeter(int length, int breadth) { cout<<"\nThe Perimeter of Triangle for length = "<<length<<" and\ breadth = "<<breadth<<" is "<<(length * breadth)/3<<endl; } 

I use the interface in the code, but my question is that I should use it, and what are the advantages of it, there is no performance, the real one is not necessary, why should I use it (interfaces). what's the point of using it, please explain.

and thanks!

+4
source share
5 answers

Abstract interfaces separate the interface from the implementation. Just like the idiom pimpl it

  • reduces compilation time and
  • allows you to change the implementation without violating the ABI.

Both are important benefits in large programs.

+5
source

The interface can be used, for example, for a vector of various Shape objects. Otherwise, you cannot have a collection that mixes triangles and rectangles, for example. Or another class can have a Shape element, which can be either a triangle or a rectangle. These are just some examples.

Edit:

Let me give you a concrete example. Say you have an interface called Car. Imagine you want to have a Garage class that has room for one car. You have implemented different types of cars, and all of them use the Car interface. Then the Garage class might be something like this:

 class Garage { public: Car getCar(); // returns _theCar private: Car _theCar: } 
+1
source

A common mistake when programming C ++ (and other object-oriented languages) is the excessive use of inheritance. Interfaces are inherited correctly. The reason is that the power of interfaces is to process objects of a different type in another system, as if they were of the same type. Triangle and Circle can be both Shapes, for example, and can be transferred to the graphics engine for drawing on the screen.

The reason interfaces are “better” than inheritance, which also includes inherited functionality, because it quickly becomes very difficult to understand what the class really does to debug it and make sure that the internal state of objects cannot be destroyed using external methods .

The need for this type of structure, where you use interfaces more than sporadically, is difficult to motivate in a small example, but it becomes apparent when projects become large. In addition to creating things like I describe above, they are also good for making it easier to test the program, because instead you can replace the implementation of part of your program (say, accessing the database for instace) with a shaded implementation, and thus allow you to write automatic tests that check other parts of the program (for example, data processing)

There is no reason to choose an interface for direct access to members, and not vice versa, since you are calling methods that are virtual. This, however, is a very slight decrease in performance in most cases.

+1
source

Look here to learn more about C ++ MI - Why should I avoid multiple inheritance in C ++? .

Tune in to the “3 interfaces” section and ybungalobill answer, consider a typical Observer pattern:

 class MyClass : public IScreenListener { public: MyClass() { PowerManager::RegisterScreenListener(*this); } // Overriding from IScreenListener void OnScreenOn() { // do as you like } void OnScreenOff() { // do as you like } } 

Here, the IScreenListener interface provides two pure virtual methods, OnScreenOff and OnScreenOn, that should be implemented in your code. This example is based on the Bada screen listener: it allows you to receive notifications of such events.

Of course, there are other benefits. How to hide code library implementation details from your users, etc.

0
source

Interfaces (pure abstract classes) provide common functionality. In your example, the Shape class is generic. This means that you cannot have the actual instance (or object) from the Shape class. Where, as you say, a rectangle is a shape or a triangle is a shape. You cannot calculate the area or perimeter of a shape unless you know what the shape is.

Interfaces (Pure Abstract Classes) enforce the protocol that a class derived from it must implement all of its methods. Otherwise, it also becomes an interface. Interface pointers can be sent to functions or other classes, and from there you can call the functional functions of the derived classes.

For example, if there is a class called Animal from which you will get all the animals, such as dogs, snakes, people, etc., you can send an array of pointers to animals (which are actually instances of its derived classes), and then call functionality like Run (), Walk (), Eat (), etc. The animal in this case is a general class.

0
source

Source: https://habr.com/ru/post/1385644/


All Articles