Description
The purpose of this challenge is to define and use basic classes.
Requirements
- Define a class called Country as below
class Country { private: string name; int gold, silver, bronze; };
- In class Country, create an overloaded constructor that will pass in the country name. Within this constructor, set variables gold, silver and bronze to 0.
- In class Country, create a setter function void add_medal(int medal). The value of medal can be 1, 2 or 3. Whichever medal value is passed will increment the corresponding data member. For example if it’s called as add_medal(2), then increment silver property. If it’s called as add_medal(1), then increment the gold member.
- Create getter functions for each of the properties of the Country class. For the name getter function, write this outside of the class using the Scope Resolution Operator.
- Create a global function (non-class member) called void score_event(double scores[], Country & c1, Country & c2, Country & c3)
- In this function, the scores array will contain 3 values respectively for each of three countries, c1, c2 and c3
- This function will determine which country will be awarded which medal based on the scores. Highest score will award a gold medal to the winning country; Median score will award a silver medal to the winning country, and lowest score will award a bronze medal to the appropriate country. Use each country’s objects to increment the correct medals
- This function will display the current medal tallies (See Sample Interaction)
- In main() — See interaction,
- Declare a double array scores[3]
- ask the user for three competing country names. The country names entered will be used in instantiating three Country objects. Remember that the overloaded constructor must be used during instantiation.
- Ask the user to score 3 events (use a loop) by asking for three scores, one score for each country. The elements of the scores array will be used for countries 1, 2 and 3 respectively.
- Call the score_event function, passing in all the necessary parameters (event name, the scores array, and the three country objects)
Sample main()
// class definitions here int main() { string cname; double scores[3]; // Country 1 cout << "Enter country 1"; cin >> cname; Country c1(cname); // Ask similary for the next two countries // Score 3 events for (int i = 1; i <= 3; i++) { cout << "Enter score for " << c1.get_name() << ": " << endl; cin >> scores[0]; // ask for the other two countries score_event(scores, c1, c2, c3); } return 0; }
Sample Interaction
Enter country 1: USA Enter country 2: Russia Enter country 3: China Event 1 Enter score for USA: 9.25 Enter score for Russia: 9.5 Enter score for China: 9.0 (this is displayed by score_event function <- don't display this) Medal Tally (Gold, Silver, Bronze): USA: 0 1 0 Russia: 1 0 0 China: 0 0 1 Event 2 Enter score for USA: 5 Enter score for Russia: 4.5 Enter score for China: 4 Medal Tally (Gold, Silver, Bronze): USA: 1 1 0 Russia: 1 1 0 China: 0 0 2 Event 3 Enter score for USA: 9.0 Enter score for Russia: 9.25 Enter score for China: 9.75 Medal Tally (Gold, Silver, Bronze): USA: 1 1 1 Russia: 1 2 0 China: 1 0 2
LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT
CATALOG ID: CPP-CHAL0066
Print Requirements