自由思维∝的博客

开开心心的去做该做的事,偶尔沮丧但不退却。

面向对象编程技术(作业题四)

#include <iostream>

using namespace std;

class Vehicle
{
protected:
 int m_nWheels;  // 车轮个数
 double m_nWeight; // 车重

public:
 Vehicle(int nWheels, double nWeight)
  : m_nWheels(nWheels)
  , m_nWeight(nWeight)
 {
 }

 // 输出信息
 virtual void OutputInfo()
 {
  cout<<"车轮个数: "<<m_nWheels<<endl;
  cout<<"车重: "<<m_nWeight<<endl;
 }
};

class Car : public Vehicle
{
private:
 int m_nPassengers; // 载人数

public:
 Car(int nWheels, int nWeight, int nPassengers)
  : Vehicle(nWheels, nWeight)
  , m_nPassengers(nPassengers)
 {
 }

 // 输出信息
 virtual void OutputInfo()
 {
  Vehicle::OutputInfo();
  cout<<"载人数: "<<m_nPassengers<<endl;
 }
};

class Truck : public Vehicle
{
private:
 int m_nPassengers; // 载人数
 double m_nPayload; // 载重量

public:
 Truck(int nWheels, int nWeight, int nPassengers, double nPayload)
  : Vehicle(nWheels, nWeight)
  , m_nPassengers(nPassengers)
  , m_nPayload(nPayload)
 {
 }

 // 输出信息
 virtual void OutputInfo()
 {
  Vehicle::OutputInfo();
  cout<<"载人数: "<<m_nPassengers<<endl;
  cout<<"载重量: "<<m_nPayload<<endl;
 }
};


///////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
 Vehicle* pVehicle = NULL;
 Vehicle vehicle(4, 2);
 Car car(4, 3, 5);
 Truck truck(6, 5, 25, 5);

 // 输出信息
 pVehicle = &vehicle;
 pVehicle->OutputInfo();
 cout<<endl;

 pVehicle = &car;
 pVehicle->OutputInfo();
 cout<<endl;

 pVehicle = &truck;
 pVehicle->OutputInfo();
 cout<<endl;

 return 0;
}

阅读更多
个人分类: 工程硕士
上一篇面向对象编程技术(作业题三)
下一篇No0 开发工具 - GNU Toolchain,MSVC
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