建立一个简单的运动会管理系统,包括田赛和径赛的管理。需要完成如下功能: 1、建立一个基类SportEvent,它的属性有比赛名称name、比赛地点location,参赛的运动员athlete;还要给SportEvent类定义合适的成员函数,其中包括虚函数show_info (),用于输出比赛项目的基本信息。 2、建立一个田赛类FieldEvent和一个径赛类TrackEvent,它们均由SportEvent类公有继承而来。 3、田赛类FieldEvent有新增数据成员:运动员跳的距离distance,给田赛类重定义show_info ()函数以输出其相关信息。田赛类还应该具有修改距离等功能和比较运动员的成绩。 4、径赛类TrackEvent有新增数据成员:运动员跑的时间time,给径赛类重定义show_info ()函数以输出其相关信息。径赛类还应该具有修改时间和比较运动员的成绩等功能。 5、重载>>,实现用cin为田赛类和径赛类的对象取得值。 6、为每个类写构造函数。田赛对象和径赛对象的信息都能够保存到文件。 7、编写main()主函数,测试上述功能。
时间: 2023-06-19 17:04:33 浏览: 87
以下是一个简单的运动会管理系统的代码实现:
```
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class SportEvent {
protected:
string name; //比赛名称
string location; //比赛地点
string athlete; //参赛运动员
public:
SportEvent(string n = "", string l = "", string a = "") {
name = n;
location = l;
athlete = a;
}
virtual void show_info() {
cout << "比赛名称:" << name << endl;
cout << "比赛地点:" << location << endl;
cout << "参赛运动员:" << athlete << endl;
}
};
class FieldEvent : public SportEvent {
private:
double distance; //跳远距离
public:
FieldEvent(string n = "", string l = "", string a = "", double d = 0.0) : SportEvent(n, l, a) {
distance = d;
}
void show_info() {
cout << "比赛名称:" << name << endl;
cout << "比赛地点:" << location << endl;
cout << "参赛运动员:" << athlete << endl;
cout << "跳远距离:" << distance << endl;
}
void modify_distance(double d) {
distance = d;
}
bool operator<(const FieldEvent& f) {
return (distance < f.distance);
}
};
class TrackEvent : public SportEvent {
private:
double time; //跑步时间
public:
TrackEvent(string n = "", string l = "", string a = "", double t = 0.0) : SportEvent(n, l, a) {
time = t;
}
void show_info() {
cout << "比赛名称:" << name << endl;
cout << "比赛地点:" << location << endl;
cout << "参赛运动员:" << athlete << endl;
cout << "跑步时间:" << time << endl;
}
void modify_time(double t) {
time = t;
}
bool operator<(const TrackEvent& t) {
return (time < t.time);
}
};
//重载输入运算符>>
istream& operator>>(istream& input, FieldEvent& f) {
cout << "请输入比赛名称:" << endl;
input >> f.name;
cout << "请输入比赛地点:" << endl;
input >> f.location;
cout << "请输入参赛运动员:" << endl;
input >> f.athlete;
cout << "请输入跳远距离:" << endl;
input >> f.distance;
return input;
}
//重载输入运算符>>
istream& operator>>(istream& input, TrackEvent& t) {
cout << "请输入比赛名称:" << endl;
input >> t.name;
cout << "请输入比赛地点:" << endl;
input >> t.location;
cout << "请输入参赛运动员:" << endl;
input >> t.athlete;
cout << "请输入跑步时间:" << endl;
input >> t.time;
return input;
}
int main() {
FieldEvent f("跳远比赛", "体育场", "小明", 7.5);
f.show_info();
cout << "修改跳远距离后:" << endl;
f.modify_distance(8.2);
f.show_info();
TrackEvent t("100米短跑", "田径场", "小红", 12.8);
t.show_info();
cout << "修改跑步时间后:" << endl;
t.modify_time(11.6);
t.show_info();
ofstream outfile;
outfile.open("data.txt", ios::out | ios::binary);
outfile.write((char*)&f, sizeof(FieldEvent));
outfile.write((char*)&t, sizeof(TrackEvent));
outfile.close();
ifstream infile;
infile.open("data.txt", ios::in | ios::binary);
FieldEvent f2;
TrackEvent t2;
infile.read((char*)&f2, sizeof(FieldEvent));
infile.read((char*)&t2, sizeof(TrackEvent));
f2.show_info();
t2.show_info();
infile.close();
cin >> f2;
f2.show_info();
cin >> t2;
t2.show_info();
return 0;
}
```
阅读全文
相关推荐

















