The construction and destruction of "The tenth class of C + +"---inheritance

Source: Internet
Author: User

I. The assignment compatibility principle in inheritance

1. A subclass object can be used as a parent class object

2. Subclass objects can be assigned directly to the parent class object

3. Subclass object can initialize parent class object directly

4. The parent pointer can point directly to the child class object

5. Parent class reference can directly reference subclass object

6. Subclasses are special parent classes.

To illustrate:

#include <iostream>using namespace Std;class parent{       protected:              const char* name;       Public:              Parent ()              {                     name= "parent ...";                    }              void print ()              {                     cout<< "name=" <<name<<endl;              }}; Class Child:public parent{       protected:              int i;       Public: Child              (int. i)              {                     name= "child ...";                                   I= i;              }             }; int main (int argc, char** argv) {       parentpobj;       Child  CObj (1);       pobj= CObj;       A subclass object can be directly assigned to a parent object       parent*pp = &Cobj;//The parent pointer can point directly to the subclass object       parent&p_p = CObj;//parent class reference can directly refer to the subclass object             Parentppobj = Cobj;return 0; }


Ii. Models of inherited objects

1. Classes can be understood as structs within the C + + compiler

2. Subclasses are the new members of the parent class that are superimposed on the child class.

Introduction of the problem:

1. How do I initialize a parent class member?

2. What is the relationship between the parent class and the constructor of the subclass?

Answer:

1. When a subclass object is constructed, it needs to call the parent class constructor to initialize its inherited members.

2. When a subclass object is refactored, it needs to call the parent class destructor to clean up its inherited members

An example:

#include <iostream> using namespace std; Class parent{public       :              parent ()              {                     cout<< "parent () ..." <<endl;                }              ~parent ()              {                     cout<< "~parent () ..." <<endl;              }; Class Child:public parent{public   : Child              ()              {                     cout<< ' child () ... "<<endl;                 }              ~child ()              {                     cout<< "~child () ..." <<endl;               }           ; void Func () {       child  cobj;   } int main (int argc, char** argv) {   func ();       Cin.get ();       Return0; }


Printing results:

To sum up is:

1. When the subclass object is created, it calls the constructor of the parent class first

2. After the parent class constructor finishes executing, the constructor of the subclass is executed

3. When the constructor of the parent class has parameters, the call must be displayed in the initialization list of the subclass

4. The sequence of destructor calls is reversed from the constructor function

Now that we mention the initialization list, let's take a look at an example of an initialization list:

Iii. How to use the initialization list

#include <iostream> using namespace std; Class parent{public       :              parent (const char* s)              {                     cout<< "parent () ..." <<endl;                       cout<<s<<endl;                   }              ~parent ()              {                     cout<< "~parent () ..." <<endl;              }; Class Child:public parent{public   : Child              ():P arent ("Para fromchild")              {                     cout<< ' child () ... "<<endl;                 }              ~child ()              {                     cout<< "~child () ..." <<endl;               }           ; void Fun () {       child  cobj;       } int main (int argc, char** argv) {fun   ();       Cin.get ();       Return0; }


A member variable in a class can be an object of another class.

So here's the question:

If a class inherits from the parent class and has other objects as members, how does the constructor call?

Formula: First parents, after the guests, and then themselves.

Iv. member variables of the same name

What happens when a member variable defined in a subclass has the same name as a member variable in the parent class?

1. When the child class member variable has the same name as the parent class member variable

2. Subclasses still inherit members of the same name from the parent class

3. In a subclass by the scope of the separate character:: The same name is distinguished by the member

4. A member of the same name is stored in a different location in memory

V. Examples of succession depth

#include <cstdlib> #include <iostream>using namespace Std;class object{public:   Object (const char* s)    {       cout<< "Object () ..." << "" <<s<<endl;    }}; class Parent:public object{public:   Parent (const char* s): Object (s)    {       cout<< "Parent () ..." << "" <<s<<endl;    }}; Class Child:public parent{protected:   Object O1;   Object o2;public: Child   (): O2 ("O2"), O1 ("O1"), Parent ("Parameterfrom child!...")    {        cout<< ' Child () ... "<<endl;    }; void Fun () {child child   ;} int Main (int. argc, Char *argv[]) {fun   ();   Cin.get ();   return 0;}


Print content:

The basis of the formula is: first parents, after the guests, and then themselves.

Vi. Summary

1. A subclass object can be used as a parent class object

2. The subclass object needs to be initialized when it is created by calling the parent class constructor.

3. Subclass objects need to call the parent class destructor for cleanup when destroying

4. Execute the parent class constructor before executing the member constructor

5. The sequence of destruction in inheritance is opposite to the construction order symmetry

6. Members with the same name are differentiated by scope resolution

The construction and destruction of "The tenth class of C + +"---inheritance

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.