内容纲要

题目1:

编写一个汽车类Vehicle,包含轮子个数wheelNum、车重weight。
小车类Car是Vehicle的派生类,包含载重人数passengerLoad。
卡车类Truck是Car的派生类,包含载重重量payLoad。每个类都有相关数据的输出方法。

#include <iostream>

using namespace std;

class Vehicle {
public:
    int wheelNum;
    double weight;
    Vehicle() { wheelNum = 0, weight = 0.0; };
    Vehicle(int _wheelNum, double _weight);
    ~Vehicle();
    void SetOut1() {
        cout << "车轮数:" << wheelNum << endl;
        cout << "车重:" << weight << endl;
    }
};

class Car :public Vehicle {
public:
    int passengerLoad;
    Car(Vehicle &,int);
    Car() { passengerLoad = 0; };
    ~Car();
    void SetOut2() {
        cout << "载重人数:" << passengerLoad << endl;
    }
};

class Truck :public Car {
public:
    double payLoad;
    Truck(Car &c,double);
    ~Truck();
    void SetOut3() {
        cout << "载重重量:" << payLoad << endl;
    }
};

Vehicle::Vehicle(int _wheelNum, double _weight)
{
    wheelNum = _wheelNum;
    weight = _weight;
}

Vehicle::~Vehicle()
{
}

Car::Car(Vehicle &v, int _passengerLoad)
{
    wheelNum = v.wheelNum;
    weight = v.weight;
    passengerLoad = _passengerLoad;
}

Car::~Car()
{
}

Truck::Truck(Car &c,double _payLoad)
{
    wheelNum = c.wheelNum;
    weight = c.weight;
    passengerLoad = c.passengerLoad;
    payLoad = _payLoad;
}

Truck::~Truck()
{
}

void main() {
    Vehicle v1(5, 5.2);
    Car c1(v1,5);
    Truck t1(c1,10.5);
    t1.SetOut1();
    t1.SetOut2();
    t1.SetOut3();
    system("pause");
}

题目2

设计一个圆类Circle(半径)和桌子类Table(高度、颜色),另设计一个圆桌类RoundTable,他是前两个类的派生的,要求输出圆桌的高度,面积和颜色的信息。

#include <iostream>

using namespace std;

enum Colour
{
    red,
    yellow,
    blue,
    green,
    young,
    powder,
    purple
};

class Circle {
public:
    double r;
    Circle() { r = 0.0; }
    double Area(const double r) {
        return r * r*3.14;
    }
    Circle(double _r) { r = _r; }
};

class Table {
public:
    double h;
    int colour;
    Table() { h = 0.0, colour = red; }
    Table(double _h, int _c) {
        h = _h, colour = _c % 7;
    }
    void SetColour(int _c) {
        switch (_c)
        {
        case red: cout << "red" << endl;
            break;
        case yellow:cout << "yellow" << endl;
            break;
        case blue:cout << "blue" << endl;
            break;
        case green:cout << "green" << endl;
        break;
        case young:cout << "young" << endl;
        break;
        case powder:cout << "powder" << endl;
        break;
        case purple:cout << "purple" << endl;
        break;
        default:
            break;
        }
    }
};

class RoundTable :public Circle,public Table{
public:
    RoundTable(const Table &t,const Circle &c) {
        h = t.h;
        colour = t.colour;
        r = c.r;
    }
    void SetOut() {
        cout << "面积:" << Area(r) << endl;
        cout << "颜色:";
        SetColour(colour) ;
        cout << "高度:" << h << endl;
    }
};

void main() {
    Circle c1(1.5);
    Table t1(1.2, 0);
    RoundTable rt1(t1, c1);
    rt1.SetOut();
    system("pause");
}

发表评论