(二)Team

2023-03-23  本文已影响0人  GoodTekken
#include<iostream>
#include<string>

using namespace std;

class Team
{
    private:
        string name;
        int goals;  //(1) 

    public:
        Team(string name)
        {
            this->name = name; //(2) 
            goals = 0;
        }
        void increamentGoal()
        {
            goals++; //(3) 
        }

        int getGoals()
        {
            return goals;
        }

        string getName()
        {
            return name;
        }
};

class Game
{
    private:
        Team *a,*b;   //两支比赛球队

    public:
        Game(Team *t1, Team *t2)
        {
            a = t1;
            b = t2;
        }

        void getResults()  //输出比分
        {
            cout << a->getName() << " : " << b->getName() << " = ";
            cout << a->getGoals() << " : " << b->getGoals() << endl;
        }

        void increamentGoal(Team* t) //球队t 进1球  //(4) 
        {
            t->increamentGoal();
        }
};

int main()
{
    Team *t1 = new Team("TA");
    Team *t2 = new Team("TB");
    Game *football = new Game(t1,t2); //(5) 

    football->increamentGoal(t1);
    football->increamentGoal(t2);
    football->getResults();       //输出为:TA:TB = 1:1
    football->increamentGoal(t2);
    football->getResults();       //输出为:TA:TB = 1:2

    return 0;
}

答案:
(1) int goals

(2) this->name

(3) goals++

(4) Team*

(5) new Game(t1,t2)

上一篇 下一篇

猜你喜欢

热点阅读