c+++ Inherited Learning

Keywords: C++ REST

Basic concepts of inheritance

  • Inheritance and Derivation
    • Inheritance concept
    • Comprehensive Training of Access Control of Derived Classes (Inherit Three Ways, Class Three Access Control, Three Look Principles)
    • Constructions and Destructions in Inheritance
      • Type Compatibility Principle
      • Constructions and Destructions in Inheritance
      • Processing method of member function and member variable with the same name in inheritance
      • static keyword in inheritance
  • Inheritance concept
  • Comprehensive Training of Access Control of Derived Classes (Inherit Three Ways, Class Three Access Control, Three Look Principles)
  • Constructions and Destructions in Inheritance
    • Type Compatibility Principle
    • Constructions and Destructions in Inheritance
    • Processing method of member function and member variable with the same name in inheritance
    • static keyword in inheritance
  • Multiple Inheritance Concept
  • Multiple Inheritance
    • Multiple Inheritance Concept
    • Ambiguity
    • Basic experimental principle of virtual inheritance
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>

using namespace std;


class Student
{
public:
    Student()
    {

    }
    Student(int id, string name)
    {
        this->id = id;
        this->name = name;
    }

    void printS() {
        cout << "id = " << this->id << ", name = " << this->name << endl;
    }
    int id;
    string name;
};

//Create a new student class, add score, function
class Student2
{
public:
    Student2(int id, string name, int score)
    {
        this->id = id;
        this->name = name;
        this->score = score;
    }

    void printS() {
        cout << "id = " << this->id << ", name = " << this->name << endl;
        cout << "score = " << this->score << endl;
    }
private:
    int id;
    string name;

    //add
    int score;
};


//Create a new student class by inheriting
class Student3 :public Student
{
public:
    Student3(int id, string name, int score) :Student(id, name)
    {

        this->score = score;
    }

    void printS() {
        Student::printS();
        cout << "score = " << this->score << endl;
    }
private:
    int score;
};


int main(void)
{
    Student3 s3(1, "zhang3", 80);

    s3.printS();


    return 0;
}

Inheritance Mode

  • Rule 1, no matter how the inheritance is, no son can access private s in the parent
  • Rule 2, if inherited by the public, access control rights in the son remain unchanged.
  • Rule 3 If inheritance is protected, the father of the son is protected except for the private s.
  • Rule 4. If inherited privately, the father of the son is a privately owned member, except for the private member.

  • Three-Look Principle:
    • See if the member variable invoked is inside or outside the class
    • Looking at how your son inherits,
    • Access control of the current variable in the son in the father
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <memory>

using namespace std;



class Parent
{
public:
    int pub; //Accessible both inside and outside the class.
protected:
    int pro; //Accessible inside class, not outside class
private:
    int pri;  //Accessible inside class, not outside class
};

//public inheritance
class Child :public Parent
{
public:
    void func()
    {
        cout << pub << endl; //The public member variable of the pub parent class is accessible [inside and outside] of the public inherited class.

        cout << pro << endl;//pro is a parent protected member variable that is accessible within the public inherited class.Outside Access Not Available 
                            //At this point, the pro is accessible to grandchildren and says that pro is not a private member at this time, but a protected member

        //Cout << pri << endl; //pri is a parent private member variable that is accessible [inside, outside] of the public inherited class.
    }

};

//Grandchildren
class SubChild : public Child
{
    void sub_func()
    {
        cout << pro << endl;
    }
};


//Protect Inheritance
class Child2 :protected Parent
{
public:
    void func2() {
        pub;//In this case, pub can be accessed inside the class through protected inheritance. 
            //pub is accessible inside the class, outside the class, and son of the class
            //pub is the protected member
        pro;//ProRoot pub is the same property and pro is also a member of protected
        //pri;
    }
};

class Sub_child2:public Child2
{
public:
    void sub_func2() {
        pub;
        pro;
    }
};

//Private Inheritance
class Child3 :private Parent
{
public:
    void func3()
    {
        pub;//pub is accessible inside the class.It is accessible inside a class, but not outside.
            //Pub is not accessible from his son, indicating that pub is a private member in Child3
        pro;//The ProRoot pub is of the same nature and is a private member.
        pri;
    }
};

class Sub_Child3 :public Child3
{
public:
    void sub_fun3()
    {
        pub;
        pro;
    }
};



int main(void)
{
    Child c1;
    c1.func();

    c1.pub;
    //c1.pri;

    //Child2 c2;
    //c2.pub;
    //c2.pro;
    
    Child3 c3;
    //c3.pub;
    //c3.pro;

    Child2 c2;
    c2.pub;
    c2.pro;

    c1.pub;
    

    return 0;
}

Exercise on inheritance

#include     <iostream>
using namespace std;


class   A
{
private:
    int a;
protected:
    int b;
public:
    int c;
    A()
    {
        a = 0;
        b = 0;
        c = 0;
    }
    void    set(int a, int  b, int  c)
    {
        this->a = a;
        this->b = b;
        this->c = c;
    }
};

class   B : public  A
{
public:
    void    print()
    {
        //Cout << "a =" << a; //a is not accessible by private members of the parent class
        cout << "b  =   " << b;              //b is now a protected member and is accessible from within the class
        cout << "c  =   "<<c << endl;           //c is now a public member and is accessible from within the class
    }
};

class   C : protected   A
{
public:
    void    print()
    {
        //Cout << "a =" << a; //a is not accessible by private members of the parent class
        cout << "b  =   " << b;              //b is the protected permission in the subclass, which is accessible inside the class.
        cout << "c  =   " <<c << endl;   //protected member of the c subclass, accessible internally of the class.
    }
};

