《面向对象程序设计》第六次作业

1. (程序题)

1、设计人民币类Money,其数据成员为fen(分)、jiao(角)、yuan(元),如有需要可添加其他数据成员。(本题使用了友元,若用Vc6.0则需更改头文件)

重载这个类的加法、减法运算符,实现人民币的加减并通过 输入输出流的重载直接对该类对象进行输入输出;

输入格式

输入在两行中分别给出3个正整数,以空格隔开

输出格式

若结果为正 :输出几yuan几jiao几fen; 若结果为负:只在前面给出一个负号即可;

输入样例

5 6 9

4 8 3

输出样例

10yuan5jiao2fen

0yuan8jiao6fen

#include <iostream>
#include <cmath> // 必须加上这个头文件,因为要用到 abs()
using namespace std;

class Money {
    friend istream& operator>>(istream&, Money&);
    friend ostream& operator<<(ostream&, const Money&);

public:
    Money(int = 0, int = 0, int = 0);
    
    Money operator+(const Money&) const;
    Money operator-(const Money&) const;

private:
    int yuan, jiao, fen;
};

Money::Money(int y, int j, int f) {
    int total_fen = y * 100 + j * 10 + f;
    yuan = total_fen / 100;
    jiao = (total_fen % 100) / 10;
    fen = total_fen % 10;
}

Money Money::operator+(const Money& m2) const {
    int t1 = yuan * 100 + jiao * 10 + fen;
    int t2 = m2.yuan * 100 + m2.jiao * 10 + m2.fen;
    return Money(0, 0, t1 + t2);
}

Money Money::operator-(const Money& m2) const {
    int t1 = yuan * 100 + jiao * 10 + fen;
    int t2 = m2.yuan * 100 + m2.jiao * 10 + m2.fen;
    return Money(0, 0, t1 - t2);
}

istream& operator>>(istream& in, Money& m) {
    int y, j, f;
    in >> y >> j >> f;
    int total_fen = y * 100 + j * 10 + f;
    m.yuan = total_fen / 100;
    m.jiao = (total_fen % 100) / 10;
    m.fen = total_fen % 10;
    return in;
}

ostream& operator<<(ostream& out, const Money& m) {
    // 只有结果为负,才在最前面输出一个负号
    if (m.yuan < 0 || m.jiao < 0 || m.fen < 0) {
        out << "-";
    }
    // 后面的具体金额必须输出,并且用 abs() 去掉它们自带的负号
    out << abs(m.yuan) << "yuan" << abs(m.jiao) << "jiao" << abs(m.fen) << "fen";
    return out;
}

int main() {
    int y1, j1, f1;
    int y2, j2, f2;
    
    cin >> y1 >> j1 >> f1;
    cin >> y2 >> j2 >> f2;
    
    Money m1(y1, j1, f1), m2(y2, j2, f2);
    
    cout << m1 + m2 << endl;
    cout << m1 - m2 << endl;
    
    return 0;
}


第二题

定义一个教师类(Teacher),由教师类派生出讲师(lecturer)、副教授(AssociateProfessor)
教授(Professor)类。教师的工资分别由基本工资和课时费构成。其中副教授额外有交通补贴,
教授额外有房屋补贴,定义虚函数通过多态来计算 教师的工资;
 输入格式
 第一行输入讲师的     工时 基本工资  每小时课时费
 第二行输入副教授的  工时 基本工资  每小时课时费   交通补贴
 第三行输入教授的    工时 基本工资  每小时课时费   房屋补贴
 如果工时为零在职位后加 on vacation 
 输出格式
 每行分别输出对应的提示语及工资
输入样例  
200 3500 50
150 5000 100 1300
80 7000 200  2300
输出样例 
Lecturer salary:13500
AssociateProfessor salary:21300
Professor salary:25300

工时为0,有基础工资和补贴


#include <iostream>
#include <string>
using namespace std;

class Teacher {
public:
    Teacher(double, double, double);
    virtual ~Teacher();
    
    virtual double Salary() const = 0;

