面向对象程序设计-实验七 虚函数的使用

2022/5/29
1.补充完成以下程序

有两类不同的运货车:一种称作 BoxCar,用来运送箱子,以面积来计算容量,最大容量是20 平米;另一种称作TankCar,用来运送液体,以体积来计算容量,最大容量是 5000 升。请编写该类系,使得如下 main 函数可以运行。

#include<vector>
using namespace std;
int main() {
vector<Car*> cars(4);
cars[0] = new BoxCar(15);     // 已用15平米
cars[1] = new BoxCar(10);
cars[2] = new TankCar(3000); // 已用3000升
cars[3] = new BoxCar(20);
for(int i=0; i<4; i++)
cars[i]->printCapacity(); // 输出剩余容量
for(int j=0; j<4; j++)
delete cars[j];
}

运行输出:
本车剩余5平米
本车剩余10平米
本车剩余2000升
本车剩余0平米

#include<iostream>
#include<vector>
using namespace std;
class Car {
public:
	Car(int c = 0) {
		capacity = c;
	}//构造函数
	virtual void printCapacity() = 0;
protected:
	int capacity;//容量
};
class BoxCar :public Car {
public:
	BoxCar(int c) {
		capacity = 20 - c;
	}
	virtual void printCapacity();//输出
};
void BoxCar::printCapacity() {
	cout << "本车剩余" << capacity << "平米" << endl;
}
class TankCar :public Car {
public:
	TankCar(int c) {
		capacity = 5000 - c;
	}
	virtual void printCapacity();
};
void TankCar::printCapacity() {
	cout << "本车剩余" << capacity << "升" << endl;
}
int main() {
	vector<Car*> cars(4);
	cars[0] = new BoxCar(15);//已用15平米
	cars[1] = new BoxCar(10);
	cars[2] = new TankCar(3000);//已用3000升
	cars[3] = new BoxCar(20);
	for (int i = 0; i < 4; i++) {
		cars[i]->printCapacity();//输出剩余容量
	}
	for (int j = 0; j < 4; j++) {
		delete cars[j];
	}
	return 0;
}
2.编写一个工资管理程序,将雇员类作为所有类的基类,其派生类包括经理类,销售员类,计件工类和小时工类。经理的收入为固定月薪,销售员的收入是一小部分的基本工资加上销售额的提成,计件工类的收入完全取决于其生产的工件数量,小时工的收入以小时计算,再加上加班费。将工资计算函数和显示输出函数设为虚函数,初始化所有类的对象并显示输出其信息。
#include<iostream>
#include<vector>
using namespace std;
class Employee {
public:
	virtual void salarycalculate() {}//计算收入
	virtual void printsalary() {}//输出收入
protected:
	int salary = 0;//收入
};
class Manger :public Employee {
public:
	virtual void salarycalculate();
	virtual void printsalary();
};
void Manger::salarycalculate() {
	salary = 5000;//固定工资
}
void Manger::printsalary() {
	cout << "经理的收入为:" << salary << endl;
}
class Salesperson :public Employee {
public:
	Salesperson(int s) :sales(s) {}
	virtual void salarycalculate();
	virtual void printsalary();
private:
	int sales;
};
void Salesperson::salarycalculate() {
	salary = 3000 + sales * 0.1;//固定工资+提成
}
void Salesperson::printsalary() {
	cout << "销售员的收入为:" << salary << endl;
}
class Pieceworker :public Employee {
public:
	Pieceworker(int c) :counts(c) {}
	virtual void salarycalculate();
	virtual void printsalary();
private:
	int counts;
};
void Pieceworker::salarycalculate() {
	salary = counts * 5;//按件数
}
void Pieceworker::printsalary() {
	cout << "计件工的收入为:" << salary << endl;
}
class Hourlyworker :public Employee {
public:
	Hourlyworker(int h, int a) :hours(h), add(a) {}
	virtual void salarycalculate();
	virtual void printsalary();
private:
	int hours;
	int add;
};
void Hourlyworker::salarycalculate() {
	salary = hours * 100 + add;//小时费+加班费
}
void Hourlyworker::printsalary() {
	cout << "小时工的收入为:" << salary << endl;
}
int main() {
	vector<Employee*> Employee(4);
	Employee[0] = new Manger();
	int x = 0, y = 0;
	cout << "请输入销售额:";
	cin >> x;
	Employee[1] = new Salesperson(x);
	cout << "请输入计件数:";
	cin >> x;
	Employee[2] = new Pieceworker(x);
	cout << "请输入小时数和加班费:";
	cin >> x >> y;
	Employee[3] = new Hourlyworker(x, y);
	for (int i = 0; i < 4; i++) {
		Employee[i]->salarycalculate();
	}
	for (int i = 0; i < 4; i++) {
		Employee[i]->printsalary();
	}
	for (int i = 0; i < 4; i++) {
		delete Employee[i];
	}
	return 0;
}
3.创建类Car和类MotorBike,其中,类Car中包含产地,颜色,座位数,车门数量,类MotorBike包含产地,颜色和气缸数。创建基类Vehicle,基类包含了类Car和类MotorBike的共有成员。将以下三个函数设为虚函数:显示对象信息的函数PrintInfo,增加指定的对象函数Add(即给定对象的参数,将其添加到数组中)和删除指定特征的对象函数Delete(即给定某一特征,如产地,删除数组中具有该特征的对象)。