class   D : private A
{
public:
    void    print()
    {
        //Cout << "a =" << a; //a is not accessible by private members of the parent class
        cout << "b  =   " << b << endl;      //b At this time, it is a private member and is accessible from within the class.
        cout << "c  =   " << c << endl;      //c is now a private member and is accessible from within the class.
    }
};

int main(void)
{
    A   aa;
    B   bb;
    C   cc;
    D   dd;
    aa.c = 100;                  //c is public and accessible outside the class.
    bb.c = 100;                  //Bpublic inherits from A, keeps permissions unchanged, c is public, and the outside of the class is accessible
    //c C.c = 100; //c protected inherits from A, where c is a member of protected and is not accessible outside the class.
    //Dd.c = 100; //D privates inherit from A, C are privates in this class and are not accessible outside the class.
    aa.set(1, 2, 3);            //Can I access it???
    bb.set(10, 20, 30);         //Can I access it???
    //cc.set(40, 50, 60); //Accessible??
    //dd.set(70, 80, 90); //Accessible??

    bb.print();                  //Prit is a public member function defined in class B and is accessible outside the class.
    cc.print();                  //Prit is defined as a class C public member function and is accessible outside the class.
    dd.print();                  //Prit is defined as a public member function in class D and is accessible outside the class.

    return 0;
}

Compatibility Replication Principles for Classes

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

/*
    Subclass objects can be used as parent objects.
    Subclass objects can be assigned directly to parent objects.
    Subclass objects can directly initialize parent objects.
    ****Parent pointer can point directly to child class object***
    Parent references can refer directly to subclass objects
*/

class Parent
{
public:
    void printP() {
        cout << "a " << this->a << endl;
    }
    int a=333;
};

class Child :public Parent
{
public:
    void printC()
    {
        cout << "b = " << this->b << endl;
    }
    int b;
};


void myPrint(Parent *pp)
{
    pp->printP();
}

int main(void)
{
//  Parent p;

//  Child c = p; //p objects do not fill up the c object space.

//  Child c;

//  Parent p = c;//c object occupies more memory than= p object occupies enough space to fill the required space of p object.

//  p = c;

//  c.printP(); //c can be used as parent p.

    Parent *pp = NULL;//Parent Pointer
    Child *cp = NULL;//Subclass Pointer
    

    Parent p;//Parent Object
    Child c; //Subclass object

    

    pp = &c;//The c memory layout satisfies all the needs of the parent pointer and assigns values to the parent pointer using the object address of a son.
    
    myPrint(&p);
    myPrint(&c);

    return 0;
}

Construction and Destruction of Subclasses

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

class Parent
{
public:
    Parent()
    {
        cout << "Parent().." << endl;
        a = 0;
    }
    Parent(int a) {
        cout << "Parent(int)..." << endl;
        this->a = a;
    }
    ~Parent(){
        cout << "~Parent" << endl;
    }
    int a;
};

class Child :public Parent
{
public:
    //When calling the constructor of a subclass, the constructor of the parent class must be called
    // The parent class constructs first, the child class constructs later.
    Child(int a, int b) :Parent(a)
    {
        cout << "Child(int, int)..." << endl;
        this->b = b;
    }

    void printC() {
        cout << "b = " << b << endl;
    }

    ~Child(){
        cout << "~Child()..." << endl;
    }

    int b;
};

int main(void)
{
    Child c(10, 20);

    c.printC();
    
    return 0;
}

Subclass has the same variable name as parent

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;


class Parent
{
public:
    Parent(int a) {
        this->a = a;
    }

    int a;
};

class Child :public Parent
{
public:
    Child(int p_a, int c_a) :Parent(p_a)
    {
        this->a = c_a;
    }

    void print()
    {
        cout << Parent::a << endl;
        cout << this->a << endl;//child's a
    }
    int a;
};


int main(void)
{
    Child c(10, 100);
    c.print();

    
    return 0;
}

static in inheritance

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

class A
{
public:
    static int a;
private:

};

class B :public A
{
public:
private:
};

int A::a = 100;//Initialization of static member variables

int main(void)
{
    A a1;
    A a2;

    cout << a1.a << endl;//100
    cout << a2.a << endl;//100

    A::a = 300;

    cout << a1.a << endl;//300
    cout << a2.a << endl;//300

    B b1;
    B b2;
    A::a = 400;

    cout << "------" << endl;
    cout << b1.a << endl;//400
    cout << b2.a << endl;//400
    cout << a1.a << endl;//400
    cout << a2.a << endl;//400

    return 0;
}

Multiple Inheritance

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

//Furniture class
class Furniture
{
public:
    int m; //Texture of material
};

//Change paternal inheritance to virtual inheritance to prevent my son from making multiple copies of variables in my grandfather when he inherits me more.
class Bed:virtual public Furniture
{
public:
    void sleep() {
        cout << "Sleep in bed" << endl;
    }
};


class Sofa:virtual public Furniture
{
public:
    void sit() {
        cout << "Rest on the sofa" << endl;
    }
};

//Sofa bed
class SofaBed :public Bed, public Sofa
{
public:
    void SleepAndSit() {
        sleep();
        sit();
    }
};

int main(void)
{
    Bed b;
    b.sleep();

    Sofa s;
    s.sit();

    cout << " ------ " << endl;

    SofaBed sb;
    sb.SleepAndSit();

    sb.m = 100;//There is only one m at this time
    // sb.Bed::m = 100;
    // sb.Sofa::m = 200;
    
    return 0;
}

Posted by garrywinkler on Sat, 21 Dec 2019 17:03:31 -0800