    //=0代表纯虚函数
    //派生类声明中 override 关键字,表示该函数是虚函数的重载,C++98可以省略,C++11要求写上
    
    virtual void printSalary(const string&) const = 0;
    virtual void printSalary(const string&) const;
    
protected: 
    double time, basesalary, hsalary;
};

class lecturer : public Teacher {
public:
    lecturer(double, double, double);
    ~lecturer();
    double Salary() const override;
};

class AssociateProfessor : public Teacher {
public:
    AssociateProfessor(double, double, double, double);
    ~AssociateProfessor();
    double Salary() const override;
    
   
private:
    double transpotationSalary;
};

class Professor : public Teacher {
public:
    Professor(double, double, double, double);
    ~Professor();
    double Salary() const override;
    
private:
    double houseSalary;
};



Teacher::Teacher(double t, double b, double h) : time(t), basesalary(b), hsalary(h) {}

Teacher::~Teacher() {}

void Teacher::printSalary(const string& title) const {
    cout << title;
    if (time == 0) {
            // cout << " on vacation"<<" salary:"<<Salary()<<endl;
            cout << " salary on vacation:"<<" "<<Salary()<<endl;
 //         cout << " on vacation"<<" salary:0"<<endl;
//			cout<< " on vacation"<<endl;
         
    } else {
      
        cout << " salary:" << Salary() << endl; 
    }
}

lecturer::lecturer(double t, double b, double h) : Teacher(t, b, h) {}

lecturer::~lecturer() {}

double lecturer::Salary() const {
//  if(time ==0) return basesalary;
//   if(time == 0) return 0;
//   else
    return basesalary + (time * hsalary);
}

AssociateProfessor::AssociateProfessor(double t, double b, double h, double trans) : Teacher(t, b, h), transpotationSalary(trans) {}

AssociateProfessor::~AssociateProfessor() {}

double AssociateProfessor::Salary() const {
//  if(time ==0) return basesalary;
//   if(time == 0) return 0;
//   else
    return basesalary + (time * hsalary) + transpotationSalary;
}

Professor::Professor(double t, double b, double h, double house) : Teacher(t, b, h), houseSalary(house) {}

Professor::~Professor() {}

double Professor::Salary() const {
  //if(time ==0) return basesalary;
//   if(time == 0) return 0;
//   else
    return basesalary + (time * hsalary) + houseSalary;
}

int main() {
    double t, b, h, allowance;


    cin >> t >> b >> h;
    Teacher *p1 = new lecturer(t, b, h);

    cin >> t >> b >> h >> allowance ;
    Teacher *p2 = new AssociateProfessor(t, b, h, allowance);

    cin >> t >> b >> h >> allowance;
    Teacher *p3 = new Professor(t, b, h, allowance);

    p1->printSalary("Lecturer");
    p2->printSalary("AssociateProfessor");
    p3->printSalary("Professor");

    delete p1; 
    delete p2;
    delete p3; 
    
    return 0;
}


#include <iostream>
#include <string>
using namespace std;

class Teacher {
protected:
    string title;
    int hours;
    double baseSalary;
    double hourlyRate;
public:
    Teacher(string t, int h, double b, double r) : title(t), hours(h), baseSalary(b), hourlyRate(r) {}
    
    virtual ~Teacher() {}
    
    virtual double calculateSalary() const {
        return baseSalary + hours * hourlyRate;
    }
    
    string getTitle() const {
        return title;
    }
    
    int getHours() const {
        return hours;
    }
};

class Lecturer : public Teacher {
public:
    Lecturer(int h, double b, double r) : Teacher("Lecturer", h, b, r) {}
    
    ~Lecturer() {}
    
    double calculateSalary() const override {
        return Teacher::calculateSalary();
    }
};

class AssociateProfessor : public Teacher {
private:
    double transportAllowance;
public:
    AssociateProfessor(int h, double b, double r, double t) : Teacher("AssociateProfessor", h, b, r) {
        this->transportAllowance = t;
    }
    
