当前位置: 首页 > news >正文

【C++程序设计实践】实验十一

1.【描述】
①声明并实现一个名为Vehicle的基类,表示汽车。Vehicle类包括:
int类型的私有数据成员numberOfDoors,表示车门数量。
int类型的私有数据成员numberOfCylinders,表示气缸数量。
string类型的私有数据成员color,表示汽车颜色。
double类型的私有数据成员fuelLevel,表示油位,即燃油数量。
int类型的私有数据成员transmissionType,表示变速箱类型,即0表示手动、1表示自动。
string类型的私有数据成员className,表示汽车类别。
有参构造函数,将车门数量、气缸数量、汽车颜色、燃油数量、变速箱类型设置为给定的参数。
访问器函数getNumberOfDoors、getNumberOfCylinders、getColor、getFuelLevel、getTransmissionType、getClassNme,分别用于访问车门数量、气缸数量、汽车颜色、燃油数量、变速箱类型、汽车类别。
更改器函数setColor、setFuelLevel、setClassNme,分别用于更改汽车颜色、燃油数量、汽车类别。
重载流插入运算符<<,输出相关信息。
②从Vehicle类派生出Taxi类,表示出租车。Taxi类有数据成员customers(是否载客,bool类型)以及有参构造函数,将车门数量、气缸数量、汽车颜色、燃油数量、变速箱类型、是否载客设置为给定的参数;访问器/更改器函数hasCustomers和setCustomers;重载流插入运算符<<,输出相关信息。
③从Vehicle类派生出Truck类,表示卡车。Truck类有数据成员cargo(是否载货,bool类型)以及有参构造函数,将车门数量、气缸数量、汽车颜色、燃油数量、变速箱类型、是否载货设置为给定的参数;访问器/更改器函数hasCargo和setCargo;重载流插入运算符<<,输出相关信息。
【输入】
输入车门数量、气缸数量,颜色、燃油数量、变速箱类型。
【输出】
见【输出示例】

【输入示例】
2 6 red 50 1
4 6 yellow 60 0
2 16 black 100 0

【输出示例】
Vehicle
Number of doors:2
Number of cylinders:6
Transmission type:Automatic
Color:red
Fuel level:50
Taxi
Number of doors:4
Number of cylinders:6
Transmission type:Manual
Color:yellow
Fuel level:60
Has no passengers
Truck
Number of doors:2
Number of cylinders:16
Transmission type:Manual
Color:black
Fuel level:100
Is carrying cargo

Code:

