4月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;
    }
    
};

 

posted @ 2023-04-21 21:29  kxzzow  阅读(84)  评论(0)    收藏  举报