Inheritance and derivation of C + + classes

Source: Internet
Author: User
Tags access properties
inheritance and derivation of C + + classes

First, the basic concept 1, class inheritance, is the new class from existing classes there to get the existing characteristics. Or the process of generating a new class from an existing class is the derivation of the class. The original class is called a base class or a parent class, and the resulting new class is called a derived class or subclass.   2, derived class declaration: Class derived class Name: Inheritance method base class Name 1, inheritance method base class Name 2, ..., inheritance way base class name N {    derived class member declaration;   3, a derived class can have multiple base classes at the same time, which is called multiple inheritance, and a derived class has only one base class, called single inheritance. Direct derivation, indirect derivation.   4, the inheritance method provides access to the members of the base class inheritance. Inheritance way has public, private, protected. If you do not show an inheritance method, the default is private inheritance. The inheritance method specifies the derived class members and the access rights of the outer-class objects to members inherited from the base class.   5, derived classes inherit all members except constructs and destructors in the base class.   6, derived class generation:    absorbing base class members (all except constructors);    transform base class members (based on inheritance to adjust the access of base class members, overrides of functions in subclasses, and overlay of virtual functions in subclasses);    add new members;   7, public inheritance when a class is inherited as a public inheritance, the access properties of the public and protected members of the base class are unchanged in the derived class, and the private members of the base class are inaccessible. That is, the public and protected members of the base class are inherited to the public and protected members of the derived class that are still derived. Other members of the derived class can access them directly. A member of a derived class or an object of a derived class cannot access a private member of the base class.   8, private inheritance when a class inherits as a private inheritance, both public and protected members in the base class appear in the derived class as private members, and the private members of the base class are not accessible in the derived class. Public members of the base class and protected members are inherited as private members of derived classes, and other members of the derived class can access them directly, but cannot be accessed outside the class through the objects of the derived class. You cannot access a private member inherited from a base class, whether it is a member of a derived class or an object from a derived class. After multiple private inheritance, the members of the base class become inaccessible. Therefore, private inheritance is less used.   9, protect inheritance protection inheritance, both public and private members of the base class appear in the derived class as protection members, while the private members of the base class are inaccessible. Other members of the derived class can directly access public and protected members inherited from the base class, but they cannot be accessed by objects outside the class through the derived class, withoutA member of a derived class or an object of a derived class cannot access the private members of the base class.   Ii. constructors and destructors for derived classes 1, the initialization of a member inherited from a base class in a derived class, or by the constructor of the base class, and then the new members in the derived class are initialized in the constructor of the derived class.   2, the syntax of derived class constructors: derived class Name:: derived class name (parameter Total table): base class name 1 (parameter table 1), base class name (argument Name 2) .... base class name N (parameter name N), inline child object 1 (Parameter table 1), inline child object 2 (Parameter Table 2) .... Inline child object N (parameter table N) {    Initialization statement for new members of derived class;} Note: constructors are initialized in the order in which they are not in the sequence above, but in the order in which they are declared.   3, if the base class does not have a constructor without parameters, the base class constructor must be called in the constructor of the derived class to initialize the base class member.   4, the order in which the derived class constructors execute:     Call the base class constructor, in the order that they are declared when they are inherited (from left to right);    call the constructor of the inline member object, The call order is in the order that they are declared in the class;    the contents of the constructor body of the derived class. Example:  

#include <iostream>
#include <time.h>
using namespace std;

Class B1
{public
:
    B1 (int i)
    {
        cout<< "constructing B1" <<i<<endl;
    }
};

class B2
{public
:
    B2 (int j)
    {
        cout<< "constructing B2" <<j<<endl;
    }
};

Class B3
{public
:
    B3 ()
    {
        cout<< "constructing B3" <<endl;
    }
};

Class C:public B2, public B1, public B3
{public
:
    C (int A, int b, int C, int d): B1 (a), memberB2 (d), member B1 (c), B2 (b)
    {

    }
private:
    B1 memberB1;
    B2 memberB2;
    B3 memberB3;
};

int main () 
{ 
    C obj (1,2,3,4);

    return 0; 
}

Output is: Constructing B2 2 constructing B1 1 constructing B3 constructing B1 3 constructing B2 4 constructing B3 5, destructor derivation The function of a class destructor is to perform some necessary cleanup before the object dies, with no type and no parameters. Destructors are executed in the opposite order as constructors. Example:

 #include <iostream> #include <time.h> using

namespace Std;
    Class B1 {public:b1 (int i) {cout<< "Constructing B1" <<i<<endl;
    } ~b1 () {cout<< "destructing B1" <<endl;

}
};
    Class B2 {public:b2 (int j) {cout<< "Constructing B2" <<j<<endl;
    } ~b2 () {cout<< "destructing B2" <<endl;

}
};
    Class B3 {public:b3 () {cout<< "Constructing B3" <<endl;
    } ~b3 () {cout<< "destructing B3" <<endl;

}
}; 
    Class C:public B2, public B1, public B3 {public:c (int a, int b, int C, int d): B1 (a), memberB2 (d), memberB1 (C), B2 (b)
    {} PRIVATE:B1 memberB1;
    B2 memberB2;
B3 memberB3;

};

    int main () {C obj (1,2,3,4); 
return 0; }

  Output: Constructing B2 2 constructing B1 1 constructing B3 constructing B1 3 constructing B2 4 constructing B3 destructing B3 destructing B2 destructing B1 destructing the B3 destructing B1 destructing B2   III. Identification and access of derived class Members 1, division of derived class member properties For four kinds:    inaccessible members; private members; protection members; public members;   2, scope resolution is: base class Name:: member name; base class Name:: member name (parameter table); If multiple base classes of a derived class have members with the same name, A derived class adds such a member with the same name, in which case the derived class member overwrites all the base class members with the same name. This requires a call like this to invoke a member of the same name as the base class. Example: multiple inheritance with the same name    

Related Article

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.