- #1
sxal96
- 15
- 1
Hi, I'm having difficulty with this program in a textbook. The instructions are as follows:
Overload the + operator as indicated. Sample output for the given program:
First vacation: Days: 7, People: 3
Second vacation: Days: 12, People: 3
This is the code that follows
I have to put my solution directly underneath where it asks for it, so I tried this but it sets the "People" value in the second vacation to 0.
Idk what to do so that the number of people doesn't change when I update numDays. Also, if I use anything involving "return" at the very end, it leads to an error. Any help/guidance would be appreciated.
Overload the + operator as indicated. Sample output for the given program:
First vacation: Days: 7, People: 3
Second vacation: Days: 12, People: 3
This is the code that follows
Code:
#include <iostream>
using namespace std;
class FamilyVacation{
public:
void SetNumDays(int dayCount);
void SetNumPeople(int peopleCount);
void Print() const;
FamilyVacation operator+(int moreDays);
private:
int numDays;
int numPeople;
};
void FamilyVacation::SetNumDays(int dayCount) {
numDays = dayCount;
return;
}
void FamilyVacation::SetNumPeople(int peopleCount) {
numPeople = peopleCount;
return;
}
// FIXME: Overload + operator so can write newVacation = oldVacation + 5,
// which adds 5 to numDays, while just copying numPeople.
/* Your solution goes here */
void FamilyVacation::Print() const {
cout << "Days: " << numDays << ", People: " << numPeople << endl;
return;
}
int main() {
FamilyVacation firstVacation;
FamilyVacation secondVacation;
cout << "First vacation: ";
firstVacation.SetNumDays(7);
firstVacation.SetNumPeople(3);
firstVacation.Print();
cout << "Second vacation: ";
secondVacation = firstVacation + 5;
secondVacation.Print();
return 0;
}
I have to put my solution directly underneath where it asks for it, so I tried this but it sets the "People" value in the second vacation to 0.
Code:
FamilyVacation FamilyVacation::operator+(int moreDays){
moreDays = numDays + 5;
}
Idk what to do so that the number of people doesn't change when I update numDays. Also, if I use anything involving "return" at the very end, it leads to an error. Any help/guidance would be appreciated.