#include <iostream>
#include <string>
using namespace std;/* 请在此处分别编写Vehicle、Taxi和Truck类 */
class Vehicle {public:Vehicle(int doors, int cylinders, string color, double fuel, int transmission);int getNumberOfDoors() const;int getNumberOfCylinders() const;string getColor() const;double getFuelLevel() const;int getTransmissionType() const;string getClassName() const;void setColor(string color);void setFuelLevel(double fuel);void setClassName(string className);private:int numberOfDoors; // 车门数量int numberOfCylinders;// 气缸数量string color; // 汽车颜色double fuelLevel; // 油位int transmissionType; // 变速箱类型string className; // 汽车类别friend ostream &operator<<(ostream &out, const Vehicle &v);
};Vehicle::Vehicle(int doors, int cylinders, string color, double fuel, int transmission) {numberOfDoors = doors;numberOfCylinders = cylinders;this->color = color;fuelLevel = fuel;transmissionType = transmission;className = "Vehicle";
}int Vehicle::getNumberOfDoors() const {return numberOfDoors;
}int Vehicle::getNumberOfCylinders() const {return numberOfCylinders;
}string Vehicle::getColor() const {return color;
}double Vehicle::getFuelLevel() const {return fuelLevel;
}int Vehicle::getTransmissionType() const {return transmissionType;
}string Vehicle::getClassName() const {return className;
}void Vehicle::setColor(string color) {this->color = color;
}void Vehicle::setFuelLevel(double fuel) {fuelLevel = fuel;
}void Vehicle::setClassName(string className) {this->className = className;
}ostream &operator<<(ostream &out, const Vehicle &v) {string msg;out << v.className << endl<< "Number of doors:" << v.numberOfDoors << endl<< "Number of cylinders:" << v.numberOfCylinders << endl;if (v.transmissionType == 0)msg = "Manual";elsemsg = "Automatic";out << "Transmission type:" << msg << endl<< "Color:" << v.color << endl<< "Fuel level:" << v.fuelLevel << endl;return out;
}class Taxi: public Vehicle {public:Taxi(int doors, int cylinders, string color, double fuel, int transmission, bool customers);void setCustomers(bool customers);bool hasCustomers() const;private:bool customers;friend ostream &operator<<(ostream &out, const Taxi &t);
};
Taxi::Taxi(int doors, int cylinders, string color, double fuel, int transmission, bool customers): Vehicle(doors, cylinders, color, fuel, transmission) {this->customers = customers;setClassName("Taxi");
}void Taxi::setCustomers(bool customers) {this->customers = customers;
}bool Taxi::hasCustomers() const {return customers;
}ostream &operator<<(ostream &out, const Taxi &t) {string msg;out << t.getClassName() << endl<< "Number of doors:" << t.getNumberOfDoors() << endl<< "Number of cylinders:" << t.getNumberOfCylinders() << endl;if (t.getTransmissionType() == 0)msg = "Manual";elsemsg = "Automatic";out << "Transmission type:" << msg << endl<< "Color:" << t.getColor() << endl<< "Fuel level:" << t.getFuelLevel() << endl;if (t.hasCustomers())msg = "Has passengers";elsemsg = "Has no passengers";out << msg << endl;return out;
}class Truck: public Vehicle {public:Truck(int doors, int cylinders, string color, double fuel, int transmission, bool cargo);void setCargo(bool cargo);bool hasCargo() const;private:bool cargo;friend ostream &operator<<(ostream &out, const Truck &t);
};
Truck::Truck(int doors, int cylinders, string color, double fuel, int transmission, bool cargo): Vehicle(doors, cylinders, color, fuel, transmission) {this->cargo = cargo;setClassName("Truck");
}void Truck::setCargo(bool cargo) {this->cargo = cargo;
}bool Truck::hasCargo() const {return cargo;
}ostream &operator<<(ostream &out, const Truck &t) {string msg;out << t.getClassName() << endl<< "Number of doors:" << t.getNumberOfDoors() << endl<< "Number of cylinders:" << t.getNumberOfCylinders() << endl;if (t.getTransmissionType() == 0)msg = "Manual";elsemsg = "Automatic";out << "Transmission type:" << msg << endl<< "Color:" << t.getColor() << endl<< "Fuel level:" << t.getFuelLevel() << endl;if (t.hasCargo())msg = "Is carrying cargo";elsemsg = "Is not carrying cargo";out << msg << endl;return out;
}int main() {int doors, cylinders, transmission;string color;double fuel;cin >> doors >> cylinders >> color >> fuel >> transmission;Vehicle vehicle(doors, cylinders, color, fuel, transmission);cout << vehicle;cin >> doors >> cylinders >> color >> fuel >> transmission;Taxi taxi(doors, cylinders, color, fuel, transmission, false);cout << taxi;cin >> doors >> cylinders >> color >> fuel >> transmission;Truck truck(doors, cylinders, color, fuel, transmission, true);cout << truck;return 0;
}

Result:

2.【描述】
①声明并实现一个名为Person的基类,Person类有保护数据成员name(姓名,string类型)、sex(性别,char类型,'M'表示男,'F'表示女)。以及有参构造函数,将姓名、性别设置为给定的参数;成员函数print,输出姓名和性别。
②从Person类派生出Student类,Student类有私有数据成员status(状态,枚举类型),表示年级(FRESHMAN、SOPHOMORE、JUNIOR、SENIOR),表示大一、大二、大三、大四学生。以及有参构造函数,将姓名、性别、状态设置为给定的参数;成员函数print,print函数输出姓名、性别和状态。
③定义MyDate类,它包含私有数据成员year、month和day以及带默认参数的有参构造函数,年、月、日的默认参数值分别为1900、1、1;成员函数print,输出年、月、日。
④从Person类派生出Employee类,Employee类有保护数据成员salary(薪水,int类型)、dateHired(雇佣日期),dataHired的类型是MyDate。以及有参构造函数,将姓名、性别、薪水和雇佣日期设置为给定的参数;成员函数print,输出姓名、性别、薪水和雇佣日期。
⑤从Employee类派生出Faculty类,Faculty类有私有数据成员rank(级别,枚举类型),有(PROFESSOR、ASSOCIATE_PROFESSOR、LECTURER),表示教授、副教授、讲师。以及有参构造函数,将姓名、性别、薪水、雇佣日期和级别设置为给定的参数;成员函数print,输出姓名、性别、薪水、雇佣日期和级别。
⑥从Employee类派生出Staff类,Staff类有私有数据成员headship(职务,枚举类型),有(PRESIDENT、DEAN、DEPARTMENT_CHAIRMAN),表示校长、院长、系主任。以及有参构造函数,将姓名、性别、薪水、雇佣日期和职务设置为给定的参数;成员函数print,输出姓名、性别、薪水、雇佣日期和职务。
【输入】
没有输入。
【输出】
Name:ZhangSan, Sex:M
Name:LiSi, Sex:F
Status:Freshman
Name:WangWu, Sex:M
Salary:5000, Hire date:2012-3-1
Name:LiuLiu, Sex:M
Salary:10000, Hire date:2012-3-1
Rank:Professor
Name:QianQi, Sex:M
Salary:8000, Hire date:2012-3-1
Headship:Department chairman

