zoukankan      html  css  js  c++  java
  • C++之共有继承、保护继承、私有继承

    1.封装,public,private作用就是这个目的。

      类外只能访问public成员而不能方位private成员;

      private成员只能被类成员和友元访问;

    2.继承,protected的作用就是这个目的;

      protected成员可以被子类对象访问,但不能被类外的访问;

    3.公有继承:class A : public B

    #include<iostream>
    #include<assert.h>
    using namespace std;
     
    class A{
    public:
      int a;
      A(){
        a1 = 1;
        a2 = 2;
        a3 = 3;
        a = 4;
      }
      void fun(){
        cout << a << endl;    //正确
        cout << a1 << endl;   //正确
        cout << a2 << endl;   //正确
        cout << a3 << endl;   //正确
      }
    public:
      int a1;
    protected:
      int a2;
    private:
      int a3;
    };
    class B : public A{
    public:
      int a;
      B(int i){
        A();
        a = i;
      }
      void fun(){
        cout << a << endl;        //正确,public成员
        cout << a1 << endl;       //正确,基类的public成员,在派生类中仍是public成员。
        cout << a2 << endl;       //正确,基类的protected成员,在派生类中仍是protected可以被派生类访问。
        cout << a3 << endl;       //错误,基类的private成员不能被派生类访问。
      }
    };
    int main(){
      B b(10);
      cout << b.a << endl;   //正确
      cout << b.a1 << endl;   //正确
      cout << b.a2 << endl;   //错误,类外不能访问protected成员
      cout << b.a3 << endl;   //错误,类外不能访问private成员
      system("pause");
      return 0;
    }

    4.保护继承

    #include<iostream>
    #include<assert.h>
    using namespace std;
    class A{
    public:
      int a;
      A(){
        a1 = 1;
        a2 = 2;
        a3 = 3;
        a = 4;
      }
      void fun(){
        cout << a << endl;    //正确
        cout << a1 << endl;   //正确
        cout << a2 << endl;   //正确
        cout << a3 << endl;   //正确
      }
    public:
      int a1;
    protected:
      int a2;
    private:
      int a3;
    };
    class B : protected A{
    public:
      int a;
      B(int i){
        A();
        a = i;
      }
      void fun(){
        cout << a << endl;       //正确,public成员。
        cout << a1 << endl;       //正确,基类的public成员,在派生类中变成了protected,可以被派生类访问。
        cout << a2 << endl;       //正确,基类的protected成员,在派生类中还是protected,可以被派生类访问。
        cout << a3 << endl;       //错误,基类的private成员不能被派生类访问。
      }
    };
    int main(){
      B b(10);
      cout << b.a << endl;       //正确。public成员
      cout << b.a1 << endl;      //错误,protected成员不能在类外访问。
      cout << b.a2 << endl;      //错误,protected成员不能在类外访问。
      cout << b.a3 << endl;      //错误,private成员不能在类外访问。
      system("pause");
      return 0;
    }

    5.私有继承

    #include<iostream>
    #include<assert.h>
    using namespace std;
    class A{
    public:
      int a;
      A(){
        a1 = 1;
        a2 = 2;
        a3 = 3;
        a = 4;
      }
      void fun(){
        cout << a << endl;    //正确
        cout << a1 << endl;   //正确
        cout << a2 << endl;   //正确
        cout << a3 << endl;   //正确
      }
    public:
      int a1;
    protected:
      int a2;
    private:
      int a3;
    };
    class B : private A{
    public:
      int a;
      B(int i)
    { A(); a
    = i; } void fun(){ cout << a << endl; //正确,public成员。 cout << a1 << endl; //正确,基类public成员,在派生类中变成了private,可以被派生类访问。 cout << a2 << endl; //正确,基类的protected成员,在派生类中变成了private,可以被派生类访问。 cout << a3 << endl; //错误,基类的private成员不能被派生类访问。 } }; int main(){ B b(10); cout << b.a << endl; //正确。public成员 cout << b.a1 << endl; //错误,private成员不能在类外访问。 cout << b.a2 << endl; //错误, private成员不能在类外访问。 cout << b.a3 << endl; //错误,private成员不能在类外访问。 system("pause"); return 0; }

    总结:

      protected,是指子类可以访问类成员,但是不能被外部访问;

  • 相关阅读:
    pyspark读取parquet数据
    python求时间差
    pandas索引操作之loc,iloc,ix等方法
    pandas的concat和drop函数
    mysql语句的书写顺序和执行顺序
    hive的lower,upper,length,concat,lpad,rpad,cast,split函数简述
    hive的floor函数,ceil函数,round函数
    Pandas建立空的dataframe和cumsum累加函数
    Python基础笔记二之求序列均值、标准差、中位数、分位数
    NAT实验
  • 原文地址:https://www.cnblogs.com/weiyouqing/p/9648563.html
Copyright © 2011-2022 走看看