C/C++ How can I overload the + operator in C++ for a family vacation program?

  • Thread starter Thread starter sxal96
  • Start date Start date
  • Tags Tags
    C++ Operator
AI Thread Summary
The discussion centers around implementing operator overloading for the `+` operator in a `FamilyVacation` class, specifically to add days to a vacation while retaining the number of people. The user initially struggles with their implementation, which incorrectly modifies a parameter instead of the class attributes and fails to return a new instance of `FamilyVacation`. Key points include the need to create a new instance to hold the modified values and properly copy the `numPeople` attribute from the original object. Suggestions emphasize that the overloaded operator function must return a `FamilyVacation` object, initialized with the correct number of days and copied number of people. The user is guided to declare a `result` object, set its properties, and return it, clarifying that simply modifying input parameters does not achieve the desired outcome.
sxal96
Messages
15
Reaction score
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
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.
 
Technology news on Phys.org
Hi moofasa! Welcome to MHB! (Smile)

The FIXME comment says: "which adds 5 to numDays, while just copying numPeople."

However, in your solution 'numDays' is not changed. Instead 'moreDays' gets changed. But that's only a parameter that gets discarded afterwards.
Furthermore, 'numPeople' is not copied. (Worried)
 
I like Serena said:
Hi moofasa! Welcome to MHB! (Smile)

The FIXME comment says: "which adds 5 to numDays, while just copying numPeople."

However, in your solution 'numDays' is not changed. Instead 'moreDays' gets changed. But that's only a parameter that gets discarded afterwards.
Furthermore, 'numPeople' is not copied. (Worried)

Thank you!

How would I copy numPeople? Anytime I try incorporating 'numPeople' into my solution, I get an error saying that numPeople isn't declared or that it could not be converted.
 
moofasa said:
How would I copy numPeople? Anytime I try incorporating 'numPeople' into my solution, I get an error saying that numPeople isn't declared or that it could not be converted.

We need a new instance of FamilyVacation that we initialize with the proper values.
We can do that by declaring [m]FamilyVacation result;[/m], initializing it, and returning it at the end.
 
I like Serena said:
We need a new instance of FamilyVacation that we initialize with the proper values.
We can do that by declaring [m]FamilyVacation result;[/m], initializing it, and returning it at the end.

I don't really understand operator overloading at all but this is my attempt that led to an error

Code:
class FamilyVacation result{
   public: 
      void result(int peopleCount, numPeople);
   private:
};

void FamilyVacation FamilyVacation::result(int peopleCount, int numPeople){
   peopleCount = numPeople;
   return;
}

FamilyVacation FamilyVacation::operator+(int moreDays){
   moreDays = numDays + 5;
}
 
moofasa said:
I don't really understand operator overloading at all but this is my attempt that led to an error

Code:
FamilyVacation FamilyVacation::operator+(int moreDays){
   moreDays = numDays + 5;
}

Consider the function [m]FamilyVacation FamilyVacation::operator+(int moreDays)[/m].
It has 'moreDays' as input.
Changing that input won't have any effect.

More importantly, the function is supposed to return a [m]FamilyVacation[/m].
As it is now, it's not returning anything, which is already an error condition.

It should be something like:
Code:
FamilyVacation FamilyVacation::operator+(int moreDays){
   FamilyVacation result;
   ...
   return result;
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top