第十三周 程序阅读

#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//B是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=b;
    a.printA();
    b.printA();
    b.printB();
    return 0;
}

//我认为的输出结果:2 3    2 3    2 3 4
//程序的运行结果:2 3    2 3     2 3 4

 

 

(2)

#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//B是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;//此处存在错误
    a.printA();
    b.printA();
    b.printB();
    return 0;
}
//程序会发生编译错误,原因是:多不能赋值少
//记录下IDE中提示的错误并理解:no match for 'operator=' in 'b=a'


 

(3)

#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//B是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;
}
/*
将会发生错误的一行删除;
对余下的程序,你认为输出是:__1,1______2,3__
实际运行的输出是:_______1,1______2,3,_____________
那一行的错误原因是:_________基类中的对象不能调用派生类中的对象_________
*/



 

(4)

#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//B是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 *p=&a;
    p->printA();
    p=&b;
    p->printA();
    //p->printB();错误
    return 0;
}
/*
将会发生错误的一行删除;
对余下的程序,你认为输出是:__1,1______2,3__
实际运行的输出是:_______1,1______2,3,_____________
那一行的错误原因是:_____基类中的对象不能调用派生类中的对象_____________
*/


 

(5)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值