Summary of parent class, subclass, and inheritance

Source: Internet
Author: User
1. parent class and Child class 1. Mutual conversion between parent class and Child class

The object of the derived class can be assigned to the Base class, but not vice versa.

The pointer of the base class can point to the derived class, but not vice versa.
The base class can be initialized as the object of the derived class.

The pointer of a derived class must be forcibly converted to the base class pointer before it can be directed to the base class.

Converting a base class pointer to a derived class pointer may cause collapse errors.

The reference or derivation of a virtual base class cannot be converted to a derived class.
Class father {}; class son: Public father {}; int main () {father f; son s; F = s; // correct S = F; // error father * pF = new son; // correct son * PS = new father; // error father & Rf = s; // correct father & rs = F; // error return 0 ;}

2. Impact of inheritance relationships on base class members

  Public Member Protect members Private member
Public inheritance Public Protection Inaccessible
Protection inheritance Protection Protection Inaccessible
Private inheritance Private Private Inaccessible
Member Functions 1 1 1
Object 1 0 0
Subclass 1 1 0

1: accessible. 0: inaccessible.

When all the members become inaccessible, it does not make sense to derive further. 2. Subclass Construction and Analysis 1. when constructing a derived class object, first execute the constructor of the base class, and then execute the constructor of the subclass.
class father{public:father(){cout<<"father construct"<<endl;}~father(){cout<<"father delete"<<endl;}};class son : public father{public:son(){cout<<"son construct"<<endl;}~son(){cout<<"son delete"<<endl;}};int main(){son s;return 0;}

Output:

Father construct
Son construct
Son Delete
Father Delete

2. For multi-inheritance, the construction order of the base class is in the given order.

class father{public:father(){cout<<"father construct"<<endl;}~father(){cout<<"father delete"<<endl;}};class mother{public:mother(){cout<<"mother construct"<<endl;}~mother(){cout<<"mother delete"<<endl;}};class son : public father, public mother{public:son(){cout<<"son construct"<<endl;}~son(){cout<<"son delete"<<endl;}};int main(){son s;return 0;}

Output:

Father construct
Mother construct
Son construct
Son Delete
Mother Delete
Father Delete

3. Construct subclass using constructors of base classes, which is more efficient

class father{int x;public:father(int a):x(a){cout<<"father construct:"<<x<<endl;}};class son : public father{int y;public:son(int a, int b):father(a), y(b){cout<<"son construct:"<<y<<endl;}};int main(){son s(1, 2);return 0;}

Output:

Father construct: 1
Son construct: 2

Iii. Multi-Inheritance

1. The root cause of the ambiguity of multiple continuation is

If a has test (), B and c both have test (), so d produces the ambiguity.

Class A {public: void test () {cout <"A" <Endl ;}}; Class B {public: void test () {cout <"B" <Endl ;}}; Class C: Public A, public B {}; int main () {C; C. test (); // error C. a: Test (); // correct. Output: AC. b: Test (); // correct. Output: breturn 0 ;}

2. compilers usually search from the directory tree closest to themselves.

The test () of the subclass overwrites the test () of the base class. It does not mean that the test () of the base class disappears, but cannot be accessed directly.

Class A {public: void test () {cout <"A" <Endl ;}}; Class B {public: void test () {cout <"B" <Endl ;}}; Class C: Public A, public B {void test () {cout <"C" <Endl ;}}; int main () {C; C. test (); // correct, output: CC. a: Test (); // correct. Output: AC. b: Test (); // correct. Output: breturn 0 ;}

 

3. For a single inheritance, whether the subclass can access the parent class of the parent class depends only on the Inheritance Method.

For multi-inheritance, child classes cannot directly access the parent class of the parent class.
 

4. Use virtual to avoid ambiguity.

Class B: virtual public.

Iv. Inheritance and inclusion 1. The member variable list of a class contains the object of another class, which is called inclusion (inclusive ). 2. Differences between private and private inheritance:

Includes:

1) make the program look clearer and easier to understand

2) There is no Inheritance Problem

3) It can include multiple objects of another class.

Private inheritance:

1) access the protection members of the base class

2) You can redefine virtual functions to achieve polymorphism.

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.