首页 > 其他分享 >课后习题

课后习题

时间:2023-04-17 20:45:39浏览次数:34  
标签:int Bus Vehicle Truck 课后 Car 习题 public

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

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要求输入载客数。

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

设计思路:

1.设计一个基类Vechile。

2.由基类衍生出子类Car,Truck,Bus.Car构造函数包括载客数和重量,Truck的构造函数包括重量,Bus的构造函数包括载客数。

3.在Car中返回载客数*8+重量*2,在Truck中返回重量*5,在Bus中返回载客数*3

 代码实现:

#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
{
private:
    int guest, weight;
public:
    Car(string a, int b, int c) :Vehicle(a)
    {
        guest = b;
        weight = c;
    }
    virtual int fee()
    {
        return guest * 8 + weight * 2;
    }

};
class Truck :public Vehicle
{
private:
    int  weight;
public:
    Truck(string a, int c) :Vehicle(a)
    {

        weight = c;
    }
    virtual int fee()
    {
        return weight * 5;
    }

};
class Bus :public Vehicle
{
private:
    int guest;
public:
    Bus(string a, int b) :Vehicle(a)
    {

        guest = b;

    }
    virtual int fee()
    {
        return guest * 3;
    }

};

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

image.png

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

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

设计思路:1.设计一个基类Shape包括空构造函数和析构函数和area函数。

2.由基类衍生出子类Circle,Rectangle.Circle构造函数包括构造函数(xyradius)析构函数和返回area的函数π*radius*radius,Rectangle的构造函数包括wh,析构函数和area(w*h)

 代码实现:

#include <iostream>
using namespace std;

class Shape
{
private:

public:
    Shape() { }
    ~Shape() {}
    virtual float area() = 0;
};
class Circle :public Shape
{
private:
    int x, y;
    float radius;
public:
    Circle(int c, int d, float b)
    {
        radius = b;
        x = c;
        y = d;
    }
    virtual float area()
    {
        return 3.1415 * radius * radius;
    }
};
class Rectangle :public Shape
{
private:
    int x, y;
public:
    Rectangle(int c, int d)
    {

        x = c;
        y = d;
    }
    virtual float area()
    {
        return x * y;
    }
};

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

如本章开篇所述,当小学里的上课铃响之后,学生(Student)、教师(Teacher)和校长(Principal)会对同一个消息表现出不同的行为。请设计Person、Student、Teacher以及Principal类,合理安排他们之间的继承关系并将所有类的bellRing()及析构函数设计为虚函数,使得下述代码可以正常执行并产生期望的执行结果。

设计思路:

1.基类Person包括虚析构函数和虚bellRing函数。

2设计子类Student,teacherPrincipal类包括虚析构函数和虚bellRing函数。其中输出内容不同。

 

#include <iostream>
using namespace std;

class Person
{
public:
    virtual void bellRing()
    {

    }
    virtual  ~Person()
    {

    }
};
class Student :public Person
{
public:
    virtual void bellRing()
    {
        cout << "I am a student learning in classroom." << endl;
    }
    virtual ~Student()
    {
        cout << "A student object destroyed." << endl;
    }
};
class Teacher :public Person
{
public:
    virtual void bellRing()
    {
        cout << "I am a teacher teaching in classroom." << endl;
    }
    virtual ~Teacher()
    {
        cout << "A teacher object destroyed." << endl;
    }
};
class Principal :public Person
{
public:
    virtual void bellRing()
    {
        cout << "I am the principal inspecting in campus." << endl;
    }
    virtual ~Principal()
    {
        cout << "A principal object destroyed.";
    }
};

int main() {
    cout << "School bell rings..." << endl;
    Person* persons[3] = { new Student(),new Teacher(),new Principal() };
    persons[0]->bellRing();
    persons[1]->bellRing();
    persons[2]->bellRing();
    for (auto i = 0; i < 3; i++)
        delete persons[i];
    return 0;
}

 

