栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++小作业-虚函数--交通工具

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++小作业-虚函数--交通工具

一、问题描述

定义一个交通工具vehicle,将他作为基类派生校车类car,卡车类truck和轮船类boat,定义类并定义虚函数来显示各类信息。

二、代码实现
#include
using  namespace  std;
class  vehicle
{      
protected:
        double  speed; //速度,公里/小时
        int  wheels;   //轮子数
        double  weight;  //重量
public:
        vehicle(double  speed=80,int  wheels=4,double  weight=1000);
        virtual  void  show(void)=0;  
};
vehicle::vehicle(double speed,int wheels,double weight)
{
    this->speed=speed,this->wheels=wheels,this->weight=weight;
}
class  car  :  public  vehicle
{    
		int  passenger_load;
public:
        car(double  speed=80,int  wheels=4,double  weight=1000,int  passenger_load=4);
        virtual  void  show(void);
};
car::car(double  speed,int  wheels,double  weight,int  passenger_load):vehicle(speed,wheels,weight)
{
    this->passenger_load=passenger_load;
}
void  car::show(void)  
{
	cout  <<  "Car  messagen";
	cout<  
	double  rated_load;      //额定载重
    public:
            truck(double  speed=80,int  wheels=4,double  weight=2500,double  rated_load=3000);
            virtual    void  show(void);
};
truck::truck(double  speed,int  wheels,double  weight,double  rated_load):vehicle(speed,wheels,weight)
{
    this->rated_load=rated_load;
}
void truck::show(void)
{
    cout<<"truck messagen";
    cout<  
	char  kind;              //轮船类别,如客轮为'k'
    public:
          boat(double  speed=30,int  wheels=0,double  weight=12000,char  kind='k');
          virtual    void  show(void);
};
boat::boat(double  speed,int  wheels,double  weight,char  kind):vehicle(speed,wheels,weight)
{
    this->kind=kind;
}
void boat::show(void)
{
    cout<<"boat messagen";
    cout<    
	  vehicle  *unicycle;    car  *BMW;
      unicycle  =  new  car;      unicycle  ->show();  
      BMW=  (car  *)  unicycle;      BMW  ->show();  
      delete  unicycle;
      unicycle  =  new  truck;      unicycle  ->show();  
      delete  unicycle;
      unicycle  =  new  boat;      unicycle  ->show();  
      delete  unicycle;
      return  0;
}  


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/883071.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号