Code:

#include <iostream>
#include <string>
using namespace std;
/* 请在此处分别编写Person、Student、MyDate、Employee、Faculty、Staff类 */
class Person {protected:string name;char sex;public:Person(string name, char sex) {this->name = name;this->sex = sex;}void print() {cout << "Name:" << name << ",Sex:" << sex << endl;}};enum Status {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR};class Student: public Person {public:Student(string name, char sex, Status status): Person(name, sex) {this->status = status;}void print() {string grade;cout << "Name:" << name << ",Sex:" << sex << endl;switch (status) {case FRESHMAN:grade = "FRESHMAN";break;case SOPHOMORE:grade = "SOPHOMORE";break;case JUNIOR:grade = "JUNIOR";break;case SENIOR:grade = "SENIOR";break;}cout << "Status:" << grade << endl;}private:Status status;};class MyDate {public:MyDate(int year = 1900, int month = 1, int day = 1) {this->year = year;this->month = month;this->day = day;}void print() {cout << year << "-" << month << "-" << day << endl;}private:int year;int month;int day;
};class Employee: public Person {protected:int salary;MyDate dateHired;public:Employee(string name, char sex, int salary, MyDate &dateHired): Person(name, sex) {this->salary = salary;this->dateHired = dateHired;}void print() {Person::print();cout << "Salary:" << salary << ", Hire date:";dateHired.print();}
};enum Rank {PROFESSOR, ASSOCIATE_PROFESSOR, LECTURER};class Faculty: public Employee {public:Faculty(string name, char sex, int salary, MyDate &dateHired, Rank rank): Employee(name, sex, salary, dateHired) {this->rank = rank;}void print() {string level;Employee::print();switch (rank) {case PROFESSOR:level = "Professor";break;case ASSOCIATE_PROFESSOR:level = "Associate professor";break;case LECTURER:level = "Lecturer";break;}cout << "Rank:" << level << endl;}private:Rank rank;
};enum Headship {PRESIDENT, DEAN, DEPARTMENT_CHAIRMAN};class Staff: public Employee {public:Staff(string name, char sex, int salary, MyDate &dateHired, Headship headship): Employee(name, sex, salary, dateHired) {this->headship = headship;}void print() {string post;Employee::print();switch (headship) {case PRESIDENT:post = "President";break;case DEAN:post = "Dean";break;case DEPARTMENT_CHAIRMAN:post = "Department chairman";break;}cout << "Headship:" << post << endl;}private:Headship headship;
};
int main() {Person person("ZhangSan", 'M');Student student("LiSi", 'F', FRESHMAN);MyDate date(2012, 3, 1);Employee employee("WangWu", 'M', 5000, date);Faculty faculty("LiuLiu", 'M', 10000, date, PROFESSOR);Staff staff("QianQi", 'M', 8000, date, DEPARTMENT_CHAIRMAN);person.print();student.print();employee.print();faculty.print();staff.print();return 0;
}

Result:

3.【描述】
有5个类A、B、C、D、E。它们的形式均为:
class T {
public:
    T() {
        cout << "T()" << endl;
    }
    ~T() {
        cout << "~T()" << endl;
    }
};
这里T代表类名A、B、C、D、E。
给定如下main函数:
int main() {
    E e;
    return 0;
}
要求根据输出结果,声明并实现类A、B、C、D、E,确定类A、B、C、D、E之间的继承关系。
【输入】
没有输入。
【输出】
C()
B()
C()
A()
D()
E()
~E()
~D()
~A()
~C()
~B()
~C()

