第15节 惊艳的继承

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

第一:

继承的概念

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

 

 继承初体验

 

#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 成员只能在当前类和子类中被访问
  不同的继承方式可能改变继承成员的访问属性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值