vehicleを基底クラスとした、2つの派生クラスbicycle motorcar、またbicycle とmotorcarを基底クラスとした派生クラスmotorcycle を用意しました。これら5つのオブジェクトをそれぞれ5つだけ生成したいのですが、vehicle が2回生成されてしまいます。どのようにすれば良いのか教えてください。
#include<iostream>
using namespace std;
class vehicle{
public:
vehicle(){cout <<"vehicle Construct" << endl;}
virtual ~vehicle(){cout << "vehicle Destroy" << endl;}
};
class bicycle : public vehicle{
public:
bicycle(){cout << "bicycle Construct" << endl;}
virtual ~bicycle(){cout << "bicycle Destroy" << endl;}
};
class motorcar : public vehicle{
public:
motorcar(){cout << "motorcar Construct" << endl;}
virtual ~motorcar(){cout << "motorcar Destroy" << endl;}
};
class motorcycle : public bicycle, public motorcar{
public:
motorcycle(){cout << "motorcycle Construct" << endl;}
~motorcycle(){cout << "motorcycle Destroy" << endl;}
};
int main()
{
motorcycle mymotorcycle;
return 0;
}
出力
vehicle Construct
bicycle Construct
vehicle Construct
motorcar Construct
motorcycle Construct
motorcycle Destroy
motorcar Destroy
vehicle Destroy
bicycle Destroy
vehicle Destroy
目標
vehicle Construct
bicycle Construct
motorcar Construct
motorcycle Construct
motorcycle Destroy
motorcar Destroy
bicycle Destroy
vehicle Destroy