setter/getter related question
Write your question here.
q. Create getter and setter methods shown below that interact with the private members with matching names.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include "worldSeries.hpp"
#include <iostream>
#include <string>
using namespace std;
int main() {
int year;
string americanLeagueTeam, nationalLeagueTeam, whoWon;
cout<< "What was the year of the game?"<<endl;
cin >> year;
cout<< "Who won the game? (americanLeagueTeam or nationalLeagueTeam)"<<endl;
cin >> whoWon;
WorldSeries WC (year, americanLeagueTeam, nationalLeagueTeam, whoWon);
cout << "Year was: " << WC.getYear() <<endl;
cout << "Winner was: " << WC.getWinner() <<endl;
cout << "Loser was: " << WC.getLoser() <<endl;
return 0;
}
|
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include "worldSeries.hpp"
WorldSeries::WorldSeries( int year, string americanLeagueTeam, string nationalLeagueTeam, string whoWon )
{
my_Year = year;
my_Winner = whoWon;
if ( my_Winner == americanLeagueTeam )
my_Loser = nationalLeagueTeam;
else if (my_Winner== nationalLeagueTeam )
my_Loser = americanLeagueTeam;
}
string WorldSeries::getWinner() {
return my_Winner;
}
string WorldSeries::getLoser() {
return my_Loser;
}
int WorldSeries::getYear() {
return my_Year;
}
void WorldSeries::setWinner ( string winner ) {
my_Winner = winner;
}
void WorldSeries::setLoser( string loser ) {
my_Loser = loser;
}
void WorldSeries::setYear( int year ) {
my_Year = year;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#ifndef worldSeries_hpp
#define worldSeries_hpp
#include <string>
using namespace std;
class WorldSeries{
public:
WorldSeries( int year, string americanLeagueTeam, string nationalLeagueTeam, string whoWon );
string getWinner(); //getter methods
string getLoser();
int getYear();
void setWinner( string winner ); //setter methods
void setLoser( string loser );
void setYear( int year );
private:
string my_Winner; //the series winning team
string my_Loser; // the series losing team
int my_Year; // this cup's year
};
#endif /* worldSeries_hpp */
|
I'm trying to write a code as a question requests,
but m' not sure how to deal with next part.
1 2 3 4 5 6 7 8 9 10 11
|
WorldSeries::WorldSeries( int year, string americanLeagueTeam, string nationalLeagueTeam, string whoWon )
{
my_Year = year;
my_Winner = whoWon;
if ( my_Winner == americanLeagueTeam )
my_Loser = nationalLeagueTeam;
else if (my_Winner== nationalLeagueTeam )
my_Loser = americanLeagueTeam;
}
|
How should I fix this to get an proper answer at main?
Last edited on
I don't understand the question. The code looks alright to me. What's the issue you're seeing?
Topic archived. No new replies allowed.