Code:

#include <iostream>
using namespace std;
/* 请在此处分别编写A、B、C、D、E类,注意:类A、B、C、D、E之间的继承关系 */
class C {public:C() {cout << "C()" << endl;}~C(){cout << "~C()" << endl;}
};
class A : public C {public:A() {cout << "A()" << endl;}~A(){cout << "~A()" << endl;}
};
class B : public C {public:B() {cout << "B()" << endl;}~B(){cout << "~B()" << endl;}
};
class D : public B, public A {public:D() {cout << "D()" << endl;}~D(){cout << "~D()" << endl;}
};
class E : public D {public:E() {cout << "E()" << endl;}~E() {cout << "~E()" << endl;}
};
int main() {E e;return 0;
}

 Result:

 4.【描述】
①声明并实现一个名为Arc的类,在二维空间中以某一个点为中心,绘制圆弧。Arc类包含私有数据成员p(圆心,Point类型),radius(圆弧半径,double类型);有参构造函数,将圆心、圆弧半径设置为给定的参数;成员函数draw,输出圆心和半径。
②声明并实现一个名为Circle的类,在二维空间中以某一个点为中心,绘制圆。Circle类包含私有数据成员p(圆心,Point类型),radius(圆半径,double类型);有参构造函数,将圆心、圆半径设置为给定的参数;成员函数draw,输出圆心和半径。
③声明并实现一个名为Ellipse的类,在二维空间中以某一个点为中心,绘制椭圆。Ellipse类包含私有数据成员p(圆心,Point类型),xRadius、yRadius(椭圆轴,double类型);有参构造函数,将圆心、椭圆轴设置为给定的参数;成员函数draw,输出圆心和轴。
④声明并实现一个名为Rectangle的类,在二维空间中以某一个点为左上角,绘制矩形。Rectangle类包含私有数据成员p(左上角坐标,Point类型),width、height(矩形宽度和高度,double类型);有参构造函数,将左上角坐标、矩形宽度和高度设置为给定的参数;成员函数draw,输出左上角坐标和宽度、高度。
⑤声明并实现一个名为Mix的类,在二维空间中以某一个点为中心,绘制圆弧、圆、椭圆,以某一个点为左上角,绘制矩形。Mix类包含有参构造函数,将点坐标、圆弧半径、圆半径、椭圆轴、矩形宽度和高度设置为给定的参数;成员函数draw绘制圆弧、圆、椭圆和矩形,调用Arc类、Circle类、Ellipse类、Rectangle类的draw函数,输出相关信息。
Mix类是Arc类、Circle类、Ellipse类、Rectangle类的多继承派生类。
【输入】
没有输入。
【输出】
Drawing an arc: Center(320, 240), radius(100)
Drawing a circle: Center(320, 240), radius(70)
Drawing an ellipse: Center(320, 240), x-axis(100), y-axis(70)
Drawing a rectangle: Upper left corner coordinates(320, 240), width(100), height(70)
【提示】
需要自定义Point类。
给定如下main函数:
int main() {
    Point p(320, 240);
    Mix mix(p, 100, 70);
    mix.draw();
    return 0;
}

Code

#include <iostream>
using namespace std;
class Point{public:Point(double x=0,double y=0){this->x=x;this->y=y;}double getX() const {return x;}double getY() const {return y;}private:double x;double y;
};
class Arc{public:Arc(Point n, double radius):p(n){this->radius=radius;}void draw() {cout << "Drawing an arc: Center(" << p.getX() << ", " << p.getY()<< "), radius(" << radius << ")" << endl;}private:Point p;double radius;
};
class Circle{public:Circle(Point n, double radius):p(n) {this->radius=radius;}void draw() {cout << "Drawing a circle: Center(" << p.getX() << ", " << p.getY()<< "), radius(" << radius << ")" << endl;}private:Point p;double radius;
};
class Ellipse{public:Ellipse(Point n, double xRadius, double yRadius):p(n),xRadius(xRadius),yRadius(yRadius) { }void draw() {cout << "Drawing an ellipse: Center(" << p.getX() << ", " << p.getY()<< "), x-axis(" << xRadius << "), y-axis(" << yRadius << ")" << endl;}private:Point p;double xRadius;double yRadius;
};
class Rectangle{public:Rectangle(Point n, double width, double height):p(n), width(width), height(height) { }void draw() {cout << "Drawing a rectangle: Upper left corner coordinates(" << p.getX() << ", " << p.getY()<< "), Width(" << width << "), Height(" << height << ")" << endl;
}private:Point p;double width;double height;
};
class Mix:public Arc, public Circle, public Ellipse, public Rectangle{public:Mix(Point p, double radius1, double radius2):Arc(p, radius1), Circle(p, radius2),Ellipse(p, radius1, radius2), Rectangle(p, radius1, radius2) { }void draw() {Arc::draw();Circle::draw();Ellipse::draw();Rectangle::draw();}
};int main() {Point p(320, 240);Mix mix(p, 100, 70);mix.draw();return 0;
}

 Result

