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

In summary: Consider the function FamilyVacation FamilyVacation::operator+(int moreDays).It has 'moreDays' as input.Changing that input won't have any effect.More importantly, the function is supposed to return a FamilyVacation.As it is now, it's not returning anything, which is already an error condition.
  • #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
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
  • #2
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)
 
  • #3
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.
 
  • #4
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.
 
  • #5
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;
}
 
  • #6
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::eek:perator+(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;
}
 

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

What is operator overloading in C++?

Operator overloading in C++ refers to the ability to redefine the behavior of an operator when it is used with user-defined data types, such as classes or structs. This allows for more intuitive and natural syntax for operations on these types.

Why is operator overloading important in C++?

Operator overloading in C++ is important because it allows for more flexibility and readability in code. By redefining the behavior of operators for user-defined types, programmers can create more intuitive and natural syntax for operations on these types.

What are the limitations of operator overloading in C++?

There are a few limitations to operator overloading in C++, such as the inability to create new operators, the inability to change the number of operands an operator takes, and the inability to change the precedence or associativity of operators.

How is operator overloading different from function overloading in C++?

Operator overloading in C++ is different from function overloading in that it specifically refers to redefining the behavior of operators, while function overloading refers to creating multiple functions with the same name but different parameters. Operator overloading allows for more intuitive syntax for operations on user-defined types, while function overloading allows for more flexibility in how functions are called.

What are some common operators that can be overloaded in C++?

Some common operators that can be overloaded in C++ include arithmetic operators (+, -, *, /), comparison operators (==, !=, <, >), and stream operators (<<, >>). However, almost any operator can be overloaded in C++, as long as it is used with a user-defined type.

Similar threads

  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
8K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
2
Replies
53
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
Back
Top