Help with Operator overloading

Hello I'm trying to use operator overloading to add the values stored in two objects. I'm trying add all the int contents in the program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<string>
using namespace std;
class juice
{
	int sugar;
	string flavour;
	int size;
	int amount_of_water;
public:
	juice()
	{
		sugar = 0;
		flavour = "Nil";
		size = 0;
		amount_of_water = 0;
	}
	friend juice operator + (juice const &j1, juice const &j2) {
		if (j1.flavour != j2.flavour) {
			cout << "Flavours should be same";
		}
		else
		{
			return juice(j1.size + j2.size);	
		}
	}
	juice(int s, string f, int si, int a)
	{
		sugar = s;
		flavour = f;
		size = si;
		amount_of_water = a;
	}
	juice(const juice &a)
	{
		sugar = a.sugar;
		flavour = a.flavour;
		size = a.size;
		amount_of_water = a.amount_of_water;
	}
	void showjuice()
	{
		cout << "Sugar of juice is " << sugar << endl;
		cout << "Flavour of juice is " << flavour << endl;
		cout << "Size of juice is " << size << endl;
		cout << "Amount of water in juice is " << amount_of_water << endl;
	}
};
void main()
{
	juice j1(5, "Mango", 1, 1);
	juice j2(2, "Orange", 1, 1);
	juice j3(3, "Grapes", 1, 1);
	juice j4 = j1 + j2;
	j1.showjuice();
	j2.showjuice();
	j3.showjuice();
	j4.showjuice();
}
Last edited on
1
2
3
4
if (j1.flavour != j2.flavour) 
{
    cout << "Flavours should be same";
}

You need to return sth even when the flavours are not the same or throw an exception.
Thank you for your reply but I think I'm making a syntax error (sorry I'm new to operator overloading).

1
2
3
4
5
6
7
8
9
10
11
friend juice operator + (juice const &j1, juice const &j2)
	{
		if (j1.flavour != j2.flavour) {
			cout << "Flavours should be same";
			return 0;
		}
		else
		{
			return juice(j1.size + j2.size);
		}
	}


When i return it I am getting an error on both. I'm getting the following errors.

'<function-style-cast>': cannot convert from 'int' to 'juice'
'return': cannot convert from 'int' to 'juice'

What should I change to fix these errors?
You need to return a juice not an int from this function since that's what you told the compiler.

What is the purpose of this overload? Does adding one juice to another juice actually make sense?

Well the question was to add the size, amount of water and sugar through operator overloading which are the part of an object like if I add j1 twice then size becomes 2 sugar 10 and amount of water 2 aswell.
This seems like an abuse of operator overloading.

return juice(j1.size + j2.size);
You don't have a constructor that only takes in an int. You either have a default constructor (zero arguments), or you have a constructor that takes in juice(int s, string f, int si, int a).
You're adding the sizes together, but you're forgetting about everything else
1
2
3
4
	int sugar;
	string flavour;
	int size;
	int amount_of_water;

You have to add those together as well.

You could just concatenate the flavours together if you want.
1
2
3
4
5
6
juice(
  j1.sugar + j2.sugar,
  j1.flavour + j2.flavour,
  j1.size + j2.size,
  j1.amount_of_water + j2.amount_of_water
);


Otherwise, it's not clear what you want the operator+ to do when the juices are different flavours.

Perhaps just return a default construction (return juice();) if the flavours are incompatible, and have the user check for an error.

1
2
3
juice j4 = j1 + j2;
if (j4.flavour == "Nil")
    std::cout << "You're only supposed to add like flavours together!" << std::endl;
Last edited on
Worked thank you for your help everyone.
Topic archived. No new replies allowed.