class, abstract base class, interface class The difference between the three and the connection (C + +)

Source: Internet
Author: User

Contact is obvious, three are ' class ', if the reader is not clear about the concept of the class, you can refer to Wid blog post http://www.cnblogs.com/mr-wid/archive/2013/02/18/2916309.html.

The following emphasis on explaining the difference, but this article is only a personal understanding, if you think I said the wrong place, please enlighten.

(1) Structural differences:

General class: Data + method + implementation

Abstract class: Data + method (must contain virtual method N>=1) + partial method implementation

Interface class: Method (pure virtual method)

(2) Conceptual differences:

The difference between the ordinary class and the other two is obvious, the ordinary class is the cat and the dog and so on, and the abstract class is the animal class. But the difference between the interface class and the abstract class is less obvious, so let's talk about these two classes.

The interface class is a special abstract class (pure virtual method and abstract class without data) separated from abstract class, the abstract class focuses on the ' class ' angle, while the interface class focuses on ' method '. In the words of "Big talk design Mode", "class is an abstraction of an object, an abstract class is an abstraction of a class, and an interface is an abstraction of the behavior." ”

For example, cats, dogs (ordinary concrete classes) inherit from the Animal class (abstract class), and cats, dogs and the common "eat" behavior, at this time, we can write a ' eat ' interface class, let the cat, dog class inherit and implement this method.

Abstract class is found from the subclass of the common thing to generalize out of the parent class, so that the child class inherits the parent class, but you do not necessarily know the existence of the subclass when writing the interface class, but like ' eat ' like, must be, just the specific sub-class how to realize what the sub-class is, the cat eat fish, dog chew bones The method of eating ' is implemented in subclasses.

Look at the "Big Talk Design Model" when summed up, if not understand, you can go straight to see the book Appendix A.

class, abstract base class, interface class The difference between the three and the connection (C + +)

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.