5.【描述】
将输入数据按特定要求原样输出。
【输入】
第一行是整数t,表明一共t组数据。
对每组数据:
第一行是整数n,表示下面一共有n行,0<n<100。
下面的每行,以一个字母开头,然后跟着一个整数,两者用空格分隔。字母只会是'A'或'B'。整数范围0到100。
【输出】
对每组输入数据,将其原样输出,每组数据的最后输出一行"****"。
【输入示例】
2
4
A 3
B 4
A 5
B 6
3
A 4
A 3
A 2
【输出示例】
4
A 3
B 4
A 5
B 6
****
3
A 4
A 3
A 2
****

Code

#include <iostream>
using namespace std;class A {protected:int x;public:void Print() {cout << "A " << x << endl;}A(int i):x(i) { }
};
class B:public A {public:void Print() {cout << "B " << x << endl;}B(int i):A(i) { }
};
void PrintInfo(A *p) {p->Print();
}
A *a[100];int main() {int t;cin >> t;while(t--) {int n;cin >> n;for(int i = 0; i < n; ++i) {char c;int k;cin >> c >> k;if(c == 'A')a[i] = new A(k);elsea[i] = new B(k);}	cout << n << endl; for(int i = 0; i < n; ++i)	PrintInfo(a[i]);cout << "****" << endl;}
}

 Result

6.【描述】
按要求计算数值。
【输入】
第一行是整数n,表示第二行有n个整数。
第二行:n个整数。
所有整数都在0和100之间。
【输出】
先输出:
1
100
101
101
对于输入中第二行的每个整数x,输出两行:
第一行:k=x;
第二行:x的平方。
【输入示例】
3
3 4 5
【输出示例】
1
100
101
101
k=3
9
k=4
16
k=5
25 

Code

#include <iostream>
#include <string>
using namespace std;
class A {
private:int n;
public:A(int x) {n = x;}operator int() {return n;}A &operator++() {++n;return *this;}A operator++(int) {A tmp(*this);++n;return tmp;}
};class B {private:static int k;static int Square(int n) {cout <<  "k=" << k <<endl;return n * n;}friend int main();
};
int B::k;
int main() {A a1(1), a2(2);cout << a1++ << endl;(++a1) = 100;cout << a1 << endl;cout << ++a1 << endl;cout << a1 << endl;int n;cin >> n;while(n --) {cin >> B::k;A a3(B::k);cout << B::Square(a3) << endl;}return 0;
}

 Result

7.

【描述】
①Shape类是抽象类,包括了纯虚函数getArea(求面积)、getPerimeter(求周长)、show(输出对象信息)以及成员函数getClassName(返回类名“Shape”)。
②Circle类继承了Shape类,包括了double类型的私有数据成员radius,表示圆半径;带默认参数的有参构造函数,radius的默认参数值为1;访问器函数getRadius和更改器函数setRadius;重定义了getArea(求圆面积)、getPerimeter(求圆周长)、show(输出半径)、getClassName(返回类名“Circle”)函数。
③Rectangle继承了Shape类,包括了double类型的私有数据成员width、height,分别表示矩形的宽度和高度;带默认参数的有参构造函数,width和height的默认参数值分别为1、1;访问器函数getWidth、getHeight和更改器函数setWidth、setHeight;重定义了getArea(求矩形面积)、getPerimeter(求矩形周长)、show(输出宽度和高度)、getClassName(返回类名“Rectangle”)函数。
④Triangle类继承了Shape类,包括了double类型的私有数据成员side1、side2和side3,表示三角形三条边;有参构造函数,将三角形三条边设置为给定的参数;重定义了getArea(求三角形面积)、getPerimeter(求三角形周长)、show(输出三条边)、getClassName(返回类名“Triangle”)函数。
假设PI为3.14159。
【输入】
输入圆的半径、矩形的宽度和高度以及三角形的三条边。
【输出】
见【输出示例】
【输入示例】
3.5
5.8 11.8
1 1.5 1
【输出示例】