要求:
1)主程序中设立基类指针,指向不同类型的派生类对象,显示所指向的不同派生类对象的参数;
2)动态建立派生类对象,并验证析构函数的调用;
3)建立动态数组。添加三个Car的对象,三个MotorBike的对象;
4)查找并删除数组中指定颜色或者产地的对象。

#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Vehicle {
public:
	Vehicle(string p, string c) :produce(p), colour(c) {}//构造函数
	virtual ~Vehicle(){cout << "delete Vehicle" << endl;}//析构函数
	virtual void PrintInfo(){}
	virtual void Add(string p, string c, int s = 0, int d = 0, int y = 0){}
	virtual void Delete(string x, string y){}
	int key = 1;//是否删除
protected:
	string produce;//产地
	string colour;//颜色
};
class Car :public Vehicle {
public:
	Car(string p, string c, int s, int d) :Vehicle(p, c), seats(s), doors(d) {}//构造函数
	virtual ~Car() { cout << "delete Car" << endl; }//析构函数
	virtual void PrintInfo();
	virtual void Add(string p, string c, int s, int d, int y);
	virtual void Delete(string x, string y);
private:
	int seats;//座位数
	int doors;//车门数
};
void Car::PrintInfo() {
	cout << "Car 产地:" << produce << " 颜色:" << colour << " 座位数:" << seats << " 车门数:" << doors << endl;
}
void Car::Add(string p, string c, int s, int d, int y) {
	*this = Car(p, c, s, d);
}
void Car::Delete(string x, string y) {
	if ((x == "产地" && y == produce) || (x == "颜色" && y == colour)) {
		key = 0;
	}
}
class MotorBike :public Vehicle {
public:
	MotorBike(string p, string c, int y) :Vehicle(p, c), cylinders(y) {}//构造函数
	virtual ~MotorBike() { cout << "delete MotorBike" << endl; }//析构函数
	virtual void PrintInfo();
	virtual void Add(string p, string c, int s, int d, int y);
	virtual void Delete(string x,string y);
private:
	int cylinders;//气缸数
};
void MotorBike::PrintInfo() {
	cout << "MotorBike 产地:" << produce << " 颜色:" << colour << " 气缸数:" << cylinders << endl;
}
void MotorBike::Add(string p, string c, int s, int d, int y) {
	*this = MotorBike(p, c, y);
}
void MotorBike::Delete(string x, string y) {
	if ((x == "产地" && y == produce) || (x == "颜色" && y == colour)) {
		key = 0;
	}
}
int main() {
	vector<Vehicle*> Vehicle(7);
	Vehicle[0] = new Car("浙江", "blue", 4, 4);
	Vehicle[1] = new Car("江苏", "yellow", 4, 4);
	Vehicle[2] = new Car("北京", "white", 6, 6);
	Vehicle[3] = new MotorBike("浙江", "blue", 4);
	Vehicle[4] = new MotorBike("江苏", "yellow", 4);
	Vehicle[5] = new MotorBike("北京", "white", 6);
	string x;
	string p;
	string c;
	int s;
	int d;
	int y;
	cout << "请输入添加类型:";
	cin >> x;
	cout << "请输入产地:";
	cin >> p;
	cout << "请输入颜色:";
	cin >> c;
	if (x == "Car") {
		Vehicle[6] = new Car("", "", 0, 0);
		cout << "请输入座位数和车门数:";
		cin >> s >> d;
		y = 0;
	}
	else if(x == "MotorBike") {
		Vehicle[6] = new MotorBike("", "", 0);
		cout << "请输入气缸数:";
		cin >> y;
		s = 0;
		d = 0;
	}
	Vehicle[6]->Add(p, c, s, d, y);
	for (int i = 0; i < 7; i++) {
		Vehicle[i]->Delete("产地", "江苏");
	}
	for (int i = 0; i < 7; i++) {
		if (Vehicle[i]->key == 1) {
			Vehicle[i]->PrintInfo();
		}
	}
	for (int i = 0; i < 7; i++) {
		delete Vehicle[i];
	}
	return 0;
}
实验总结
  1. 注意虚函数的使用
  2. 注意基类和派生类中指针的使用
  3. 注意虚析构函数的作用
  4. 注意临时对象的创建和析构
  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闻闻闻闻笛声

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值