The inheritance of C + + __c++

Source: Internet
Author: User

1 Why to inherit
Implement code reuse.
When instantiating a derived class object, the base class constructor is called first, and the derived class constructor is invoked;
When destroying a derived class object, the destructor call order is just the opposite

#include <iostream>
#include <string>
using namespace std;

Class person{public
: Person
    () {
        cout << ' person () ' << Endl
    }
    ~person () {
        cout << "~person ()" << Endl;
    }
    void Eat () {
        cout << "Eat ()" << Endl;
    }
    string M_strname;
    int m_iage;

};


Class worker:p ublic person{public
:
    worker () {
        cout << "worker ()" << Endl;
    }
    ~worker () {
        cout << "~worker ()" << Endl;
    }
    void Worker () {
        cout << "worker ()" << Endl;
    }
    int m_isalary;

};

int main () {
    worker *pworker = new Worker ();

    Delete Pworker;
    Pworker = NULL;
    return 0;
}

2) Way of inheriting
A. Public inheritance
Class A:public B

Base class      public      protected       private
derived class    public       protected       not accessible

b) Protection of inheritance
Class a:protected B

Base class      public          protected       private
derived class    protected        protected       unreachable

c) Private inheritance
Cladd a:private B

Base class      public      protected       private
derived class    private      private         cannot access

3) Hide and overwrite
The parent class void F ();
Subclass void F ();
When instantiating a subclass object, only the subclass's F () is accessible, and the F () of the parent class is hidden;
Summary: Parent-child relationship members are hidden with the same name

Overlay refers to a derived class function that overrides a base class function, characterized by:
Different ranges (in derived classes and base classes, respectively);
The function name is the same;
parameters are the same;
The base class function must have a virtual keyword.

4) is_a Relationship
Eg: humans are the base class, derived classes are soldiers and workers, soldiers and workers are human beings.
A derived class object can be converted to a base class object, whereas it cannot be

5 multiple inheritance and multiple inheritance
Multiple inheritance Human-> Soldier Class-> Infantry class
Multiple inheritance: A derived class has more than one base class

Class worker{};
Class farmer{};
Class Migrantworker:ppublic Worker.public farmer{}; By default, #include <iostream> #include <string> with private inheritance

using namespace Std;
        Class farmer{Public:farmer (String name= "Jack") {m_strname = name;
    cout << "Farmer ()" << Endl;
    Virtual ~farmer () {cout << "~farmer ()" << Endl;
} void Sow ();
Protected:string M_strname;
};
    void Farmer::sow () {cout << m_strname << Endl;
cout << "Farmer--souw ()" << Endl;
        Class worker{Public:worker (String code= "001") {m_strcode = code;
    cout << "Worker ()" << Endl;
    Virtual ~worker () {cout << "~worker ()" << Endl;
        } void Carry () {cout << m_strcode << Endl;
    cout << "Worker--carry ()" << Endl;
} protected:string M_strcode;

}; Class Migrantworker:public Farmer,public worker{Public:migrantworker (sTring name, string code);
    ~migrantworker () {cout << "~migrantworker ()" << Endl;
}
}; Migrantworker::migrantworker (string name, String code): Farmer (name), Worker (code) {//constructor consistent with initialization list order cout << Mig
Ranworker () "<< Endl;
    int main () {Migrantworker *p = new Migrantworker ("Huheqing", "112");
    P->carry ();
    P->sow ();
    Delete p;
    p = NULL;
return 0; }

6) Virtual inheritance

Diamond Inheritance
With multiple inheritance and multiple inheritance, there are data redundancy problems
Class worker:virtual Public person{

};
Class farmer:virtual Public person{

};
Class migranfarmer:public worker,public farmer{

};
Note: In multiple inheritance or multiple inheritance, when an object is instantiated, the arguments of the parent class with the parameter's constructor are passed through the constructor initialization list of the subclass

#include <iostream> #include <string> using namespace std;
        Class person{Public:person (String color= "Blue") {m_strcolor = color;
    cout << "person ()" << Endl;
    Virtual ~person () {cout << "~person ()" << Endl;
        } void Printcolor () {cout << m_strcolor << Endl;
    cout << "Printcolor ()" << Endl;
} protected:string M_strcolor;
};
        Class Farmer:virtual Public person{public:farmer (string name = "Jack", String color = "Blue"):P Erson (color) {
        M_strname = name;
    cout << "Farmer ()" << Endl;
    Virtual ~farmer () {cout << "~farmer ()" << Endl;
} void Sow ();
Protected:string M_strname;
};
    void Farmer::sow () {cout << m_strname << Endl;
cout << "Farmer--souw ()" << Endl; Class Worker:virtual Public person{public:worker (String code = "001", String color = "Blue"):P Erson (colOR) {M_strcode = code;
    cout << "Worker ()" << Endl;
    Virtual ~worker () {cout << "~worker ()" << Endl;
        } void Carry () {cout << m_strcode << Endl;
    cout << "Worker--carry ()" << Endl;
} protected:string M_strcode;

};
    Class Migrantworker:public Farmer,public worker{public:migrantworker (string name, string code,string color);
    ~migrantworker () {cout << "~migrantworker ()" << Endl;
}
}; Migrantworker::migrantworker (string name, string code,string color): Farmer (Name,color), Worker (Code,color) {//
Constructors are consistent with the initialization list order cout << "Migranworker ()" << Endl;
    int main () {Migrantworker *p = new Migrantworker ("Huheqing", "112", "Black");
    P->carry ();
    P->sow ();
    Delete p;
    p = NULL;
return 0; }

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.