Inheritance and derivation of the 09--c++ class

Source: Internet
Author: User
Tags access properties

First, the basic concept

1, class inheritance, is the new class from existing classes 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 parent class, and the resulting new class is called a derived class or subclass.  2, derived class declaration: Class derived class name: Inherit way base class name 1, Inherit way base class Name 2, ..., inherit way base class name n{    derived class member declaration;}; 3, a derived class can have multiple base classes at the same time. This condition is called multiple inheritance, and the derived class has only one base class, called single inheritance. Direct derivation, indirect derivation.  4, inheritance specifies how to access the members inherited by the base class. The way of inheritance is public, private, protected. If the inheritance method is not shown, the default is private inheritance. inheritance specifies the access rights of derived class members and out-of-class objects to members inherited from the base class.  5, derived classes inherit all members of the base class except constructs and destructors.  6, derived classes generate:   to absorb base class members (except for all members that construct destructors);   to transform base class members (the access of base class members is adjusted according to inheritance, the functions are overwritten in subclasses, and virtual functions are covered in subclasses);    add a new member;  7, public inheritance when a class inherits from a public inheritance, the access properties of the base class's public and protected members 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 into the derived class as public members and protected members of the derived class. Other members of the derived class can access them directly. The private members of the base class cannot be accessed by either the members of the derived class or the object of the derived class.  8, private inheritance when a class inherits in a private inheritance, the public and protected members in the base class appear in the derived class as private memberships, and the private members of the base class are inaccessible in the derived class. The public and protected members of the base class are inherited as private members of the derived class, and other members of the derived class can access them directly, but objects outside the class that are derived from the class cannot be accessed. A private member that inherits from a base class cannot be accessed either by a member of a derived class or by an object of a derived class. After multiple private inheritance, members of the base class become inaccessible. Therefore, private inheritance is less used.  9, protection inheritance protection inheritance, both public and private members of the base class appear in the derived class as protected members, while private members of the base class are inaccessible. Other members of the derived class can access public and protected members that inherit from the base class directly, but the private members of the base class cannot be accessed by an object of the derived class outside the class, regardless of the members of the derivation class or the object of the derived class. &nbspThe constructor and destructor of a derived class 1, the initialization of a member inherited from a base class in a derived class, is done by the constructor of the base class, and then the new member in the derived class is initialized in the constructor of the derived class. Syntax for  2, derived class constructors: derived class Name:: Derived class name (general table of parameters): base class name 1 (parameter table 1), base class name (parameter Name 2) .... base class name N (parameter name N), inline sub-object 1 (Parameter table 1), inline sub-object 2 (Parameter Table 2) .... Inline sub-object N (parameter table N) {    The initialization statement of the new member of the derived class;} Note: The initialization order of constructors is not performed in the order above, but is initialized in the order in which they are declared.  3, if there is no constructor in the base class without parameters, the base class constructor must be called in the constructor of the derived class to initialize the base class member. The order in which the  4, derived class constructors execute:    calls the base class constructor, which invokes the constructor of the inline member object in the order in which they are declared (left to right);   the inheritance. The invocation order;   the contents of the constructor body of the derived class in the order in which they are declared in the 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), memberB1 (C), B2 (b) c14/>{    }private:    B1 memberB1;    B2 memberB2;    B3 memberB3;}; int main () {     C obj (1,2,3,4);    return 0; }

The output is: Constructing B2 2constructing B1 1constructing b3constructing B1 3constructing B2 4constructing B3 5, the function of the destructor of the derived class of the destructor Can be a necessary cleanup before the object dies, the destructor has 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) c25/>{    }private:    B1 memberB1;    B2 memberB2;    B3 memberB3;}; int main () {     C obj (1,2,3,4);    return 0; }

Output: Constructing B2 2constructing B1 1constructing b3constructing B1 3constructing B2 4constructing b3destructing b3des Tructing b2destructing b1destructing b3destructing b1destructing B2 Third, the identity and access of derived class members 1, derived class member properties are divided into four kinds: inaccessible members; private members; protect members ; 2, scope resolution form: base class Name:: Member name, base class Name:: member name (parameter table), if more than one base class of a derived class has a member with the same name, and the derived class adds such a member with the same name, in which case, the derived class member overrides the member of the same name for all base classes. This is required in order to invoke a member of the same name as the base class. Example: multiple inheritance with the same name
#include <iostream> #include <time.h>using namespace Std;class b1{public:    int nV;    void Fun ()    {        cout<< "member of B1" <<nV<<endl;    }}; Class B2{public:    int nV;    void Fun ()    {        cout<< "member of B2" <<nV<<endl;    }}; Class D1:public B1, public b2{public:    int nV;    void Fun ()    {        cout<< "member of D1" <<nV<<endl;    }}; int main () {     D1 D1;    D1.NV = 1;    D1.fun ();    D1. B1::NV = 2;    D1. B1::fun ();    D1. B2::NV = 3;    D1. B2::fun ();    return 0; }

