首页 > 其他分享 >4月21

4月21

时间:2023-04-21 21:34:16浏览次数:36  
标签:guest weight no int Vehicle public 21

问题:

现在要开发一个系统,管理对多种汽车的收费工作。
给出下面的一个基类框架

class Vehicle

{

protected:

string NO;
 

public:

Vehicle(string n){

    NO = n;

}
 

virtual int fee()=0;//计算应收费用

};

以Vehicle为基类,构建出Car、Truck和Bus三个类。

Car的收费公式为: 载客数*8+重量*2

Truck的收费公式为:重量*5

Bus的收费公式为: 载客数*3

生成上述类并编写主函数

主函数根据输入的信息,相应建立Car,Truck或Bus类对象,对于Car给出载客数和重量,Truck给出重量,Bus给出载客数。假设载客数和重量均为整数

输入格式:第一行输入测试用例数。接着每个测试用例占一行,每行给出汽车的基本信息,第一个数据为当前汽车的类型:1为car,2为Truck,3为Bus。第二个数据为它的编号,接下来Car是载客数和重量,Truck要求输入重量,Bus要求输入载客数。

要求输出各车的编号和收费。

裁判测试程序样例:

 
#include<iostream>
#include <string>
using namespace std;
class Vehicle
{
protected:
    string NO;//编号
public:
    Vehicle(string n){        NO = n;  }
    virtual int fee()=0;//计算应收费用
};

/* 请在这里填写答案 */

int main()
{
    Car c("",0,0);
    Truck t("",0);
    Bus b("",0);
    int i, repeat, ty, weight, guest;
    string no;
    cin>>repeat;
    for(i=0;i<repeat;i++){
        cin>>ty>>no;
        switch(ty){
            case 1: cin>>guest>>weight; c=Car(no, guest, weight); cout<<no<<' '<<c.fee()<<endl; break;
            case 2: cin>>weight; t=Truck(no, weight); cout<<no<<' '<<t.fee()<<endl; break;
            case 3: cin>>guest; b=Bus(no, guest); cout<<no<<' '<<b.fee()<<endl; break;
        }
    }
    return 0;
}

class Car:public Vehicle

{

public:

    Car(string no,int guest,int weight):Vehicle(no)

// 不知道为啥要加一个:Vehicle(no),哪个大神可以讲解一下

    {

        m_no=no;

         m_guest=guest;

         m_weight=weight;

    }

    virtual int fee()

    {

        return (m_guest*8+m_weight*2);

    }

private:

    string m_no;

    int m_guest,m_weight;

};

class Truck:public Vehicle

{

public:

    Truck(string no,int weight):Vehicle(no)

    {

 

        m_no=no;

        m_weight=weight;

 

    }

    virtual int fee()

    {

        return (m_weight*5);

    }

private:

    string m_no;

    int m_weight;

};

class Bus :public Vehicle

{

public:

    Bus(string no,int guest):Vehicle(no)

    {

 

        m_no=no;

        m_guest=guest;

 

    }

    virtual int fee()

    {

        return (m_guest*3);

    }

private:

 

    string m_no;

    int m_guest;

};

问题:

请结合如图所示的继承关系设计Shape、Circle以及Rectangle类,使得下述代码可以正确计算并输出矩形和圆的面积。

提示:Shape的析构以及area()函数都应为虚函数。

裁判测试程序样例:

 
//Project - Shapes
#include <iostream>
using namespace std;

//在此处定义Shape, Cirlce及Rectangle类

int main(){
    Shape* shapes[2];
    int w, h;
    cin >> w >> h;   //输入矩形的长宽
    shapes[0] = new Rectangle(w,h);
    float r;   //输入圆的半径
    cin >> r;
    shapes[1] = new Circle(0,0,2);  //圆心在(0,0),半径为r的圆
    printf("Area of rectangle:%.2f\n",shapes[0]->area());
    printf("Area of circle:%.2f\n",shapes[1]->area());
    for (auto i=0;i<2;i++)
        delete shapes[i];
    return 0;
}
 

