I should be overloading the operator + for adding two squads that will return as a result a new squad with the number of members that is the sum of the number of members of both squads, and other attributes are taken from the squad with larger number of members. I am trying it but the result does not make any sense! And I don't know whether the mistake is in the part where I am overloading the + operator, or it is in the function that shows which tour has most members?!
Here is what all the exercise says: "Mountain squad Problem 2 (0 / 30)
Write a class for mountain squad, that keeps information for the name of the squad (dynamically allocated array of chars), number of tours (integer) and number of members (integer). For this class implement:
•operator + for adding two squads that will return as a result a new squad with number of members that is sum of the number of members of both squads, and other attributes are taken from the squad with larger number of members.
•operators >, < for comparison by the number of members
•operator << for printing the info on SO.
Write a function that accepts array of mountain squads and the size of the array and prints the squad with max number of members. "
#include <iostream>
#include <string.h>
using namespace std;
class MSquad{
private:
char *name;
int tours;
int members;
void copy(const MSquad &toCopy){
name = new char[strlen(toCopy.name) + 1];
strcpy(name, toCopy.name);
tours = toCopy.tours;
members = toCopy.members;
}
public:
MSquad(char *n = "unknown", int nT = 0, int nM = 0){
name = new char[strlen(n + 1)];
strcpy(name, n);
tours = nT;
members = nM;
}
MSquad(const MSquad &toCopy){
copy(toCopy);
}
~MSquad(){
delete [] name;
}
const MSquad &operator=(const MSquad &right){
if(&right != this){ // avoiding self- assignment
delete [] name;
copy(right);
}
return *this;
}
MSquad &operator+(const MSquad &right) const{
members = members + right.members;
if(right.members > members){
name = new char[strlen(right.name) + 1];
strcpy(name, right.name);
tours = right.tours;
//members = members + right.members;
}
return *this;
}
bool operator>(const MSquad &right){
return members > right.members;
}
bool operator<(const MSquad &right){
return members < right.members;
}
friend ostream &operator<<(ostream &output, const MSquad &right);
friend void mostMembers(MSquad *squads, int size);
};
ostream &operator<<(ostream &output, const MSquad &right){
output << "Name: " << right.name;
output << " Tours: " << right.tours;
output << " Members: " << right.members << endl;
return output;
}
void mostMembers(MSquad squads[], int size){
int max = squads[0].members;
int j = 0;
for(int i = 1; i < size; i++){
if(squads[i].members >= max){
max = squads[i].members;
j = i;
}
}
cout << "The max number of members is in squad in: " << squads[j] << endl;
}
int main()
{
MSquad squads[3];
MSquad s;
for (int i=0;i<3;i++)
{
char name[100];
int tours;
int members;
cin>>name;
cin>>tours;
cin>>members;
squads[i] = MSquad(name, tours, members);
}
s = squads[0] + squads[1];
cout<<s;
mostMembers(squads, 3);
return 0;
}