Circle:
Radius:3.5
Area:38.4845, Perimeter:21.9911
Rectangle:
Width:5.8, Height:11.8
Area:68.44, Perimeter:35.2
Triangle:
Side:1, 1.5, 1
Area:0.496078, Perimeter:3.5

Code

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
const double PI = 3.14159;/* 请在此处编写Shape、Circle、Rectangle和Triangle类 */
class Shape{public:virtual double getArea()=0;   //纯虚函数virtual double getPerimeter()=0;virtual void show()=0;virtual string getClassName(){return "Shape";}
};class Circle:public Shape{private:double radius;public:Circle(double radius=1){this->radius=radius;}double getRadius(){return radius;}double setRadius(double radius){this->radius=radius;}double getArea(){return PI*radius*radius;}double getPerimeter(){return 2*PI*radius;}void show(){cout<<"Radius:"<<radius<<endl;}string getClassName(){return "Circle";}
};class Rectangle:public Shape{public:Rectangle(double width=1,double height=1){this->width=width;this->height=height;}double getWidth(){return width;}double setWidth(double width){this->width=width;}double getHeight(){return height;}double setHeight(double height){this->height=height;}double getArea(){return width*height;}double getPerimeter(){return 2*(width+height);}void show(){cout<<"Width:"<<width<<",Height:"<<height<<endl;}string getClassName(){return "Rectangle";}private:double width,height;
};class Triangle:public Shape{public:Triangle(double side1,double side2,double side3){this->side1=side1;this->side2=side2;this->side3=side3;}double getArea(){double p=(side1+side2+side3)*0.5;return sqrt(p*(p-side1)*(p-side2)*(p-side3));}double getPerimeter(){return side1+side2+side3;}void show(){cout<<"Side:"<<side1<<","<<side2<<","<<side3<<endl;}string getClassName(){return "Triangle";}private:double side1,side2,side3;
};int main() {double radius, width, height, side1, side2, side3;cin >> radius;Circle circle(radius);cin >> width >> height;Rectangle rectangle(width, height);cin >> side1 >> side2 >> side3;Triangle triangle(side1, side2, side3);cout << circle.getClassName() << ":" << endl;circle.show();cout << "Area:" << circle.getArea();cout << ", Perimeter:" << circle.getPerimeter() << endl;cout << rectangle.getClassName() << ":" << endl;rectangle.show();cout << "Area:" << rectangle.getArea();cout << ", Perimeter:" << rectangle.getPerimeter() << endl;cout << triangle.getClassName() << ":" << endl;triangle.show();cout << "Area:" << triangle.getArea();cout << ", Perimeter:" << triangle.getPerimeter() << endl;return 0;
}

 Result

8.【描述】
①Employee类是抽象类。Employee类的派生类有Boss类、CommissionWorker类、PieceWorker类和HourlyWorker类。
②Employee类包括私有数据成员name(姓名,string类型);有参构造函数,将name设置为给定的参数;访问器函数getName;还有纯虚函数show和earnings。show和earning函数将在每个派生类中实现,因为每个派生类显示的信息不同、计算工资的方法不同。
③Boss类有固定的周工资且不计工作时间。Boss类包括私有数据成员weeklySalary(周工资,double类型);有参构造函数,将name、weeklySalary设置为给定的参数;更改器函数setWeeklySalary;show函数和earnings函数。
④CommissionWorker类有工资加上销售提成。CommissionWorker类包括私有数据成员salary(工资,double类型)、commission(佣金,double类型)和quantity(销售数量,int类型);有参构造函数,将name、salary、commission、quantity设置为给定的参数;更改器函数setSalary、setCommission和setQuantity;show函数和earnings函数。
⑤PieceWorker类的工资根据其生产的产品数量而定。PieceWorker类包括私有数据成员wagePerPiece(每件产品工资,double类型)、quantity(生产数量,int类型);有参构造函数,将name、wagePerPiece、quantity设置为给定的参数;更改器函数setWage、setQuantity;show函数和earnings函数。
⑥HourlyWorker类的工资根据小时计算并有加班工资。HourlyWorker类包括私有数据成员wage(小时工资,double类型)、hours(工作时数,double类型);有参构造函数,将name、wage、hours设置为给定的参数;更改器函数setWage、setHours;show函数和earnings函数。
动态绑定show和earnings函数显示和计算各类人员的信息和工资。
【输入】
输入Boss的姓名和周工资。
输入CommissonWorker的姓名、工资、佣金和销售数量。
输入PieceWorker的姓名、每件产品工资和生产数量。
输入HourlyWorker的姓名、小时工资、工作时数。
【输出】
见【输出示例】
【输入示例】
ZhangSan 800.0
LiSi 400.0 3.0 150
WangWu 2.5 200
LiuLiu 13.75 40
【输出示例】
Boss: ZhangSan
Earned: $800
Commission Worker: LiSi
Earned: $850
Piece Worker: WangWu
Earned: $500
Hourly Worker: LiuLiu
Earned: $550 

