An introduction to abstract classes and interfaces in C++

  • 2020-04-01 21:35:59
  • OfStack

1. If A class B extends class A syntactically, then semantically class B is A class A.
2. If a class B grammatically implements interface I, class B follows the protocol set forth by interface I.

The fundamental reason for using abstract class is that people want to represent different levels of abstraction in this way.
The essence of interface is a set of protocols. In the development of programming, people found that interface can be used to express the abstract of behavior, but this is just a use of interface, not its essence.

The best way to learn is to combine theory with practice, but here are some examples of how I've seen interfaces used against me:

1. Include data members in the interface. This is almost certainly wrong, since the protocol is a specification and a standard, and should not have any implications for the implementation, nor should it impose any burden on the implementation.
2. Delete an interface in C++.


class IInterface() 
{ 
Public: 
Virtual ~IInterface(){}; 
 ...  
} 
Class ClassImpl : public IInterface 
{ 
 ...  
} 
Int main() 
{ 
IInterface* pInterface = new ClassImpl(); 
 ...  
delete pInterface; 
} 

From the point of view of the perspective of syntax and language itself, it is feasible, and as long as the interface of the destructor is set to the virtual, can avoid memory leaks. But I want to say, this is not a grammar and language problems, but is fundamentally wrong. Because the interface is a set of protocols, a set of specifications, not implementation. Delete an interface code, exactly want what kind of semantic expression? If a piece of code doesn't make sense semantically, it shouldn't be in the program.
One way to represent the concept of an interface in C++ is to:

class IInterface 
{ 
public: 
virtual void DoSomething() = 0; 
} 
//There should be no destructors, because semantically, the interface cannot be deleted.

If you want to delete, you can delete only one instance of the class:

Class A 
{ 
Public: 
Virtual ~A(); 
Public: 
Virtual void DoSomething() = 0; 
} 
Class B : public A 
{ 
 ...  
} 
Int main() 
{ 
A* pA = new B(); 
 ...  
Delete pA; 
} 

We can do that, because pA corresponds to an instance, and we can destroy it at A level.
Let me give you an example so that you can understand it, and then I will abstract the conclusion from the example.

For example, a door company needs to define a door template in order to quickly produce doors of various specifications.
There are usually two types of templates: abstract class templates and interface templates.

Abstract class template: this template should contain common properties (such as the shape and color of the door) and common behaviors (such as opening and closing) that all doors should have.

Interface templates: some doors may require features like alarm and fingerprint recognition, but these are not required for all doors, so actions like this should be kept in a separate interface.

With the above two types of templates, it will be convenient to produce doors in the future: using the abstract class template and the interface template that contains the alarm function, you can produce doors with the alarm function. Similarly, using the abstract class template and the interface template that contains the fingerprint recognition capability can produce a door with the fingerprint recognition capability.

In summary: abstract classes are used to abstract objects from nature that have similar properties and behaviors. The standards and specifications that interfaces are used to abstract behavior tell implementers of interfaces that certain specifications are necessary to perform certain functions.

This is my own view and I welcome you to discuss this issue with me.


Related articles: