关闭

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

标签: 作业编程iostreamclassincludenull
867人阅读 评论(0) 收藏 举报
分类:

#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;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:80440次
    • 积分:1115
    • 等级:
    • 排名:千里之外
    • 原创:35篇
    • 转载:14篇
    • 译文:0篇
    • 评论:33条
    最新评论