Code

#include <iostream>
#include <string>
using namespace std;/* 请在此处分别编写Employee、Boss、CommissionWorker、PieceWorker、HourlyWorker类 */class Employee {private:string name;public:Employee(string name) {this->name = name;}string getName() {return name;}virtual double earnings() = 0;virtual void show() = 0;
};class Boss: public Employee {private:double weeklySalary; //周工资public:Boss(string name, double weeklySalary): Employee(name) {this->weeklySalary = weeklySalary;}void setWeeklySalary(double weeklySalary) {this->weeklySalary = weeklySalary;}void show() {cout << "Boss:" << getName() << endl;}double earnings() {return weeklySalary;}
};class CommissionWorker: public Employee {private:double salary;   //工资double commission;   //佣金int quantity;    //销售数量public:CommissionWorker(string name, double salary, double commisiion, int quantity): Employee(name) {this->salary = salary;this->commission = commission;this->quantity = quantity;}void setSalary(double salary) {this->salary = salary;}void setCommission(double commission) {this->commission = commission;}void setQuantity(int quantity) {this->quantity = quantity;}double earnings() {return salary + commission * quantity;}void show() {cout << "Commission Worker: " << getName() << endl;}};class PieceWorker: public Employee {private:double wagePerPiece;  //每件产品的工资int quantity;         //生产数量public:PieceWorker(string name, double wagePerPiece, int quantity): Employee(name) {this->wagePerPiece = wagePerPiece;this->quantity = quantity;}void setWagePerPiece(double wagePerPiece) {this->wagePerPiece = wagePerPiece;}void setQuantity(int quantity) {this->quantity = quantity;}void show() {cout << "Piece Worker:" << getName() << endl;}double earnings() {return wagePerPiece * quantity;}
};class HourlyWorker: public Employee {private:double wage;   //小时工资double hours;  //工作时数public:HourlyWorker(string name, double wage, double hours): Employee(name) {this->wage = wage;this->hours = hours;}void setWage(double wage) {this->wage = wage;}void setHours(double hours) {this->hours = hours;}double earnings() {return wage * hours;}void show() {cout << "Hourly Worker: " << getName() << endl;}
};int main() {string name;double weeklySalary, salary, commission, quantity, wagePerPiece, wage, hours;cin >> name >> weeklySalary;Boss b(name, weeklySalary);cin >> name >> salary >> commission >> quantity;CommissionWorker c(name, salary, commission, quantity);cin >> name >> wagePerPiece >> quantity;PieceWorker p(name, wagePerPiece, quantity);cin >> name >> wage >> hours;HourlyWorker h(name, wage, hours);Employee *ref;      // 基类指针ref = &b;ref->show();cout << "Earned: $" << ref->earnings() << endl;ref = &c;ref->show();cout << "Earned: $" << ref->earnings() << endl;ref = &p;ref->show();cout << "Earned: $" << ref->earnings() << endl;ref = &h;ref->show();cout << "Earned: $" << ref->earnings() << endl;return 0;
}

Result

