第11週実践項目(5)——b
913 ワード
問題およびコード:
実行結果:
実行できません!!!
コンパイルエラーの原因はAがベースクラスであり、Bがサブクラスであり、オブジェクトaでオブジェクトbを初期化することはできず、一般的にサブクラスのオブジェクトでベースクラスのデータメンバーを初期化する
/*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);
b=a;
a.printA();
b.printA();
b.printB();
return 0;
}
実行結果:
実行できません!!!
コンパイルエラーの原因はAがベースクラスであり、Bがサブクラスであり、オブジェクトaでオブジェクトbを初期化することはできず、一般的にサブクラスのオブジェクトでベースクラスのデータメンバーを初期化する