第15节 惊艳的继承

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pt_raspi_fresher/article/details/88410048

-------------------------------------资源来源于网络,仅供自学使用,如有侵权,联系我必删.

第一:

继承的概念

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

 继承初体验

#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//默认是私有继承
{
    
};

int main(int argc, char *argv[])
{
    Parent parent;
    Child child;
    
    parent.print();
    child.print();//由于私有继承原因,在main函数里面不能调用child函数里面的print函数,没有访问级别
    
    cout << "Press the enter key to continue ...";
    cin.get();
    return EXIT_SUCCESS;
}

深入了解继承

  C++ 中的访问级别与继承
 继承时的访问级别设定会影响到成员的访问级别

第二:

C++ 中的访问级别与继承
     public 继承
           父类成员在子类中保持原有访问级别
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)//新的成员函数   a属于Parent类,在Child里属于外部,没有访问级别
    {
        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;
}

第三:

小插曲

问题

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

新的关键字

 类的 protected 成员
  
protected 成员可以在子类中被访问,但不能在外界被访问
  
protected 成员的访问权限介于 publicprivate 之间

类的 protected 成员

#include <cstdlib>
#include <iostream>

using namespace std;

class Parent
{
protected://新关键字      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;
}

第四:

继承与访问级别

 类成员访问级别设置的原则
  需要被外界访问的成员直接设置为public
  只能在当前类中访问的成员设置为private
  只能在当前类和子类中访问的成员设置为protected

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

继承与访问级别深度实践

#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()
    {
        //a 在父类里面是private 在子类调用 都会报错
        cout<<"a = "<<a;//Error
        cout<<"b = "<<b;
        cout<<"c = "<<endl;
    }
};

class C : protected A
{
public:
    void print()
    {
        cout<<"a = "<<a;//Error
        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;//Error
    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 成员只能在当前类和子类中被访问
  不同的继承方式可能改变继承成员的访问属性

猜你喜欢

转载自blog.csdn.net/pt_raspi_fresher/article/details/88410048