第十一周实践项目(5)——c

问题及代码:

/*copyright(c)2016.烟台大学计算机学院
* All rights reserved,
* 文件名称:text.Cpp
* 作者:吴敬超
* 完成日期:2016年5月10日
* 版本号:codeblock
*
* 问题描述:
* 输入描述:
* 程序输出: 输出结果
*/
#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<<"s:"<<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;
}

运行结果:

第十一周实践项目(5)——c_第1张图片

错误原因:

基类A引用的是子类中的对象,对象r1只能调用基类中的函数,而不能调用子类中的函数

你可能感兴趣的:(第十一周实践项目(5)——c)