C++--第15课

第15课 - 惊艳的继承

1. 继承的概念

面向对象中的继承指类之间的父子关系,子类拥有父类的所有成员变量和成员函数。子类就是一种特殊的父类,子类对象可以当作父类对象使用,子类可以拥有父类没有的方法和属性。

如下面的程序:

#include <cstdlib>

#include <iostream>

using namespace std;

class Parent

{

private:

    int a;

public:

    Parent()

    {

        a = 1000;

    }

    void print()

    {

        cout<<"a = "<<a<<endl;

    }

};

class Child : Parent  //chlid继承的a和print函数变成了私有的。

{

};

int main(int argc, char *argv[])

{

    Parent parent;

    Child child;

    parent.print();

    //child.print();

    cout << "Press the enter key to continue ...";

    cin.get();

    return EXIT_SUCCESS;

}

2. 深入了解继承

继承时的访问级别设定会影响到成员的访问级别。

class Child : Parent

{

};

等价于:

class Child :private Parent

{

};

注意:

C++中class的继承默认为private继承。

private继承的子类拥有父类的所有成员。

private继承使得父类的所有成员在子类中变为private成员。

l  public继承

父类成员在子类中保持原有访问级别。

l  private继承

父类成员在子类中变为private成员。

为子类添加新的成员:

#include <cstdlib>

#include <iostream>

using namespace std;

class Parent

{

private:

    int a;

public:

    Parent()

    {

        a = 1000;

    }

    void print()

    {

        cout<<"a = "<<a<<endl;

    }

};

class Child : public Parent

{

private:

    int b;

public:

    void set(int a, int b)

    {

        this->a = a;

        this->b = b;

    }

};

int main(int argc, char *argv[])

{

    Parent parent;

    Child child;

    parent.print();

    child.print();  

    cout << "Press the enter key to continue ...";

    cin.get();

    return EXIT_SUCCESS;

}

以上这个程序在运行的时候会出错,显示int a是private的。我们于是开始思考下面的问题:

类成员的访问级别只有public和private是否足够?

3. protected关键字

protected成员可以在子类中被访问,但不能在外界被访问。

protected成员的访问权限介于public和private之间。

#include <cstdlib>

#include <iostream>

using namespace std;

class Parent

{

protected:

    int a;

public:

    Parent()

    {

        a = 1000;

    } 

    void print()

    {

        cout<<"a = "<<a<<endl;

    }

};

class Child : public Parent

{

protected:

    int b;

public:

    void set(int a, int b)

    {

        this->a = a;

        this->b = b;

    }

};

int main(int argc, char *argv[])

{

    Parent parent;

    Child child;

    parent.print();

    child.print();  

    cout << "Press the enter key to continue ...";

    cin.get();

    return EXIT_SUCCESS;

}

运行结果:

a = 1000

a = 1000

4. 继承与访问级别

需要被外界访问的成员直接设置为public。

只能在当前类中访问的成员设置为private。

只能在当前类和子类中访问的成员设置为protected。

private成员在子类中依然存在,但是却无法访问到。

公式:继承成员对外的访问属性= Max{ 继承方式, 父类成员访问级别 }

5. 继承与访问级别深度实践

#include <cstdlib>

#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;  //error ,private

        cout<<"b = "<<b;  //right

        cout<<"c = "<<endl;  // right

    }

};

class C : protected A

{

public:

    void print()

    {

        cout<<"a = "<<a;  // error ,private

        cout<<"b = "<<b;  //

        cout<<"c = "<<endl;  //

    }

};

class D : private A

{

public:

    void print()

    {

        cout<<"a = "<<a; // error

        cout<<"b = "<<b;

        cout<<"c = "<<endl;

    }

};

int main(int argc, char *argv[])

{

    A aa;

    B bb;

    C cc;

    D dd; 

    aa.c = 100;

    bb.c = 100;

    cc.c = 100;  //reeor

    dd.c = 100;  //error

    aa.set(1, 2, 3);

    bb.set(10, 20, 30);

    cc.set(40, 50, 60);  //error

    dd.set(70, 80, 90); //error

    bb.print();

    cc.print();

    dd.print();

    cout << "Press the enter key to continue ...";

    cin.get();

    return EXIT_SUCCESS;

}

小结:

继承是一种类之间的关系,子类是一种特殊的父类。

子类通过继承可以得到父类的所有成员。

private成员可以被子类继承但不能被子类访问。

protected成员只能在当前类和子类中被访问。

不同的继承方式可能改变继承成员的访问属性。

原文地址:https://www.cnblogs.com/free-1122/p/11336216.html