C ++ - the concept of inheritance and significance, the level of access to the succession, different inheritance

A. The concept of inheritance and significance

Q: Is there is a direct relationship between the classes?
Thus the problem to think life examples: Combination
A. combining relationship: the portion of the overall relationship
C ++ - the concept of inheritance and significance, the level of access to the succession, different inheritance
describes the relationship between the combination - sample code

#include <iostream>
#include <string>

using namespace std;

class Memory
{
public:
    Memory()
    {
        cout << "Memory()" << endl;
    }
    ~Memory()
    {
        cout << "~Memory()" << endl;
    }
};

class Disk
{
public:
    Disk()
    {
        cout << "Disk()" << endl;
    }
    ~Disk()
    {
        cout << "~Disk()" << endl;
    }   
};

class CPU
{
public:
    CPU()
    {
        cout << "CPU()" << endl;
    }
    ~CPU()
    {
        cout << "~CPU()" << endl;
    }    
};

class MainBoard
{
public:
    MainBoard()
    {
        cout << "MainBoard()" << endl;
    }
    ~MainBoard()
    {
        cout << "~MainBoard()" << endl;
    }    
};

class Computer
{
    Memory mMem;
    Disk mDisk;
    CPU mCPU;
    MainBoard mMainBoard;
public:
    Computer()
    {
        cout << "Computer()" << endl;
    }
    void power()
    {
        cout << "power()" << endl;
    }
    void reset()
    {
        cout << "reset()" << endl;
    }
    ~Computer()
    {
        cout << "~Computer()" << endl;
    }
};

int main()
{   
    Computer c;

    return 0;
}

This code can be seen, a portion of each is independent of the computer, but the computer when creating classes of calls to each section, the output is expected to use other types of objects as members of the class computer
outputs the result as follows
C ++ - the concept of inheritance and significance, the level of access to the succession, different inheritance
cOMBINATION relationship
1. other objects of the class as a member of the current class
lifecycle object 2. the object of the current members of the same class
3. member objects consistent with common usage in the object

B. inheritance relationship: parent-child relationship
C ++ - the concept of inheritance and significance, the level of access to the succession, different inheritance
object-oriented inheritance refers to the parent-child relationship between classes
1. sub-class has all the properties and behavior of the parent class
2 subclass is a special kind of parent
3. subclass object can be used as using the parent class object
4. subclasses can be added to the methods and properties of the parent class is not
the code of the inheritance relationship

#include <iostream>
#include <string>

using namespace std;

class Memory
{
public:
    Memory()
    {
        cout << "Memory()" << endl;
    }
    ~Memory()
    {
        cout << "~Memory()" << endl;
    }
};

class Disk
{
public:
    Disk()
    {
        cout << "Disk()" << endl;
    }
    ~Disk()
    {
        cout << "~Disk()" << endl;
    }   
};

class CPU
{
public:
    CPU()
    {
        cout << "CPU()" << endl;
    }
    ~CPU()
    {
        cout << "~CPU()" << endl;
    }    
};

class MainBoard
{
public:
    MainBoard()
    {
        cout << "MainBoard()" << endl;
    }
    ~MainBoard()
    {
        cout << "~MainBoard()" << endl;
    }    
};

class Computer
{
    Memory mMem;
    Disk mDisk;
    CPU mCPU;
    MainBoard mMainBoard;
public:
    Computer()
    {
        cout << "Computer()" << endl;
    }
    void power()
    {
        cout << "power()" << endl;
    }
    void reset()
    {
        cout << "reset()" << endl;
    }
    ~Computer()
    {
        cout << "~Computer()" << endl;
    }
};

int main()
{   
    Computer c;

    return 0;
}

Operating results
C ++ - the concept of inheritance and significance, the level of access to the succession, different inheritance
from the operating results can be found not define the member function void method in the subclass (), but the results in the output value of mv there, because the advantages of inheritance - the sub-class has all the attributes of the parent class and behavior
is also required to know - is a special sub-class of the parent class, subclass object can initialize the direct parent object subclass object can be assigned to the parent class object directly
inherited sense - it is an important means of inheritance in C ++ code reuse . Through inheritance, you can get all the functionality of the parent class and can override existing functionality in a subclass, or add new features

Access level two. Succession

Q: Can a subclass to directly access private members of the superclass?
C ++ - the concept of inheritance and significance, the level of access to the succession, different inheritance
Code verification


#include <iostream>
#include <string>

using namespace std;

class Parent
{
private:
    int mv;
public:
    Parent()
    {
        mv = 100;
    }

    int value()
    {
        return mv;
    }
};

class Child : public Parent
{
public:
    int addValue(int v)
    {
        mv = mv + v;   
    }
};

int main()
{   
    return 0;
}

根据上面的继承关系--子类拥有父类的所有属性和行为,我们可以期待在子类的 int addValue(int v)的成员函数中对父类的私有成员变量mv进行改动
运行结果
C ++ - the concept of inheritance and significance, the level of access to the succession, different inheritance
从运行的结果我们可以得知,子类的int addValue(int v)成员函数出错,提示mv是父类私有成员变量,不能对其进行访问,这与我们期待的结果不一样,所以在子类对父类的修饰的私有成员进行访问有了一种新的访问级别--protected
继承中的访问级别
1.面向对象中的访问级别不只是public和private
2.可以定义protected访问级别
3.关键字protected的意义--修饰的成员不能被外界直接访问,修饰的成员可以被子类直接访问
代码示例--protected的运用

#include <iostream>
#include <string>

using namespace std;

class Parent
{
protected:
    int mv;
public:
    Parent()
    {
        mv = 100;
    }

    int value()
    {
        return mv;
    }
};

class Child : public Parent
{
public:
    int addValue(int v)
    {
        mv = mv + v;    
    }
};

int main()
{   
    Parent p;

    cout << "p.mv = " << p.value() << endl;

    // p.mv = 1000;    // error

    Child c;

    cout << "c.mv = " << c.value() << endl;

    c.addValue(50);

    cout << "c.mv = " << c.value() << endl;

    // c.mv = 10000;  // error

    return 0;
}

运行结果
C ++ - the concept of inheritance and significance, the level of access to the succession, different inheritance
但是当我们在int main中对mv进行修改时得到的运行结果
C ++ - the concept of inheritance and significance, the level of access to the succession, different inheritanceC ++ - the concept of inheritance and significance, the level of access to the succession, different inheritance
可以看到在子类的外部不允许对父类的私有成员变量进行修改
由以上的两个实例我们可以一张图片来完全表示继承关系
C ++ - the concept of inheritance and significance, the level of access to the succession, different inheritance
小结
1.面向对象的访问级不只是public和private
2.protected修饰的成员不能被外界所访问
3.protected使得子类能够访问父类的成员
4.protected关键字是为了继承而专门设计的
5.没有protected就无法完成真正意义上的代码复用

Three in .C ++ supports three different inheritance
1.public inheritance - the parent class members maintain their level of access in a subclass
2.private inheritance - the parent class members become private members in a subclass
3.protected inheritance - - the parent class members become protected members of the public, other members remain unchanged

C ++ - the concept of inheritance and significance, the level of access to the succession, different inheritance

Guess you like

Origin blog.51cto.com/13475106/2415091