    ~AssociateProfessor() {}
    
    double calculateSalary() const override {
        return Teacher::calculateSalary() + transportAllowance;
    }
};

class Professor : public Teacher {
private:
    double housingAllowance;
public:
    Professor(int h, double b, double r, double ha) : Teacher("Professor", h, b, r) {
        this->housingAllowance = ha;
    }
    
    ~Professor() {}
    
    double calculateSalary() const override {
        return Teacher::calculateSalary() + housingAllowance;
    }
};

void res(Teacher *t) {
    if (t->getHours() == 0) {
        cout << t->getTitle() << " on vacation salary:" << t->calculateSalary() << endl;
    } else {
        cout << t->getTitle() << " salary:" << t->calculateSalary() << endl;
    }
}

int main() {
    int h1, h2, h3;
    double b1, b2, b3;
    double r1, r2, r3;
    double trans, house;

    cin >> h1 >> b1 >> r1;
    cin >> h2 >> b2 >> r2 >> trans;
    cin >> h3 >> b3 >> r3 >> house;

    Teacher *p1 = new Lecturer(h1, b1, r1);
    Teacher *p2 = new AssociateProfessor(h2, b2, r2, trans);
    Teacher *p3 = new Professor(h3, b3, r3, house);

    res(p1);
    res(p2);
    res(p3);

    delete p1;
    delete p2;
    delete p3;

    return 0;
}


第三题

设计Matrix类,通过运算符重载实现矩阵的和与乘 ;
输入格式
第一行输入A矩阵的行r和列c,后r行输入矩阵的元素
输入B矩阵的行r和列c,后r行输入矩阵的元素
输出格式
第一行输出A+B
如果矩阵A和B可以相加输出矩阵的元素,每个元素所占的宽度为4;否则输出Error
输出AB
如果矩阵A和B可以相加输出矩阵的元素,每个元素所占的宽度为4;否则输出Error
 
输入样例
3 3
1 1 1
1 1 1
1 1 1
3 3
1 2 3
1 5 6
1 2 4
输出样例  
A+B
   2   3   4
   2   6   7
   2   3   5
A
B
   3   9  13
   3   9  13
   3   9  13

#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

class Matrix {
private:
    int r, c;
    vector<vector<int>> mat;
    bool isValid;

public:
    Matrix() : r(0), c(0), isValid(false) {}

    Matrix(int rows, int cols) : r(rows), c(cols), isValid(true) {
        mat.assign(r, vector<int>(c, 0));
    }

    friend istream& operator>>(istream& in, Matrix& m) {
        in >> m.r >> m.c;
        m.mat.assign(m.r, vector<int>(m.c));
        m.isValid = true;
        for (int i = 0; i < m.r; ++i) {
            for (int j = 0; j < m.c; ++j) {
                in >> m.mat[i][j];
            }
        }
        return in;
    }

    Matrix operator+(const Matrix& b) const {
        if (this->r != b.r || this->c != b.c) {
            return Matrix();
        }
        
        Matrix res(this->r, this->c);
        for (int i = 0; i < this->r; ++i) {
            for (int j = 0; j < this->c; ++j) {
                res.mat[i][j] = this->mat[i][j] + b.mat[i][j];
            }
        }
        return res;
    }

    Matrix operator*(const Matrix& b) const {
        if (this->c != b.r) {
            return Matrix();
        }
        
        Matrix res(this->r, b.c);
        for (int i = 0; i < this->r; ++i) {
            for (int j = 0; j < b.c; ++j) {
                for (int k = 0; k < this->c; ++k) {
                    res.mat[i][j] += this->mat[i][k] * b.mat[k][j];
                }
            }
        }
        return res;
    }

    void print() const {
        if (!isValid) {
            cout << "Error\n";
            return;
        }
        for (int i = 0; i < r; ++i) {
            for (int j = 0; j < c; ++j) {
                cout << setw(4) << mat[i][j];
            }
            cout << "\n";
        }
    }
};

