Inheritance (C ++ language)

I. Introduction inheritance

  • C ++, when defining a new class B, class B have found that if all the characteristics of a class A has been written, in addition to the characteristics of class A is not, then you do not have to rewrite from scratch, Class B, but you can class a as a "base class" (also known as "parent"), the class B is written as a base class a "derived class" (also called "subclasses"). In this way, we can say from class A "derivative" out of the class B, it can be said Class B "inheritance" of class A. Inheritance is an abstract model of the relationship between a packaging, the package is a hierarchical classification of different models.

  • A class can be derived from more than one class, which means that it can inherit data and functions from multiple base classes. The definition of a derived class, we use a derived class list to specify the base class. Class derivation list of one or more base classes named in the following form:
class derived-class: access-specifier base-class
  • Features inheritance relationship:

    A, sub-class has all the attributes and behavior of the parent class
    B, is a special subclass of parent
    C, subsection class objects when the parent object can be used
    D, subclasses can add properties and methods of the parent class is not
    E, subclass object can be initialized directly parent class object
    F, subclass object can be assigned directly to the parent object
    G, object-oriented inheritance is an important means of programming code reuse

  • Inheritance Example:

#include <iostream>

using namespace std;

class Parent
{
public:
    Parent(int i = 0)
    {
        member = i;
    }
    void method()
    {
        cout << "member = " << member << endl;
    }
private:
    int member;
};

class Child : public Parent
{
public:
    Child(int i = 0, int j = 0):Parent(i)
    {
        childMember = j;
    }
    void childMethod()
    {
        method();
        cout << "childMember = "<< childMember << endl;
    }
private:
    int childMember;
};

int main(int argc, char *argv[])
{
    Child child(1,2);
    child.method();
    child.childMethod();
    return 0;
}
  • Derived class declaration:
class 派生类名:[继承方式] 基类名
{
    派生类成员声明;
};

If a derived class have multiple base classes, referred to as multiple inheritance; if a class has a base class is called single inheritance.


Second, inheritance

Inheritance defines how to access the members of the base class inheritance. Inheritance has public, private ,, protected. Specify a derived class inherits members and outside the class object access rights to inherit from the base class's members. Inheritance follows:
A, public inherited
public members and protected members of the base class to maintain the original access attributes in a derived class, its private members remain private members of the base class.
B, private inheritance
public members and protected members of the base class in a derived class become private members of its private members remain private members of the base class.
C, protected inheritance
public members and protected members of the base class in a derived class become protected members, private members of its base class remain private members.

Access attribute inherited member = Max {inheritance, the parent class members access attributes}


Third, examples of the different inheritance

#include <iostream>

using namespace std;

class Parent
{
public:
    Parent(int a = 0, int b = 0, int c = 0)
    {
       pub = a;
       pro = b;
       pri = c;
    }
public:
    int pub;
protected:
    int pro;
private:
    int pri;
};

class ChildA : public Parent
{
public:
    ChildA(int a = 0, int b = 0):Parent(a, b)
    {
    }
    void print()
    {
        cout << "pub = " << pub << endl;
        cout << "pro = " << pro << endl;
        //cout << "pri = " << pri << endl;//error,私有成员不可见
    }
};

class ChildB : protected Parent
{
public:
    ChildB(int a = 0, int b = 0):Parent(a, b)
    {

    }
    void print()
    {
        cout << "pub = " << pub << endl;
        cout << "pro = " << pro << endl;
        //cout << "pri = " << pri << endl;//error,私有成员不可见
    }
};

class ChildC : private Parent
{
public:
    ChildC(int a = 0, int b = 0):Parent(a, b)
    {
    }
    void print()
    {
        cout << "pub = " << pub << endl;
        cout << "pro = " << pro << endl;
        //cout << "pri = " << pri << endl;//error,私有成员不可见
    }
};
//默认继承方式
class ChildD :  Parent
{
public:
    ChildD(int a = 0, int b = 0):Parent(a, b)
    {
    }
    void print()
    {
        cout << "pub = " << pub << endl;
        cout << "pro = " << pro << endl;
        //cout << "pri = " << pri << endl;//error,私有成员不可见
    }
};

