Constructor of a derived class with sub-objects

Source: Internet
Author: User

This is what I just saw today. I felt that I didn't know much about this part of content before, so I want to share it with you. First, we need to introduce the concept of object at once. Data members of a class can be basic types such as int and char, or class objects. For example, they can contain such data members: [cpp] Student s1; // Student is a declared class. In this case, s1 is an embedded object in the class object, that is, a sub-object, which is similar to a struct variable. Let's review the syntax of the simple derived class constructor: [cpp] class student {public: student (int n, string nam, char s) {num = n; name = nam; sex = s ;}~ Student () {} protected: int num; string name; char sex;}; class student1: public student {public: student1 (int n, string nam, char s, int, string ad): student (n, nam, s) {age = a; addr = ad ;}~ Student1 () {} private: int age; string addr;}; the general format of the derived class constructor is the name of the derived class (total parameter list): Base Class Name (parameter list) {new data member initialization statement for the derived class;} How to Write the derived class constructor of the sub-object? We all know that the object should be initialized at the time of creation, but obviously the initialization of the sub-object cannot be performed when the derived class is declared, because the class is an abstract type and is only a model, no specific data. Therefore, the initialization of sub-objects is implemented by calling the constructor of the derived class when a derived class is created. Add a shift leader in the example just now. The type of the shift leader is obviously student, so it is a subobject. The constructor is as follows: [cpp] student1 (int n, string nam, char s, int n1, string nam1, char s1, int a, string ad): student (n, nam, s), monitor (n1, nam1, s1) {age = a; addr = ad;} There are eight parameters in the constructor above. The first three are parameters of the base class constructor, parameters 4th, 5, and 6 are the sub-object constructor parameters, and the last two are used for data member initialization of the derived class. To sum up, the constructor format is as follows: name of the derived class Constructor (total parameter list): name of the base class Constructor (parameter list) and sub-Object Name (parameter list) {new data member initialization statement in the derived class}

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.