定义一个MyTeam类,包含普通成员变量:团队的名字、成员人数,成员姓名的指针,奖章个数;静态成员变量:BJFU奖章个数(初始值为0)。
2.定义该类的成员函数:
1)构造函数,提示用户输入成员人数,开辟数组空间,用户键盘输入成员姓名,同时输出“构造函数被调用啦”
2)析构函数,输出“析构函数被调用啦”;
3)复制构造函数,实现对象的复制功能,并且输出“复制构造函数被调用啦”;
4)静态成员函数getBJFURewardNum,返回BJFU奖章个数的值;
6)成员函数winReward,奖章个数加一,同时BJFU奖章个数加一;
7)输出运算符重载,输出团队介绍,将团队的所有信息打印输出,并输出“输出运算符被调用啦”;8)>==和<这三个运算符重载,通过比较奖章个数大小来比较对象的大小,并分别输出“*运算符被调用啦”;
9)定义一个友元类School,包含一个成员函数getTeamInfo,实现输出MyTeam类对象的成员姓名和奖章个数。
3.在main函数中分别测试以上成员函数及友元类。

c++类问题 (奖章问题)太长了来不及做了
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- qfl_sdu 2022-06-23 11:04关注
代码如下:
#include <iostream> using namespace std; class MyTeam { private: char teamName[30];//团队名字 int teamNmb; //人数 char **teamPsn; //团队成员 int jz; static int BJFU; public: friend class School; MyTeam() { cout << "请输入团队名称:"; cin >> teamName; cout << "请输入成员人数:"; cin >> teamNmb; teamPsn = new char*[teamNmb]; for (int i = 0; i < teamNmb; i++) { teamPsn[i] = new char[30]; cout << "请输入第" << i + 1 << "个人的姓名:"; cin >> teamPsn[i]; } jz = 0; cout << "构造函数被调用啦" << endl; } ~MyTeam() { for (int i = 0; i < teamNmb; i++) { delete[] teamPsn[i]; teamPsn[i] = 0; } delete[] teamPsn; teamPsn = 0; cout << "析构函数被调用啦" << endl; } MyTeam(MyTeam& t) { strcpy(teamName,t.teamName); teamNmb = t.teamNmb; teamPsn = new char* [teamNmb]; for (int i = 0; i < teamNmb; i++) { teamPsn[i] = new char[30]; strcpy(teamPsn[i],t.teamPsn[i]); } jz = 0; cout << "复制构造函数被调用啦" << endl; } static int getBJFURewardNum() { return BJFU; } void winReward() { jz++; BJFU++; } char* getTeamName() { return teamName; } friend ostream& operator<<(ostream& os, MyTeam& t); friend int operator >(MyTeam& t1, MyTeam& t2); friend int operator ==(MyTeam& t1, MyTeam& t2); friend int operator <(MyTeam& t1, MyTeam& t2); }; ostream& operator<<(ostream& os, MyTeam& t) { os << "团队名称:" << t.teamName << endl; os << "团队人数:" << t.teamNmb << endl; os << "成员姓名:" << endl; for (int i = 0; i < t.teamNmb; i++) os << t.teamPsn[i]; os << "奖章个数:" << t.jz << endl; os << "输出运算符被调用啦" << endl; return os; } int operator >(MyTeam& t1, MyTeam& t2) { cout << ">运算符被调用啦" << endl; if (t1.jz > t2.jz) return 1; else return 0; } int operator ==(MyTeam& t1, MyTeam& t2) { cout << "==运算符被调用啦" << endl; if (t1.jz == t2.jz) return 1; else return 0; } int operator <(MyTeam& t1, MyTeam& t2) { cout << "<运算符被调用啦" << endl; if (t1.jz < t2.jz) return 1; else return 0; } int MyTeam::BJFU = 0; class School { public: void getTeamInfo(MyTeam& team) { cout << team << endl; } }; int main() { MyTeam* t1 = new MyTeam(); MyTeam t2(*t1); MyTeam* t3 = new MyTeam(); School sc; t1->winReward(); cout << "BJFU奖章个数:" << MyTeam::getBJFURewardNum() << endl; if (*t1 > *t3) cout << t1->getTeamName() << "比"<< t3->getTeamName() << "的奖章多" << endl; else if(*t1 == *t3) cout << "两个团队的奖章一样多" << endl; else if(*t1 < *t3) cout <<t3->getTeamName()<<"比"<<t1->getTeamName()<< "的奖章多" << endl; sc.getTeamInfo(*t1); sc.getTeamInfo(t2); sc.getTeamInfo(*t3); return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用