Inheritance and derived classes

Inheritance and derived classes

Format:

class derived class name: the name of the base class inherits the way, the name of the base class inheritance ....

{
class member
}

Inherit a variety of forms:

all members of the base class will inherit inherit

graph LR
A类-->B
A类-->C
graph LR
A类-->B类
B类-->C类
graph LR
A类-->B类
B类-->C类
A类-->D类
D类-->C类

There inheritance: public, protected, privated

for the inherited class is the base class for all members of the group (apart from the base class constructor and destructor) are inherited, and only different access rights. Generally yl private members of the class, the derived class can not directly access any inheritance like this

  • public: the base class public and protected in the derived class is still public and privated, but privated base class in the derived class can not directly access (available inherit the base class function of these private member access to operate)

  • protected: public and protected base class becomes protected in a derived class, the base class privated not directly accessible in a derived class

  • privated: the base class becomes privated public and protected in the derived class, the base class privated not directly accessible in a derived class

Derived classes:

in addition to inherited things also joined the direct features

Class type compatible

Class type compatible

#include
   
   
    
    
using namespace std;

class Base1{ //基类base1定义
public: 
    void display() const{
    cout << "Base1::display()" << endl;
    }
};

class Base2:public Base1{//共有派生类base2定义 
public:
    void display() const{
        cout << "Base2::disply()" << endl; 
    } 
};

class Derived:public Base2{
public:
    void display() const{
        cout << "Derived::display()" << endl;
    }
};

void fun(Base1 *ptr)
{
    ptr->display();
}
int main()
{
    Base1 base1;
    Base2 base2;
    Derived derived;
    fun(&base1);
    fun(&base2);
    fun(&derived);
    return 0;
}

   
   

result:


Derived class constructor, destructor, and copy functions

Constructor (for 1,2UML type)

  1. If you did not write the constructor uses the default constructor
  2. If writing a default constructor with arguments and did not write the default constructor, then when generating the derived class constructor needs to be displayed on their structure (I do not write the details)
  3. Note: The default constructor initializes the list, you can initialize the object class, the default constructor of the base class, the derived class data members, but can not be entrusted constructor of a derived class, commissioned by the constructor to be alone
Constructor of the class

#include
   
   
    
    
using namespace std;

class Base1 {
public:
    Base1(int i) { cout << " Constructing Base1" << endl; }
    Base1() = default;
};

class Base2 {
public:
    Base2(int j) { cout << "Constructing Base2" << endl; }
    Base2() = default;
};

class Base3 {
public:
    Base3() { cout << "Constructing Base3 *" << endl; }
};

class Derived :public Base2, public Base1, public Base3 {
public:
    Derived(int a, int b, int c, int d) :member2(d), member1(c), Base1(a), Base2(b){}
    Derived(int k) :Derived() { w = k, h = k; }
    Derived() = default;
private:
    Base1 member1;
    Base2 member2;
    Base3 memeber3;
    int w, h;
};

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

   
   

First constructed in accordance with the order of succession of the class initialization, and then the order to initialize the object by object, delegate constructor FIG line 24 to separate

Copy function

  • Inherited members from the base class, its copy constructor should be achieved by copying the base class constructor
  • Copy constructor derived class is responsible for copying the base class constructor parameters passed
  • Copy constructor derived class can only accept one parameter, this parameter is not only used to initialize the derived class definition members, it will also be transferred to the copy constructor for the base class
  • Copy constructor parameter type is a reference to the base class of the base class, the argument may be derived class object reference

Destructor

  • Destructor is not inherited, the derived class to declare if desired self destructor
  • Destructor method of the same general statement (no inheritance) classes
  • Call sequence destructor opposite constructor
    • First destructor object member, which is configured with a destructor sequence in reverse order
    • Then the execution order of the base class destructor, the execution order is opposite constructor
    • Performed first derived class destructor
Derived class destructor

#include
   
   
    
    
using namespace std;
class Base1 {
public:
    Base1(int i)
    {
        cout << "Constructing Base1" << i << endl;
    }
    ~Base1() { cout << "Destructing Base1" << endl; }
};

class Base2 {
public:
    Base2(int j)
    {
        cout << "Constructing Base2" << j << endl;
    }
    ~Base2() { cout << "Destructing Base2 " << endl; }
};

class Base3 {
public:
    Base3() { cout << "Constructing Base3 *" << endl; }
    ~Base3() { cout << "Destructing Base3" << endl; }
};

class Derived :public Base2, public Base1, public Base3 {
public:
    Derived(int a, int b,int c, int d):Base1(a), member2(d), member1(c), Base2(b){}
private:
    Base1 member1;
    Base2 member2;
    Base3 member3;
};

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


   
   

Scope

  • Members of the derived class with the same name of the base class (same number of type parameters) will be covered
  • Derived class base class member who has the same function name, the derived class can not go directly to the call, will produce ambiguity
    • With class name defined c1.A :: f () or c1.B :: f ()
    • Of the same name hidden, members of the same name function again in the derived class call A :: f () or B :: f ()

Virtual base class

To solve the problem: When a derived class is derived from multiple base classes, which have a common base class base class, then access members of this common base class, will produce redundancy and inconsistency may be due to redundancy bring

Virtual base class declaration:

  • BenQ said in a virtual class inheritance
  • class B1:virtual public B

Role of the virtual base class:

  • Mainly used to solve the same base class multiple inheritance ambiguity arising from problems that may occur when multiple inheritance
  • Provides a unique base class members furthest derived class, rather than repeat many times to produce

Note :

In the first stage the common base class will inherit designed virtual base class

Virtual base class inheritance

#include
   
   
    
    
using namespace std;

class Base0 {
public:
    int var0;
    void fun0() { cout << "Member of Base0" << endl; }
};

class Base1 :virtual public Base0 {
public:
    int var1;
};

class Base2 :virtual public Base0 {
public:
    int var2;
};

class Derived :public Base1, public Base2 {
public:
    int var;
    void fun() {
        cout << "Member of Derived" << endl;
    }
};

int main()
{
    Derived d;
    d.var0 = 2;
    d.fun0();
    cout << &d.Base1::var0 << endl;
    cout << &d.Base2::var0 << endl;
    return 0;
}

   
   

Virtual base class constructor

  • When creating objects specified class is called a derived class farthest
  • Members of the virtual base class constructor is furthest from the derived class by calling a constructor initializes a virtual base class
  • Throughout the inheritance hierarchy, directly or indirectly derived class inherits all of the virtual base class, must be listed as a parameter of the virtual base class constructor initializes the list of members constructor. If it is not, call the default constructor represents the virtual base class
  • When creating an object, only as far as the derived class constructor calls the constructor of a virtual base class, other classes call to the virtual base class constructor is ignored
Virtual base class constructor

#include
   
   
    
    
using namespace std;

class Base0 {
public:
    Base0(int var):var0(var){}
    int var0;
    void fun0() { cout << "Member of Base0" << endl; }
};
class Base1 :virtual public Base0 {
public:
    Base1(int var) :Base0(var) {}
    int var1;
};

class Base2 :virtual public Base0 {
public:
    Base2(int var) :Base0(var){}
    int var2;
};

class Derived :public Base1, public Base2 {
public:
    Derived(int var) :Base0(var), Base1(var), Base2(var){}
    int var;
    void fun()
    {
        cout << "Member of Derived" << endl;
    }
};

int main()
{
    Derived d(1);
    d.var0 = 2;
    d.fun0();
    return 0;
}

   
   

Guess you like

Origin www.cnblogs.com/Liberavi/p/11668019.html