The output is: Member of D1 1member of B1 2member of B2 3 through the scope resolution, the access to the base class of the masked member of the same name is resolved. 3, if some or all of the direct base classes of a derived class are derived from another common base class, in these direct base classes, the members that inherit from the base class above have the same name, and therefore the same names are created in the derived class, and a member of this type with the same name is uniquely identified with the scope resolution. and must be qualified with a direct base class. Example:
#include <iostream> #include <time.h>using namespace Std;class b0{public:    int nV;    void Fun ()    {        cout<< "member of B0" <<nV<<endl;    }}; Class B1:public b0{public:    int nV1;}; Class B2:public b0{public:    int nV2;}; Class D1:public B1, public b2{public:    int nVd;    void Fund ()    {        cout<< "member of D1" <<endl;    }; int main () {     D1 D1;    D1. B1::NV = 2;    D1. B1::fun ();    D1. B2::NV = 3;    D1. B2::fun ();    return 0; }

The output is: Member of B0 2member of B0 3 in this case, the derived class object also has two copies of the member NV and fun in memory. But in many cases, we only need such a copy of the data, multiple copies of the same member increase the memory overhead. You can solve this problem by using virtual functions. 4, virtual base class in order to solve the above mentioned multiple copies of the problem, the common base class can be set to virtual base class, then inherit from a different path of the same name data member in memory only one copy, the same function has only one mapping. The declaration of a virtual base class is a declaration procedure in a derived class that has the following syntax: Class derived class Name:: Virtual Inheritance Mode base class name; Example:
#include <iostream> #include <time.h>using namespace Std;class b0{public:    int nV;    void Fun ()    {        cout<< "member of B0" <<nV<<endl;    }}; Class B1:virtual public b0{public:    int nV1;}; Class B2:virtual public b0{public:    int nV2;}; Class D1:public B1, public b2{public:    int nVd;    void Fund ()    {        cout<< "member of D1" <<endl;    }; int main () {     D1 D1;    D1.NV = 2;    D1.fun ();    return 0; }

The output is: Member of B0 2 5, virtual base class and constructors for derived classes in general, derived classes only pass arguments to the constructors of their direct base classes, but in a virtual base class, all derived classes, whether directly or indirectly, virtual base classes, must list the initialization of the virtual base class in the constructor's member initialization list. Example:
#include <iostream> #include <time.h>using namespace Std;class b0{public:    B0 (int n)    {        NV = n;    }    int NV;    void Fun ()    {        cout<< "member of B0" <<nV<<endl;    }}; Class B1:virtual public b0{public:    B1 (int a): B0 (a)       {    }    int nV1;}; Class B2:virtual public b0{public:    B2 (int a): B0 (a)    {    }    int nV2;}; Class D1:public B1, public b2{public:    D1 (int a): B0 (a), B1 (a), B2 (a)    {    }    int nVd;    void Fund ()    {        cout<< "member of D1" <<endl;    }; int main () {     D1 D1 (1);    D1.NV = 2;    D1.fun ();    return 0; }

The above example looks like the B0 constructor has been called three times, but actually only the D1 (int a) in the D1 class: B0 (a), B1 (a), B2 (a)
is the real call to the B0 constructor. Iv. assignment Compatibility Rule 1, assignment compatibility rules are substituted for objects of the public derived class that can be used anywhere a base class object is needed.   2. The substitution of assignment-compatible rules includes: an object of a derived class can be assigned to a base class object, an object of a derived class can initialize a reference to a base class, and the address of a derived class object can be assigned to a pointer to a base class. After the override, the derived class object can be used as an object of the base class, but only members that inherit from the base class are used. Example:
#include <iostream> #include <time.h>using namespace Std;class b0{public:    void display ()    {        cout<< "B0::d isplay ()" <<endl;    }; Class B1:public B0{public:    void display ()    {        cout<< "B1::d isplay ()" <<endl;    }}; Class B2:public B0{public:    void display ()    {        cout<< "B2::d isplay ()" <<endl;    }}; void Fun (B0 *ptr) {    ptr->display ();} int main () {     B0 b0;    B1 B1;    B2 B2;    Fun (&b0);    B0 = B1;    Fun (&b0);    B0 = B2;    Fun (&b0);    return 0; }

The output is:

B0::d isplay ()

B0::d isplay ()

B0::d isplay ()

After this assignment is compatible, the function with the same name for each invocation is a function of the same name as the base class, and a virtual function is required if you want to invoke the derived class.

Inheritance and derivation of the 09--c++ 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.