标签:int,Bus,Vehicle,Truck,课后,Car,习题,public
From: https://www.cnblogs.com/xuechenhao173/p/17327431.html

相关文章

  • 编程打卡:C语言趣味编程习题做
    编程打卡:C语言趣味编程习题做数制转换问题描述给定一个M进制的数x,实现对x向任意非M进制的数的转换。设计思路输入M进制的数x,将x转换为十进制数,再将十进制数转换为任意非M进制的数。流程图graphA["开始"]-->B["输入M进制的数x"]-->C["将x转换为十进制数"]-->D["将十进......
  • 记录-js基础练习题
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助隔行换色(%):window.onload=function(){varaLi=document.getElementsByTagName('li');for(vari=0;i<aLi.length;i++){if(i%2==1){ aLi[i].style.background='#bfa';......
  • 4.17 离散化习题
    不理解啊不理解,找一堆和离散化没什么关系,中间最多sort一下的题就叫离散化练习题,打着离散化的牌子实则是一堆数据结构,意义何在?不懂啊不懂。今天只贴一道我比较喜欢的题,也是感觉唯一一道可做题。ACWing 2014.岛  传送门思路非常的简单(但其实还是看了题解....)。考虑枚举怎么......
  • 平面向量习题|低阶
    前言相关链接平面向量习题|高阶;典例剖析【2019高一期末考试】平行四边形\(ABCD\)中,\(AB=3\),\(AD=2\),\(\angleBAD=60^{\circ}\),若\(\overrightarrow{AE}=\lambda\overrightarrow{AB}+\overrightarrow{AD}\),且\(BD\perpAE\),则\(\lambda\)的值为【】$A.\cfrac{1}{6}$$B.\cf......
  • pta程序设计类实验辅助教学平台-练习题
    定义抽象基类Shape,由它派生出五个派生类:Circle(圆形)、Square(正方形)、Rectangle(长方形)、Trapezoid(梯形)和Triangle(三角形),用虚函数分别计算各种图形的面积,并求出它们的和。要求用基类指针数组。使它的每一个元素指向一个派生类的对象。PI=3.1415926。1#include<iostream>2#......
  • 鼎利杯练习题
    第一题moves=input()x,y=0,0formoveinmoves:ifmove=="L":x-=1elifmove=="R":x+=1elifmove=="U":y+=1elifmove=="D":y-=1ifx==0andy==......
  • C++课本第三章课后习题 3-7
    完成函数,参数为两个unsigned short int 型数,返回值为第一个参数除以第二个参数的结果,数据类型为short int;如果第二个参数为0,则返回值为一1。在主程序中实现输入输出。#include<iostream>usingnamespacestd;shortintnumber(unsignedshortintx,unsi......
  • 第二章部分习题
    用穷举法找出1~100中的质数代码:usingnamespacestd;intmain(){intk=1;for(inti=1;i<=99;i++){k++;inta=0;for(intj=2;j<=i/2;j++){if(k%j==0){......
  • Java面向对象习题接口篇
    题目一:按如下要求编写Java程序:(1)定义接口A,里面包含值为3.14的常量PI和抽象方法doublearea()。(2)定义接口B,里面包含抽象方法voidsetColor(Stringc)。(3)定义接口C,该接口继承了接口A和B,里面包含抽象方法voidvolume()。(4)定义圆柱体类Cylinder实现接口C,该类中包含三个成员变量:底......
  • 编程打卡:C语言趣味编程习题做
    编程打卡:C语言趣味编程习题做百钱百鸡问题问题描述一只Cock卖5钱,Hen卖3钱,chicken卖0.33333333钱,真正工作的时候不要用浮点数存钱啊笨蛋!然后100钱买了100只鸡,问各买了多少只。设计思路解不定方程,穷举。两层循环遍历各种鸡的个数,第三种鸡可以直接相减得出结果,然后判......