c++类奖章问题

本文介绍了如何定义MyTeam类,包括成员变量(团队名、人数、姓名和奖章数)、静态成员BJFU奖章数,以及各类成员函数如构造函数、析构函数、复制构造函数、静态获取奖章数、增加奖章、输出运算符重载和比较运算符。在main函数中展示了这些功能的使用实例。
摘要由CSDN通过智能技术生成

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

 

#include <iostream>
#include <string.h>
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

白驹_过隙

听说打赏的都进了福布斯排行榜

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

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

打赏作者

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

抵扣说明:

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

余额充值