当前位置: 代码迷 >> 综合 >> c++CLASS public protected private 继承
  详细解决方案

c++CLASS public protected private 继承

热度:82   发布时间:2023-10-14 03:19:35.0
#include <iostream>
using namespace std;
class Inherit//继承
{public:
    string public_str;
    void public_func()
    {cout<<"public_func:"<<public_str<<endl;}
protected:
    string protected_str;
    void protected_func()
    {cout<<"protected_func:"<<protected_str<<endl;}
private:
    string private_str;
    void private_func()
    {cout<<"private_func:"<<private_str<<endl;}
};
/*派生类通过 public 继承,基类的各种权限不变*/
class inheritPublic:public Inherit
{public:
    inheritPublic(){
   
    /*派生类的成员函数,可以访问基类的public成员、protected成员,*/
    public_str=   "public_str   ";
    protected_str="protected_str";
    public_func();
    protected_func();
    /*但是无法访问基类的private成员*/
    //private_str="private_str"; 访问不到 错误
    //private_func();
    }
    void inheritPublicFunc()
    {cout<<"inheritPublicFunc:"<<inheritPublicData<<endl;
    }
    ~inheritPublic()
    {}
private:    string inheritPublicData="kakakaka";
};
/*派生类通过protected继承,基类的public成员在派生类中的
 * 权限变成了protected 。protected和private不变*/
class inheritProtected:protected Inherit
{public:
        inheritProtected()
        {/*派生类的成员函数,可以访问基类的public成员、protected成员,
          但是无法访问基类的private成员*/
            public_str=   "public_str   ";
            protected_str="protected_str";
            public_func();
            protected_func();
            //private_str   ="private_str";
            //private_func();
        }
        void inheritProtectedFunc()
        {cout<<"inheritProtectedFunc:"<<inheritProtectedData<<endl;}
        ~inheritProtected()
        {}
private: string inheritProtectedData="bababa";
};
class inheritPrivate: private Inherit
{public:
    inheritPrivate(){
   
        /*派生类的成员函数,可以访问基类的public成员、protected成员,
         * 但是无法访问基类的private成员*/
        public_str=   "public_str   ";
        protected_str="protected_str";
        public_func();
        protected_func();
        //private_str  ="private_str";
        //private_func();
      }
    void inheritPrivateFunc()
    {cout<<"inheritPrivateFunc:"<<inheritPrivateData<<endl;}
    ~inheritPrivate(){}
private: string inheritPrivateData="cacaca";
};
int main()
{   inheritPublic    iPublic;//数据在派生类内  实例化派生类 将数据通过基类显示出来
    iPublic.inheritPublicFunc();
    cout<<"================iPublic   "<<endl;
    inheritProtected iProtected;
    iProtected.inheritProtectedFunc();
    cout<<"================iProtected"<<endl;
    inheritPrivate   iPrivate;
    iPrivate.inheritPrivateFunc();
    cout<<"================iPrivate  "<<endl;
    cout<<"实例化派生类-> 基类"<<endl;
    iPublic.public_func();
      //iProtected.protected_func();
      //iPrivate.private_func();
 
    //继承基类的protected; 类的实例对象无法访问
    //任何成员
      //iProtected.public_func();
      //iProtected.protected_func();
      //iProtected.private_func();
 
    //继承基类的private; 类的实例对象无法访问
    //任何成员
      //iPrivate.public_func();
      //iPrivate.protected_func();
      //iPrivate.private_func();
    cout << "Hello World!" << endl;
    return 0;
}
 
 

c++CLASS public protected private 继承

c++CLASS public protected private 继承

c++CLASS public protected private 继承

c++CLASS public protected private 继承

c++CLASS public protected private 继承

c++CLASS public protected private 继承

c++CLASS public protected private 继承 

  相关解决方案