Q.Design a Soccer Player class that includes three integer fields: a player's Jersey number, number of goals, numbers of assists and necessary constructor to initialize the data members. Overload the ">" Operator (Greater than). One Player is considered greater if the sum of goals plus assists is greater than that of others. Create an array of 11 Soccer players, then use the overloaded ">" operator to ranks the players based on greatest goals plus assists.
Question for you:-
What is assists actually? Please answer in comment.
Well First I overload the > operator to return a Boolean value (true if the first value is greater than second, else false) then I use a sorting algorithm to arrange the players.
If you are asked to select the Best player only. First take one player as reference and follow the sorting algorithm to assign the greater value to it. And display that player.
Thanks Pradeep :)
Here is the code by Nischal Lal Shrestha.
Question for you:-
What is assists actually? Please answer in comment.
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | #include <iostream>
using namespace std;
class Soccer{
int jNumber, goals, assists;
public:
Soccer(){
jNumber = 0;
goals = 0;
assists = 0;
};
Soccer(int, int, int);
bool operator > (Soccer &);
void display();
};
Soccer :: Soccer(int j, int g, int a){
jNumber = j;
goals = g;
assists = a;
}
bool Soccer :: operator > (Soccer &s){
if(goals + assists > s.goals + s.assists){
return true;
}
else{
return false;
}
}
void Soccer :: display(){
cout << "Jersey Number: " << jNumber << endl;
cout << " Goals: " << goals << endl;
cout << " Assists: " << assists << endl;
}
int main(){
int i,j, jn, goal, assist;
Soccer tmp,S1[11];
for(i = 0; i <= 2; i++){
cout << "Enter Data For Player "<< i+1 << ":-" << endl;
cout << "Jersey number:- ";
cin >> jn;
cout << "Goals:- ";
cin >> goal;
cout << "Assists:- ";
cin >> assist;
Soccer tmp(jn, goal, assist);
S1[i] = tmp;
}
for( j = 0; j<=2; j++){
for( i = j; i<=2; i++){
if(S1[i] > S1[j])
{
tmp = S1[i];
S1[i] = S1[j];
S1[j] = tmp;
}
}
}
cout << "Best User Ranking:-"<<endl;
for( i = 0; i<=2; i++){
cout << i + 1 << ": " ;
S1[i].display();
}
return 0;
}
|
Well First I overload the > operator to return a Boolean value (true if the first value is greater than second, else false) then I use a sorting algorithm to arrange the players.
If you are asked to select the Best player only. First take one player as reference and follow the sorting algorithm to assign the greater value to it. And display that player.
Thanks Pradeep :)
Here is the code by Nischal Lal Shrestha.
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include <iostream>
// #include <iostream.h>
using namespace std;
class Soccer_player{
private:
int jersey_number, number_of_goals, number_of_assists;
string player_name;
public:
Soccer_player(){
cout << "Enter Player Name: " << endl;
cin >> player_name;
cout << "Enter Jersey Number: " << endl;
cin >> jersey_number;
cout << "Enter Total Number of Goals: " << endl;
cin >> number_of_goals;
cout << "Enter Total Number of Assists: " << endl;
cin >> number_of_assists;
cout << "***** Player Info Updated *****" << endl;
}
/* This is a copy constructor,
We will used this to make another object called Best_Player
and first_player will be copied as best player for comparing it
with other(May we don't need this constructor )
*/
Soccer_player(Soccer_player &Player){
player_name = Player.player_name;
jersey_number = Player.jersey_number;
number_of_goals = Player.number_of_goals;
number_of_assists = Player.number_of_assists;
}
void display(){
cout << "Name: " << player_name << endl;
cout << "Jersey Number: " << jersey_number << endl;
cout << "Total Goals: " << number_of_goals << endl;
cout << "Total Assists: " << number_of_assists << endl;
}
Soccer_player operator > (Soccer_player &P){
if (number_of_assists + number_of_goals > P.number_of_assists + P.number_of_goals)
return *this; // Return object which is already passed as default.
else
return P;
}
};
int main(){
Soccer_player Player[3];
Soccer_player Best_Player = Player[0]; // First Player is made best player for comparision
for(int i = 1; i < 3; ++i)
Best_Player = Player[i] > Best_Player;
cout << "\n\n\nOf all the info you updated\nThe Details of Best Player: \n\n" << endl;
Best_Player.display();
// getch();
return 0;
}
|