Introduction to the difference between abstract classes and interfaces in C + + _c language

Source: Internet
Author: User

1. If a class B is syntactically inherited (extend) Class A, then in semantics Class B is a class A.
2. If a class B implements the (implement) interface I in syntax, then Class B complies with the protocol established by interface I.

The fundamental reason for using abstract class is that people want to represent different levels of abstraction in this way.
and the essence of interface is a set of agreements. In the development of programming, it is found that interfaces can be used to express the abstraction of behavior, but this is only one of the usages of interface is not its essence.

Combining theory with reality is the best way to learn, but here, I just want to cite some of the negative examples I've seen about interface use:

1. Include data members in the interface. This is almost certainly wrong, because the protocol is the norm, and should not be involved in any specific implementation, nor should it cause any burden to the specific implementation.
2. Delete an interface in C + +. For example:

Copy Code code as follows:

Class IInterface ()
{
Public:
Virtual ~iinterface () {};
...
}

Class Classimpl:public IInterface
{
...
}

Int Main ()
{
iinterface* pinterface = new Classimpl ();
...
Delete Pinterface;
}


This is feasible from the perspective of syntax and language itself, and can avoid memory leaks as long as the destructor of the interface is set to virtual. But I would say that this is not a question of grammar and language, but fundamentally wrong. Because interfaces are a set of protocols, a set of specifications, not implementations. Delete the code of an interface, what kind of semantics do you want to express? If a piece of code is semantically incorrect, it should not appear in the program.
To represent the concept of interfaces in C + +, one approach is this:
Copy Code code as follows:

Class IInterface
{
Public
virtual void dosomething () = 0;
}

There should be no destructors, because semantically, interfaces cannot be delete.


If you want to delete, you can delete only one instance of a class:
Copy Code code as follows:

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 this because the PA corresponds to an instance where we can destroy it at the layer a.
Let's take an example to make it easier for everyone to understand and then abstract the knot theory from the example.

For example, a company that manufactures doors needs to first define the template of the door so that it can quickly produce doors of all sizes.
Templates here typically have two types of templates: abstract class templates and interface templates.

Abstract class Template: This template should contain all the common attributes (such as the shape and color of doors) and common behaviors (e.g., opening and closing) that all doors should have.

Interface templates: Some doors may need to have features such as alarms and fingerprint recognition, but these functions are not required for all doors, so behavior like this should be in a separate interface.

With the above two types of templates, after the production of the door is very convenient: the use of abstract class templates and include the alarm function of the interface template can be produced with the alarm function of the door. In the same vein, the use of abstract class templates and interface templates that contain fingerprint recognition capabilities can produce a door with fingerprint recognition.

In short: Abstract classes are used to abstract natural objects of similar nature and behavior. Interfaces are used to abstract the behavior of standards and norms, to tell the interface of the implementation of the need to follow some kind of specification to complete a function.

This is my own opinion, welcome to discuss this problem with me.

Related Article

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.