int main(int argc, char *argv[])
{
    ChildA childa(1000,2000);
    childa.pub = 0;
    //childa.pro = 2000;//error,外部不可见
    //childa.pri = 3000;//error,外部不可见
    childa.print();

    ChildB childb(1001,2001);
    //childb.pub = 1001;//error,外部不可见
    //childb.pro = 2001;//error,外部不可见
    //childb.pri = 3001;//error,外部不可见
    childb.print();

    ChildC childc(1002,2002);
    //childc.pub = 1002;//error,外部不可见
    //childc.pro = 2002;//error,外部不可见
    //childc.pri = 3002;//error,外部不可见
    childc.print();

    ChildD childd(1003,2003);
    //childd.pub = 1003;//error,外部不可见
    //childd.pro = 2003;//error,外部不可见
    //childd.pri = 3003;//error,外部不可见
    childd.print();

    return 0;
}

Fourth, a derived class destructor

Destructor function of the derived class is necessary cleanup before the object is destroyed, the destructor has no type, and no parameters. Calling sequence destructor opposite constructor.
Destructor only one, no overload, no default parameters.

Subclass object destruction destructor calling sequence is as follows:

A, call the class itself destructor
B, calls the destructor member variable
C, to call the parent class destructor


Fifth, with the same name as the parent class subclass cover

Overwrite Same more member of the same name subclass definition parent class, the rules are as follows:
A, sub-class defines members of the same name as the parent class (member variables and member functions)
B, subclass members will hide the same name as the parent class member
C, a member of the same name in the parent class still exists in the sub-category
D by members of the scope of the same name accessor access the parent class
if multiple base classes in a derived class members have the same name, has added the derived class and the base class members of the same name, derived class members will shadow (hide) all the members of the base class with the same name, is called by the scope required to call the base class members with the same name.

#include <iostream>

using namespace std;

class Parent
{
public:
    int m_count;
    void print()
    {
        cout << &m_count << endl;
    }
};

class Child : public Parent
{
public:
    int m_count;
    void print()
    {
        cout << &(Parent::m_count) << endl;
        cout << &m_count << endl;
    }

};

int main(int argc, char *argv[])
{
    Parent p;
    p.print();
    cout << &p.m_count << endl;
    Child child;
    child.print();
    //子类对象的父类同名成员变量访问
    cout << &child.Parent::m_count <<endl;
    //子类对象的父类同名成员函数访问
    child.Parent::print();
    cout << &child.m_count << endl;

    return 0;
}

Function overloading occurs in the same scope, the same name of the parent class and subclass does not constitute a function overloading, are covered by the same name, the same name as a member of the subclass hides the parent class.
Subclasses can define the members of the same name in the parent class, subclass members will hide members of the same name in the parent class, members of the same name in the parent class still exists subclass of the same name by the scope resolution operator :: to access the parent class member.
Member function subclass members of the same name will be hidden in the parent class, sub-class can not overload the parent class member function of the same name, use the scope resolution operator can access the member functions of the same name in the parent class.

#include <iostream>

using namespace std;

class Parent
{
public:
    int mi;
    void add(int i)
    {
        mi += i;
    }
    void add(int a, int b)
    {
        mi += (a + b);
    }
};

class Child : public Parent
{
public:
    int mi;
    void add(int x, int y, int z)
    {
        mi += (x + y + z);
    }
};

int main(int argc, char *argv[])
{
    Parent p;
    p.add(1);
    p.add(1,2);

    Child child;
    child.add(1,2,3);
    //child.add(1);//error
    child.Parent::add(1);
    //child.add(1,2);//error
    child.Parent::add(1,2);

    return 0;
}

Guess you like

Origin www.cnblogs.com/xihuashi/p/11668024.html