输入样例:

3 2
2
 

输出样例:

Area of rectangle:6.00
Area of circle:12.57

class Shape {
    public:
  Shape(){
      
  }  
  virtual ~Shape(){
  }
  virtual float area() = 0;
};

class Circle:public Shape{
    private: 
        int x, y;
        float radius;
    public:
        Circle(int x, int y,float radius){
            this -> x = x; this -> y = y; this -> radius = radius; 
        }
        ~Circle(){}
        
        float area(){
            return 3.1415926f * radius * radius;
        }
};
class Rectangle:public Shape {
private:
    int width, height;
public:
    Rectangle(int w, int h){
        width = w; height = h;
    }
    
    ~Rectangle(){}
    
    float area(){
        return width * height;
    }
    
};

 

标签:guest,weight,no,int,Vehicle,public,21
From: https://www.cnblogs.com/kongxiangzeng/p/17341881.html

相关文章

  • 2023 4 21
     注意double类型相运算的也要是double类型如上例若把a变量更改为int类型则会导致结果出错 #include<iostream>#include<string>usingnamespacestd;classpeople{protected:intage;stringname;public:people(){}people(inta,stringstr){......
  • 每日总结2023-04-21
    今天将补货的历史记录做出来了。补货历史界面: 修改了补货的界面,调整了预约时间 ......
  • 每日总结 4.21
    今天进行了供货商的页面设计,对于需求方发送的商品信息进行数据处理显示需要付款的金额,对补货按钮进行了操作,进行了数据库的更新。packageres;importjava.io.IOException;importjava.io.PrintWriter;importjakarta.servlet.ServletException;importjakarta.servlet.an......
  • 2023/4/21
    今日站立会议,进行了对于每个端的细节设置分析,对于数据的显示进行改进,对补货进行操作,对于补货后进行改变数据进行处理,显示已经补货。 ......
  • C/C++课程信息管理系统[2023-04-21]
    C/C++课程信息管理系统[2023-04-21]综合应用所学C语言知识,设计完成一个软件工程专业课程信息管理系统。本系统拟实现以下功能:【数据文件】课程信息数据文件。每门课程的信息包含:课程编号,课程名称,理论课时实验课时、学分、开课学期(8个学期)、课程性质《分为必修、选修、限......
  • 建民打卡日记4.21
    一、问题描述一对兔子从出生后三个月起每个月生一对兔子,小兔子三个月后每月生一对兔子,依次类推,假设所有兔子都不死,三十个月内每月兔子总量?二、设计流程1.设置前两个月的初值a,b2.将前两个月的总值赋给下个月c,并依次赋给a,b下个月的值,实现迭代3.输出c三、流程图设计四、代码......
  • 4.21打卡
     2.设计思路①依次列举出所有0~9的两位相同数字②在第一步的前提下依次列举出0~9的两位相同数字③将第一第二步中的四个数字组成的数进行开根,判断结果是否为整数后输出结果 3.程序流程图 4、代码实现#include<bits/stdc++.h>usingnamespacestd;intmain(){......
  • 2023年4月21日周五
    计划一定解决修改状态的功能删减代码学习新东西英语没看完,晚上看,公司就看专业课的书就好执行09点31分  开始工作13点38分  下午开始15点50分  最后一点记录问题想法前端,js,controller,我先看他之前的项目看有无可模仿的地方去网上搜,这部分知识问剩一个......
  • 4.21总结
    --查询所有数据SELECT*FROMstu;--给指定列添加数据INSERTINTO表名(列名1,列名2,...)VALUES(值1,值2,...);INSERTintostu(id,name,sex,birthday,score,email,tel,status)values(3,'李四','男','2000-11-11',88.88,'qq.com','18100000000�......
  • 4.21
    #include<stdio.h>voidmain(){longfib1=1,fib2=1,fib;inti;printf("%12ld%12ld",fib1,fib2);for(i=3;i<=30;i++){fib=fib1+fib2;printf("%12d",fib);if(i%4==0)printf("\n");fib2=fib1;fib1=fib;}}......