9.【描述】
下面是类A的定义,需要补充或增加完整的无参构造函数、虚析构函数以及fun()虚函数。
class A {
public:
   // ...
   void g() {
      fun();
   }
};
构造函数输出:A constructor,并调用g()函数
析构函数输出:A destructor
fun()函数输出:Call class A's fun
下面是类B的定义,继承类A,需要补充或增加完整的无参构造函数、析构函数。
class B {
public:
    // ...
};
无参构造函数输出:B constructor
析构函数输出:B destructor
下面是类C的定义,继承类B,需要补充或增加完整的无参构造函数、析构函数以及fun()函数。
class C {
public:
    // ...
};
无参构造函数输出:C constructor
析构函数输出:C destructor
fun()函数输出:Call class C's fun
给定如下main函数:
int main() { 
    A *a = new C;
    a->g();
    delete a;
    return 0;
}
【输入】
没有输入。
【输出】
主函数的输出已经写好。 

#include <iostream>
using namespace std;/* 请在此处编写A、B、C类 */
class A {public:A() {cout << "A constructor" << endl;g();}virtual ~A() {cout << "A destructor" << endl;}void g() {fun();}virtual void fun() {cout << "Call class A’s fun" << endl;}
};class B: public A {public:B() {cout << "B constructor" << endl;}~B() {cout << "B destructor" << endl;}
};class C: public B {public:C() {cout << "C constructor" << endl;}~C() {cout << "C destructor" << endl;}void fun() {cout << "Call class C’s fun" << endl;}
};int main() {A *a = new C;a->g();delete a;return 0;
}

Result

10.【描述】
下面是不完整的类定义:
class A {
public:
    virtual void print(){
        cout << "print come form class A" << endl;
    }
};
class B : public A {
private:
    char *buf;
public:
    void print() {
        cout << "print come from class B" << endl;
    }   
};
void fun(A *a) {
    delete a;
}
试完成其定义(需要增加必要的构造函数、析构函数)。

类A析构函数输出:A::~A() called

类B析构函数输出:B::~B() called

给定如下main函数:
int main() {
    A *a = new B(10);
    a->print();
    fun(a);
    B *b = new B(20);
    fun(b);
    return 0;
}
【输入】
没有输入。
【输出】
主函数的输出已经写好。

Code

#include <iostream>
using namespace std;/* 请在此处分别编写A、B类 */
class A {public:virtual ~A() {cout << "A::~A() called" << endl;}virtual void print() {cout << "print come form class A" << endl;}
};class B : public A {private:char *buf;public:B(int size) {buf = new char[size];}~B() {delete buf;cout << "B::~B() called" << endl;}void print() {cout << "print come from class B" << endl;}
};void fun(A *a) {delete a;
}int main() {A *a = new B(10);a->print();fun(a);B *b = new B(20);fun(b);return 0;
}

Result


http://www.taodudu.cc/news/show-1944670.html

相关文章:

  • 【physx/wasm】在physx中添加自定义接口并重新编译wasm
  • excel---常用操作
  • Lora训练Windows[笔记]
  • linux基础指令讲解(ls、pwd、cd、touch、mkdir)
  • InnoDB 事务处理机制
  • 启明云端ESP32 C3 模组WT32C3通过 MQTT 连接 AWS
  • 程序设计综合实践
  • C程序设计实践——实验指导
  • nvivo三天写论文!社会网络分析实战
  • 语义分析
  • NLPIR系统的中文语义分析模式介绍
  • 语义结构:依存分析
  • 《图像语义分析》学习笔记 (一)
  • 计算机语言语法语义,程序设计语言语义
  • 自然语言处理 4.语义分析
  • 自然语言处理(NLP)语义分析:“词汇级”语义分析【词义消歧、词义表示和学习】、“句子级”语义分析【浅层语义分析(语义角色标注)、深层语义分析】
  • 语义分析的一些方法
  • 语义分析的方法简述之文本基本处理
  • 《图像语义分析》学习笔记 (二)
  • 语义分析的一些方法(一)
  • python 英文语义分析_python语意分析
  • 潜在语义分析(TF-IDF、LSA)
  • NLPIR的语义分析系统
  • 云WAF之语义分析引擎
  • 语义网络与知识图谱
  • 【NLP】语义分析
  • 四、语义分析
  • LTP 语义依存分析
  • python语义网络图_知识图谱之语义网络篇
  • Python实现共现语义网络
  • 基于Python实现语义分析
  • python语义网络图_语义网络 (Knowledge Graph)知识图谱
  • 浅谈语义网络
  • c++ pdflib 中文乱码解决思路
  • PDFlib+PDI图像和超文本元素提供了许多有用的功能
  • PDFLib去水印办法