In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to understand the C++ abstract base class, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
Let's talk about the abstract base class (abstract base class referred to as ABC).
As we said before, when we implement inheritance, we need to ensure that there is an is-a relationship between the derived class and the base class. Most of the time, there is no problem with such a relationship, but problems may be encountered in some special situations.
For example, suppose we want to implement all the graphics. In the figure, the circle is a special kind of ellipse. But the ellipse contains more attributes. In addition to the central point, the ellipse also has semi-major axis, semi-minor axis, and bearing, while the circle only needs the center and radius.
That is to say, although a circle is an ellipse, it contains fewer attributes, not more. This leads to the problem that when we use inheritance, circles do not need information such as semi-major axis, semi-minor axis, and bearing. And some methods will have some differences in logic, such as rotation rotate method, for example, there is no need to rotate for a circle, such as zooming in and out, the logic of a circle is different from that of an ellipse.
Although we have some ways to solve this, in general, it is easier not to use inheritance in this case. But not using inheritance will cause the same logic for both circles and ellipses to be copied and pasted, which is not very appropriate.
In view of this situation, there is another solution in C++, that is, to abstract the commonness from the circle and ellipse classes and put these commonalities into an ABC. However, Circle and Ellipse classes are derived from this ABC, so that we can use an array of base class pointers to manage both Circle and Ellipse objects, that is, we can implement polymorphism.
For example, what circles and ellipses have in common is that they both have central coordinates, the Move method is the same, and both have Area methods. But the logic of circle and ellipse is different, but the name of the method is the same. So we can't implement the Area method in ABC, we can set it to a pure virtual function.
A pure virtual function represents a function that is not implemented, and it is declared as = 0 at the end, as in the following example:
Class BaseElllipse {private: double x; double y;... Public: BaseEllipse (double x0, double y0): X (x0), y (y0) {} virtual ~ BaseEllipse () {} void Move (double nx, ny) {x = nx; y = ny;} virtual double Area () const=0;...}
When a class declaration contains a pure virtual function, we cannot create an object for that class. That is, a class that contains a pure virtual function can only be used as a base class. To become an ABC, the class needs to contain at least one pure virtual function.
In this example, our Area method is not defined, but C++ allows pure virtual functions to be defined. For example, suppose we want to set Move to a pure virtual function
We can declare that:
Void Move (double nx, ny) = 0
You can also create an implementation for the Move function:
Void BaseEllipse::Move (double nx, ny) {x = nx; y = ny;}
In short, assigning the function to 0 in the prototype indicates that the class is an abstract base class, and the function may not be defined in the class. When using it, it is important to note that we cannot create a BaseEllipse object, only a subclass object based on it.
In the subclass, we need to use the regular virtual function to implement the pure virtual function in the parent class.
The above is how the C++ abstract base class is understood. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.