/*copyright(c)2016.烟台大学计算机学院
* All rights reserved,
* 文件名称:text.Cpp
* 作者:刘涛
* 完成日期:2016年5月7日
* 版本号:vc++6.0
* 问题描述:阅读下面的定义,请说出在测试函数中不同情况的调用产生的结果
*/
#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 &r1=a;
A &r2=b;
r1.printA();
r2.printA();
//r2.printB();
return 0;
}
错误原因 error: 'class A' has no member named 'printB' r1,r2都是A基类中的成员,r2.printB()这是基类调用派生类中的成员函数,这是不合法的。
正确后输出结果:a:1b:1 a:2b:3