问题及代码:
Problem L: C++继承(改错题)
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 352 Solved: 243
[ Submit][ Status][ Web Board]
Description
下面的程序中,存在着两处语法错误。请改正错误,使程序按下面输入输出的规定运行。
只提交修改过的begin到end部分的代码。
//******************** begin ********************
#include <iostream>
using namespace std;
class A
{
private:
int a,b;
public:
A(int aa, int bb) {a=aa; b=bb;}
void printA()
{
cout<<"a: "<<a<<" b: "<<b<<endl;
}
}
class B: public A
{
int c;
public:
B(int aa, int bb, int cc){a=aa;b=bb;c=cc;}
void printB()
{
cout<<"a: "<<a<<" b: "<<b<<" c: "<<c<<endl;
}
};
//********************* end ********************
int main()
{
int a1, a2;
int b1, b2, b3;
cin>>a1>>a2;
cin>>b1>>b2>>b3;
A a(a1,a2);
B b(b1,b2,b3);
a=b;
a.printA();
b.printB();
return 0;
}
Input
用于初始化对象的5个整数
Output
调用 a.printA();和b.printB();产生输出
Sample Input
1 2
3 4 5
Sample Output
a: 3 b: 4
a: 3 b: 4 c: 5
HINT
//******************** begin ********************
#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<<" b: "<<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::a<<" b: "<<A::b<<" c: "<<c<<endl;
}
};
//********************* end ********************
int main()
{
int a1, a2;
int b1, b2, b3;
cin>>a1>>a2;
cin>>b1>>b2>>b3;
A a(a1,a2);
B b(b1,b2,b3);
a=b;
a.printA();
b.printB();
return 0;
}
运行结果: