Differences between abstract classes and interfaces in C ++

Source: Internet
Author: User

1. If A Class B inherits (extend) Class A in syntax, Class B is A Class A in semantics.
2. If a Class B implements (implement) interface I in syntax, Class B complies with the Protocol set forth in interface I.

The root cause of using abstract class is that people want to present different levels of abstraction in this way.
The essence of interface is a set of protocols. In the development of program design, people found that interfaces can be used to represent the abstraction of behaviors. However, this is only a method of interface Usage, not its essence.

Combining theory with practice is the best way to learn. But here, I just want to cite some negative materials that I have seen about the use of interfaces:

1. contains data members in the interface. this is almost certainly wrong, because the protocol is a standard and should not be related to specific implementations, nor cause any burden on specific implementations.
2. delete an interface in C ++. For example:

Copy codeThe Code is as follows: class IInterface ()
{
Public:
Virtual ~ IInterface (){};
...
}

Class ClassImpl: public IInterface
{
...
}

Int main ()
{
IInterface * pInterface = new ClassImpl ();
...
Delete pInterface;
}

From the perspective of syntax and language, this is feasible. You only need to set the interface destructor to virtual to avoid Memory leakage. but I would like to say that this is not a syntax or language problem, but a fundamental mistake. because interfaces are a set of protocols and a set of specifications, they are not implemented. delete the code of an interface. What kind of semantics do you want to express? If a piece of code cannot be semantically stated, it should not appear in the program.
One way to express the interface concept in C ++ is as follows:Copy codeThe Code is as follows: class IInterface
{
Public:
Virtual void DoSomething () = 0;
}

// There should be no destructor, because in terms of semantics, interfaces cannot be deleted.

To delete a class, you can only delete instances of the same class:Copy codeThe Code is as follows: Class
{
Public:
Virtual ~ A ();

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

We can do this, because pA corresponds to an instance, And we can destroy it at Layer.
Let's give an example to help you understand it, And then abstract and summarize the theory from the example.

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

Abstract class template: This template should contain the common attributes (such as the shape and color of the door) and common behaviors (such as opening and closing) of all doors ).

Interface Template: Some doors may need functions such as alarm and fingerprint recognition, but these functions are not mandatory for all doors, so such behavior should be placed in a separate interface.

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

Abstract classes are used to abstract objects with similar properties and behaviors in nature. Interfaces are used to abstract the standards and norms of behavior, and are used to tell the interface implementers to complete a function according to certain specifications.

This is my opinion. You are welcome to discuss this issue with me.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.