第十一周阅读程序(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)) ===|
- 第十一周阅读程序(3)
- 第十一周 阅读程序(3)
- 第十一周阅读程序(3)
- 第十一周 阅读程序(3)
- 第十一周 阅读程序 (3)
- 第十一周 阅读程序(3)
- 第十一周阅读程序(3)
- 第十一周阅读程序(3)
- 第十一周阅读程序3
- 第十一周阅读程序3
- 第十周第十一周项目1阅读程序(3)
- 第十一周 阅读程序
- 第十一周阅读程序
- 第十一周阅读程序
- 第十一周阅读程序
- 第十一周阅读程序
- 第十一周程序阅读
- 第十一周阅读程序
- [leetcode][search] Search in Rotated Sorted Array II
- 【坐在马桶上看算法】算法12:堆——神奇的优先队列(下)
- shell脚本执行的几种方式
- 黑马程序员----常见对象
- 详解JavaScript模块化开发
- 第十一周阅读程序(3)
- 鸡啄米vc++2010系列23(列表框控件ListBox)
- Html5在Android开发中的应用
- Java中LinkedList和ArrayList的区别
- 提高第27课时,实践3,项目3-数组的排序
- matlab对音频均匀量化
- 关于python IDE中中文报错的解决办法
- VMWare虚拟机下为Ubuntu 12.04.1配置静态IP(NAT方式)
- 写个较通用的32位apihook函数 免得每次都重写