第十一周阅读程序(3)

来源:互联网 发布:sqlserver 默认值约束 编辑:程序博客网 时间:2024/05/16 01:56

代码:

#include<iostream>using namespace std;class  A{private:    int  x;protected:    int y;public:    int z;    A(int a,int b,int c)    {        x=a;        y=b;        z=c;    }    int  Getx()    {        return x;    }    int  Gety()    {        return y;    }    void ShowA()    {        cout<< "x="<<x<<'\t';        cout<<"y="<<y<<'\t';        cout<<"z="<<z<<'\n';    }};class B:public A   //修改点(见后面阅读要求){private:    int m,n;public:    B(int a,int b,int c,int d,int e):A(a,b,c)    {        m=d;        n=e;    }    void Show()    {        cout<<"m="<<m<<'\t'<<"n="<<n<<'\n';        cout<<"x="<<Getx()<<'\t';        cout<<"y="<<y<<'\t'<<"z="<<z<<'\n';    }    int Sum()    {        return (Getx()+y+z+m+n);    }};int main(){    B b1(1,2,3,4,5);    b1.ShowA();    b1.Show();    cout<< "Sum="<<b1.Sum()<<'\n';    cout<<"x="<<b1.Getx()<<'\t';    cout << "y=" <<b1.Gety()<<'\t';    cout << "z="<<b1.z<<'\n';    return 0;}


运行结果:

学习心得:

运行结果和预想的结果一致

基类A中的访问权限:x是私有型的,y是保护型的,z是公有型的

在派生类B中基类A的访问方法:x是不可访问的,y是保护型的,z是公有型的

问题:

请将class B:public A 中的public改为protected,对程序进行编译,解释出现的情况。
代码:

#include<iostream>using namespace std;class  A{private:    int  x;protected:    int y;public:    int z;    A(int a,int b,int c)    {        x=a;        y=b;        z=c;    }    int  Getx()    {        return x;    }    int  Gety()    {        return y;    }    void ShowA()    {        cout<< "x="<<x<<'\t';        cout<<"y="<<y<<'\t';        cout<<"z="<<z<<'\n';    }};class B:protected A   //修改点(见后面阅读要求){private:    int m,n;public:    B(int a,int b,int c,int d,int e):A(a,b,c)    {        m=d;        n=e;    }    void Show()    {        cout<<"m="<<m<<'\t'<<"n="<<n<<'\n';        cout<<"x="<<Getx()<<'\t';        cout<<"y="<<y<<'\t'<<"z="<<z<<'\n';    }    int Sum()    {        return (Getx()+y+z+m+n);    }};int main(){    B b1(1,2,3,4,5);    b1.ShowA();    b1.Show();    cout<< "Sum="<<b1.Sum()<<'\n';    cout<<"x="<<b1.Getx()<<'\t';    cout << "y=" <<b1.Gety()<<'\t';    cout << "z="<<b1.z<<'\n';    return 0;}


||=== 生成: Debug in a (compiler: GNU GCC Compiler) ===|
F:\新建文件夹\a\main.cpp||In function 'int main()':|
F:\新建文件夹\a\main.cpp|25|error: 'void A::ShowA()' is inaccessible|
F:\新建文件夹\a\main.cpp|56|error: within this context|
F:\新建文件夹\a\main.cpp|56|error: 'A' is not an accessible base of 'B'|
F:\新建文件夹\a\main.cpp|17|error: 'int A::Getx()' is inaccessible|
F:\新建文件夹\a\main.cpp|59|error: within this context|
F:\新建文件夹\a\main.cpp|59|error: 'A' is not an accessible base of 'B'|
F:\新建文件夹\a\main.cpp|21|error: 'int A::Gety()' is inaccessible|
F:\新建文件夹\a\main.cpp|60|error: within this context|
F:\新建文件夹\a\main.cpp|60|error: 'A' is not an accessible base of 'B'|
F:\新建文件夹\a\main.cpp|10|error: 'int A::z' is inaccessible|
F:\新建文件夹\a\main.cpp|61|error: within this context|
||=== Build 失败了: 11 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

出现这种情况的原因:将继承方式从public改成protected之后,派生类B中的基类A的成员方法发生了改变,都变成了保护型的,不能在main函数中使用
0 0
原创粉丝点击