In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this "C++ class inheritance constructor instance analysis" article, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "C++ class inheritance constructor instance analysis" article.
Foreword:
The subclass needs to write its own constructor and destructor. It should be noted that the subclass is only responsible for initializing and sweeping the new members, while the initialization and destructor of the parent class is done by the constructor and destructor of the parent class.
Regardless of the type of inheritance, the subclass does not have access to all members of the parent class, so the initialization of the parent class by the subclass requires the constructor of the parent class to complete. At this point, the constructor of the subclass must provide the parameters required by the parent constructor.
The syntax of the subclass constructor is as follows:
Subclass: subclass (all parameter table): parent class 1 (parent class 1 parameter table), parent class 2 (parent class 2 parameter table)
... Object member 1 (object member 1 parameter table), object member 2 (object member 2 parameter table)
Among them, the "all parameters table" contains "parameters required by all parent classes" and "parameters required by new members of subclasses"; object members represent new object members of subclasses (objects of some external classes are members of subclasses).
It must be pointed out that the subclass first calls the constructor of the parent class, and then calls its own constructor; if the subclass contains multiple constructors, then the constructor of each parent class is called in the order in which the subclass inherits each parent class.
The following three classes are defined: X, Y, and Z, and then the Point class inherits them:
Class X {public: X (int value) {printf ("init X% d\ n", value);}}; class Y {public: y (int value) {printf ("init Y% d\ n", value);}}; class Z {public: Z () {printf ("init Z\ n");}} Class Point: public X, public Y, public z {public: Point (int value_x, int value_y, int value_point): X (value_x), Y (value_y) {printf ("init Point% d\ n", value_point);}}
As you can see, the constructor "all parameters table" of the Point class gives "parameters required by the parent class" and "parameters required by the members of this class" (in this case, the Point class does not have object members), and initializes each parent class as an initialization list. The following defines the Point class object
To see the calling order of the subclass and parent constructors:
Int main () {Point p (333,666,999);} / / get the following result: init X 333init Y 666init Zinit Point 999
It is proved again that because when Point declares, it inherits class X first and then class Y, so it first calls X constructor, then Y constructor, and finally Z class constructor.
In addition, when a parent class constructor does not need parameters, the subclass constructor can ignore the parent class, and the system will call the default constructor of the parent class (such as the Z class above). If a parent class contains both constructors that require arguments and constructors that do not require arguments, the programmer can decide which one to use.
First, the corresponding constructor is called in the order in which the parent class is declared.
Second, initialize the object members of the subclass in the order in which they are declared in the subclass
Finally, execute the subclass to construct the function body
The above is about "C++ class inheritance when the constructor instance analysis" of this article, I believe we all have a certain understanding, I hope the editor to share the content to help you, if you want to know more related knowledge, please 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.