标签:
/* *Copyright (c) 2016,烟台大学计算机学院 *All rights reserved. *文件名称 : *作 者 : 刘云 *完成日期 : 2016年5月8号 *版 本 号 : v6.0 * *问题描述 : 阅读程序写出执行结果1(5) *输入描述 : 无 *程序输出 : */ /*********************************(a)****************************************************/ #include<iostream> using namespace std; class A { protected: int a,b; public: A(int aa,int bb):a(aa),b(bb){} void printA(){cout<<"a: "<<a<<"\tb: "<<b<<endl;} }; class B: public A { int c; public: B(int aa,int bb,int cc):A(aa,bb),c(cc){} void printB() { cout<<"a: "<<a<<"\tb: "<<b<<"\tc: "<<c<<endl; } }; int main() { A a(1,1); B b(2,3,4); //此处加入各小题中的代码(a) a=b; a.printA(); b.printA(); b.printB(); return 0; }
我认为的结果是:
a: 2 b: 3
a: 2 b: 3
a: 2 b: 3 c: 4
运行程序的结果是:
/*********************************(a)****************************************************
/*********************************(b)****************************************************/
#include<iostream> using namespace std; class A { protected: int a,b; public: A(int aa,int bb):a(aa),b(bb){} void printA(){cout<<"a: "<<a<<"\tb: "<<b<<endl;} }; class B: public A { int c; public: B(int aa,int bb,int cc):A(aa,bb),c(cc){} void printB() { cout<<"a: "<<a<<"\tb: "<<b<<"\tc: "<<c<<endl; } }; int main() { A a(1,1); B b(2,3,4); //此处加入各小题中的代码(b) a=b; a.printA(); b.printA(); b.printB(); return 0; } //程序的运行结果: //C:\Users\user\Documents\123.cpp||In function 'int main()':| //C:\Users\user\Documents\123.cpp|26|error: no match for 'operator=' (operand types are 'B' and 'A')| //C:\Users\user\Documents\123.cpp|26|note: candidate is:| //C:\Users\user\Documents\123.cpp|11|note: B& B::operator=(const B&)| //C:\Users\user\Documents\123.cpp|11|note: no known conversion for argument 1 from 'A' to 'const B&'| //||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 3 second(s)) ===|
错误的原因是:
应将派生类赋值给基类
理解:
无论在什么情况下,派生类继承的是除构造、析构函数以外的全部成员,派生类还可以添加新的成员增加其独特性,当赋值时,只能将派生类赋值给基类。
/*********************************(c)****************************************************/
#include<iostream> using namespace std; class A { protected: int a,b; public: A(int aa,int bb):a(aa),b(bb){} void printA(){cout<<"a: "<<a<<"\tb: "<<b<<endl;} }; class B: public A { int c; public: B(int aa,int bb,int cc):A(aa,bb),c(cc){} void printB() { cout<<"a: "<<a<<"\tb: "<<b<<"\tc: "<<c<<endl; } }; int main() { A a(1,1); B b(2,3,4); //此处加入各小题中的代码(c) A &r1=a; A &r2=b; r1.printA(); r2.printA(); r2.printB(); return 0; } //运行结果1: //C:\Users\user\Documents\123.cpp||In function 'int main()':| //C:\Users\user\Documents\123.cpp|30|error: 'class A' has no member named 'printB'| //||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 3 second(s)) ===| #include<iostream> using namespace std; class A { protected: int a,b; public: A(int aa,int bb):a(aa),b(bb){} void printA(){cout<<"a: "<<a<<"\tb: "<<b<<endl;} }; class B: public A { int c; public: B(int aa,int bb,int cc):A(aa,bb),c(cc){} void printB() { cout<<"a: "<<a<<"\tb: "<<b<<"\tc: "<<c<<endl; } }; int main() { A a(1,1); B b(2,3,4); //此处加入各小题中的代码(c) A &r1=a; A &r2=b; r1.printA(); r2.printA(); //r2.printB(); return 0; } //运行结果2: //a: 1 b: 1 //a: 2 b: 3 // //Process returned 0 (0x0) execution time : 1.391 s //Press any key to continue. 错误原因:基类不能使用派生类的函数
/*********************************(d)****************************************************/
#include<iostream> using namespace std; class A { protected: int a,b; public: A(int aa,int bb):a(aa),b(bb){} void printA(){cout<<"a: "<<a<<"\tb: "<<b<<endl;} }; class B: public A { int c; public: B(int aa,int bb,int cc):A(aa,bb),c(cc){} void printB() { cout<<"a: "<<a<<"\tb: "<<b<<"\tc: "<<c<<endl; } }; int main() { A a(1,1); B b(2,3,4); //此处加入各小题中的代码(d) A *p=&a; p->printA(); p=&b; p->printA(); p->printB(); return 0; } //运行结果: //C:\Users\user\Documents\123.cpp||In function 'int main()':| //C:\Users\user\Documents\123.cpp|30|error: 'class A' has no member named 'printB'| //||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 3 second(s)) ===| #include<iostream> using namespace std; class A { protected: int a,b; public: A(int aa,int bb):a(aa),b(bb){} void printA(){cout<<"a: "<<a<<"\tb: "<<b<<endl;} }; class B: public A { int c; public: B(int aa,int bb,int cc):A(aa,bb),c(cc){} void printB() { cout<<"a: "<<a<<"\tb: "<<b<<"\tc: "<<c<<endl; } }; int main() { A a(1,1); B b(2,3,4); //此处加入各小题中的代码(d) A *p=&a; p->printA(); p=&b; p->printA(); // p->printB(); return 0; } //运行结果: //a: 1 b: 1 //a: 2 b: 3 // //Process returned 0 (0x0) execution time : 1.224 s //Press any key to continue. 错误原因:p属于基类指针不能派生类的函数
/*********************************(e)****************************************************/
#include<iostream> using namespace std; class A { protected: int a,b; public: A(int aa,int bb):a(aa),b(bb){} void printA(){cout<<"a: "<<a<<"\tb: "<<b<<endl;} int getA(){return a;} }; class B: public A { int c; public: B(int aa,int bb,int cc):A(aa,bb),c(cc){} void printB() { cout<<"a: "<<a<<"\tb: "<<b<<"\tc: "<<c<<endl; } }; void f(A x) { cout<<"aaaaah, my a: "<<x.getA()<<endl; } int main() { A a(1,1); B b(2,3,4); //此处加入各小题中的代码(e) f(a); f(b); return 0; }
我认为的运行结果为:
aaaaah, my a: 1
aaaaah, my a: 2
程序的运行结果:
标签:
原文地址:http://blog.csdn.net/jingmin52296358al/article/details/51344459