int main() {
    Matrix A, B;
    
    cin >> A;
    cin >> B;
    
    cout << "A+B\n";
    (A + B).print();
    
    cout << "A*B\n";
    (A * B).print();
    
    return 0;
}

第四题

各位面向对象的小伙伴们,在学习了面向对象的核心概念——类的封装、继承、多态之后,租车系统开始营运了。
 
请你充分利用面向对象思想,为公司解决智能租车问题,根据客户选定的车型和租车天数,来计算租车费用,最大载客人数,最大载载重量。
 
公司现有三种车型(客车、皮卡车、货车),每种车都有租金的属性;其中:客车只能载人,货车只能载货,皮卡车是客货两用车,即可以载人,也可以载货。本题建议使用基类数组指针调用虚函数实现本题功能。
 
下面是租车公司的可用车型、容量及价目表:
 
序号     名称            载客量        载货量        租金
                        (人)        (吨)      (元/天)
  1          A            5                        800
  2          B            5                        400
  3          C            5                        800
  4          D            51                       1300
  5          E            55                       1500
  6          F            5           0.45         500
  7          G            5            2.0         450
  8          H                          3          200
  9          I                          25         1500
 10          J                          35         2000
要求:根据客户输入的所租车型的序号及天数,计算所能乘载的总人数、货物总数量及租车费用总金额。
 
Input
 
首行是一个整数:代表要不要租车 1——要租车(程序继续),0——不租车(程序结束);
第二行是一个整数,代表要租车的数量N;
 
接下来是N行数据,每行2个整数m和n,其中:m表示要租车的编号,n表示租用该车型的天数。
 
Output
 
若成功租车,则输出一行数据,数据间有一个空格,含义为:
载客总人数 载货总重量(保留2位小数) 租车金额(整数)(其中载客总人数总数量为天数乘与该车的载客量或载货量)
 
若不租车,则输出0 0.00 0
Sample
Input :
1
2
1 1
2 2
Output :
15 0.00 1600
input 1
0



#include <iostream>
#include <cstdio>

using namespace std;

class Vehicle {
protected:
    int rent;
public:
    Vehicle(int r) : rent(r) {}
    virtual ~Vehicle() {}
    
    virtual int getPassenger() const { return 0; }
    virtual double getCargo() const { return 0.0; }
    virtual int getRent() const { return rent; }
};

class PassengerCar : public Vehicle {
private:
    int passengerCapacity;
public:
    PassengerCar(int p, int r) : Vehicle(r), passengerCapacity(p) {}
    int getPassenger() const override { return passengerCapacity; }
};

class Truck : public Vehicle {
private:
    double cargoCapacity;
public:
    Truck(double c, int r) : Vehicle(r), cargoCapacity(c) {}
    double getCargo() const override { return cargoCapacity; }
};

class Pickup : public Vehicle {
private:
    int passengerCapacity;
    double cargoCapacity;
public:
    Pickup(int p, double c, int r) : Vehicle(r), passengerCapacity(p), cargoCapacity(c) {}
    int getPassenger() const override { return passengerCapacity; }
    double getCargo() const override { return cargoCapacity; }
};

int main() {
    int isRenting;
    
    if (!(cin >> isRenting)) return 0;

    if (isRenting == 0) {
        printf("0 0.00 0\n");
        return 0;
    }

    int n;
    cin >> n;

    Vehicle* cars[11];
    cars[1]  = new PassengerCar(5, 800);
    cars[2]  = new PassengerCar(5, 400);
    cars[3]  = new PassengerCar(5, 800);
    cars[4]  = new PassengerCar(51, 1300);
    cars[5]  = new PassengerCar(55, 1500);
    cars[6]  = new Pickup(5, 0.45, 500);
    cars[7]  = new Pickup(5, 2.0, 450);
    cars[8]  = new Truck(3.0, 200);
    cars[9]  = new Truck(25.0, 1500);
    cars[10] = new Truck(35.0, 2000);

    long long totalPassenger = 0;
    double totalCargo = 0.0;
    long long totalRent = 0;

    for (int i = 0; i < n; ++i) {
        int m, days;
        cin >> m >> days;
        if (m >= 1 && m <= 10) {
            totalPassenger += cars[m]->getPassenger() * days;
            totalCargo += cars[m]->getCargo() * days;
            totalRent += cars[m]->getRent() * days;
        }
    }

    printf("%lld %.2f %lld\n", totalPassenger, totalCargo, totalRent);

    for (int i = 1; i <= 10; ++i) {
        delete cars[i];
    }

    return 0;
}

