求教C++!!!

来源:百度知道 编辑:UC知道 时间:2024/05/10 10:08:49
求教~~编写一个C++程序,定义一个汽车类vehicle,它具有一个需传递参数的构造函数,类中的数据成员包括车轮个数和车的重量,并放到保护段中;定义轿车car是汽车类vehicle的私有派生类,其中包含载人数;再定义卡车类truck是汽车类vehicle的私有派生类,其中包含载人数和载重量。每个类都有相应的数据输出。

class Vehicle
{
protected:
Vehicle( unsigned int uWheel, unsigned int uWeight ) : m_uWheel( uWheel ), m_uWeight( uWeight )
{
}

public:
virtual ostream& print( ostream& o ) const
{
o << "Wheel = " << m_uWheel << endl;
o << "Weight = " << m_uWeight << endl;
return o;
}

virtual ~Vehicle()
{
}

protected:
unsigned int m_uWheel;
unsigned int m_uWeight;
};

ostream& operator<<( ostream& o, const Vehicle& vehicle )
{
return vehicle.print( o );
}

class Car : public Vehicle
{
public:
Car( unsigned int uWheel, unsigned int uWeight, unsigned int uPerson ) : Vehicle( uWheel, uWeight ), m_uPerson( uPerson )
{
}

virtual ostream& print( ostream& o ) const
{
o << "Car :" << endl;
Vehicle::print