4.22打卡

复制代码
#include<iostream>
#include <string>
using namespace std;
class Vehicle
{
protected:
    string NO;//编号
public:
    Vehicle(string n){        NO = n;  }
    virtual int fee()=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;
};


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 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 @   yblll  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示