多态

几何形体处理程序: 输入3不同形状的几何形体的参数,输出他们的面积和周长,注意要指名几何图形的形状。

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
//类中声明,类外定义版 
//override在声明时要写,定义时不要写
//三种数据成员初始化的方法
//如果基类有数据成员,要记得再派生类中对基类数据成员初始化
class Shapes {
public:
    string name; // 将属性设为public,这样就不需要get函数了
    Shapes(string n) : name(n) {} //构造函数不能声明为虚函数
    virtual ~Shapes();
    virtual double Area() const = 0;
    virtual double Perimeter() const = 0;
    //纯虚函数派生类必须重载 
};

class Rectangle : public Shapes {
public:
    Rectangle(double, double);
    ~Rectangle();
    double Area() const override;
    double Perimeter() const override;
private:
    double w, d;
};

class Circle : public Shapes {
public:
    Circle(double);
    ~Circle();
    double Area() const override;
    double Perimeter() const override;
private:
    double radius;
};

class Triangle : public Shapes {
public:
    Triangle(double, double, double);
    ~Triangle();
    double Area() const override;
    double Perimeter() const override;
private:
    double a, b, c; 
};

Shapes::~Shapes() {}

Rectangle::Rectangle(double w, double h) : Shapes("Rectangle"), w(w), d(h) {}
Rectangle::~Rectangle() {}
double Rectangle::Area() const {
    return w * d; 
}
double Rectangle::Perimeter() const {
    return (w + d) * 2;
}

Circle::Circle(double r1) : Shapes("Circle") {
    radius = r1; 
}
Circle::~Circle() {}
double Circle::Area() const {
    return 3.14 * radius * radius;
}
double Circle::Perimeter() const {
    return 2 * 3.14 * this->radius;
}

Triangle::Triangle(double a, double b, double c) : Shapes("Triangle") {
    this->a = a;
    this->b = b;
    this->c = c;
}
Triangle::~Triangle() {}
double Triangle::Area() const {
    double p = (a + b + c) / 2.0;
    return sqrt(p * (p - a) * (p - b) * (p - c));
}
double Triangle::Perimeter() const {
    return a + b + c;
}

void ans(const Shapes & s) {
    cout << "Name:" << s.name << endl; // 直接访问public成员
    cout << "Area:" << s.Area() << endl;
    cout << "Perimeter:" << s.Perimeter() << endl;
}

void res(Shapes * s) {
    cout << "Name:" << s->name << endl; 
    cout << "Area:" << s->Area() << endl;
    cout << "Perimeter:" << s->Perimeter() << endl;
}

int main() {
    Rectangle rec1(2, 3);
    Circle c1(4);
    Triangle t1(3, 4, 5);
//  1.
    ans(rec1);
    ans(c1);
    ans(t1);
//  2.
    Rectangle *rec2 = new Rectangle(2, 3);
    Circle *c2 = new Circle(4); 
    Triangle *t2 = new Triangle(3, 4, 5);
    res(rec2);
    res(c2);
    res(t2);
    delete rec2;
    delete c2;
    delete t2;
//  3. 
    Shapes *p1 = new Rectangle(2, 3);
    Shapes *p2 = new Circle(4); 
    Shapes *p3 = new Triangle(3, 4, 5);
    res(p1);
    res(p2);
    res(p3); 
    delete p1;
    delete p2;
    delete p3;

    return 0;
}

posted @ 2026-06-03 12:09  叶臧  阅读(13)  评论(0)    收藏  举报