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

  • Context: C/C++ 
  • Thread starter Thread starter sxal96
  • Start date Start date
  • Tags Tags
    C++ Operator
Click For Summary

Discussion Overview

The discussion revolves around the implementation of operator overloading for the + operator in a C++ class representing a family vacation. Participants are attempting to modify the number of vacation days while preserving the number of people, addressing issues related to variable scope and return values.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses difficulty in modifying the numDays without affecting numPeople, noting that their initial attempt leads to numPeople being set to 0.
  • Another participant points out that the parameter 'moreDays' is not the correct variable to modify and emphasizes that numPeople is not being copied in the current implementation.
  • A later reply suggests creating a new instance of FamilyVacation to hold the updated values, indicating that this instance should be initialized and returned at the end of the operator overload function.
  • Further attempts to define a new class or function structure are presented, but they lead to confusion and errors regarding variable declarations and function returns.
  • Participants highlight the need for the operator overload function to return a FamilyVacation object, which is currently missing in the attempts shared.

Areas of Agreement / Disagreement

Participants generally agree on the need to create a new instance of FamilyVacation to properly implement the operator overload, but there is no consensus on the exact implementation details or how to resolve the errors encountered.

Contextual Notes

There are unresolved issues regarding variable scope, the need for proper return types in the operator overload, and the correct way to copy class member variables. Some participants express confusion about operator overloading concepts, which may contribute to the errors in their implementations.

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;
}
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
53
Views
5K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
12K