8.18 BallGame Lab
Create the following class. You will NOT need setters and getters for this as all of the attributes are public. There will however be 1 constructor as indicated.
class BallGame { public: string homeTeam; string awayTeam; int homeScore; int awayScore; BallGame(string hmTeam, string awTeam, int hmSc, int awSc); bool didHomeTeamWin(); };
You must implement the constructor and the didHomeTeamWin() method. The method must return whether the home team has the larger score. (Note, a tie is not a win)
In main: Allocate memory for three separate BallGame instances and point to that memory with 3 separate pointer variables, game1, game2, and game3. Make each pointer represent the following data. Your constructor MUST accept the data in the order home team name, away team name, home team score, away team score:
"Mystics", "Fire", 100,92 "Capitals", "Lightning", 2,3 "Ravens", "Colts",35,35
#include<iostream>
using namespace std;
class BallGame{
public:
string homeTeam;
string awayTeam;
int homeScore;
int awayScore;
BallGame(string hmTeam, string awTeam, int hmSc, int awSc);
bool didHomeTeamWin();
};
//constructor
BallGame::BallGame(string hT,string aT,int hS,int aS){
homeTeam=hT;
awayTeam=aT;
homeScore=hS;
awayScore=aS;
}
//function to check home team win or not
bool BallGame::didHomeTeamWin(){
if(homeScore>awayScore)
return true;
else
return false;
}
int main(){
//defining BallGame objects
BallGame g1( "Mystics", "Fire", 100,92);
BallGame g2( "Capitals", "Lightning", 2,3);
BallGame g3( "Ravens", "Colts",35,35);
//creating pointers to BallGame objects
BallGame *game1,*game2,*game3;
game1=&g1;
game2=&g2;
game3=&g3;
//verifying home team wins or not
if(game1->didHomeTeamWin()){
cout<<"Home team
("<<game1->homeTeam<<") wins game1\n";
}else{
cout<<"Home team
("<<game1->homeTeam<<") did not win game1\n";
}
if(game2->didHomeTeamWin()){
cout<<"Home team
("<<game2->homeTeam<<") wins game2\n";
}else{
cout<<"Home team
("<<game2->homeTeam<<") did not win game2\n";
}
if(game3->didHomeTeamWin()){
cout<<"Home team
("<<game3->homeTeam<<") wins game3\n";
}else{
cout<<"Home team
("<<game3->homeTeam<<") did not win game3\n";
}
return 0;
}
//#############################################################
OUTPUT
###########
Get Answers For Free
Most questions answered within 1 hours.