operator overloading +, -, and =

i know i don't need to overload the = operator because of memberwise assignment but i was wondering why this code will work correctly if i dont overload the = operator and not work correctly if i don't overload it. I'm pretty sure i wrote the code right for it but i don't get why it doesn't work.

header file:
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
#ifndef FEETINCHES_H
#define FEETINCHES_H

class FeetInches
{
private:
	int feet;
	int inches;
	static int objectCount;
	void simplify();
	
public:
	// default constructor
	FeetInches(int f = 0, int i = 0)
	{ feet = f;
	  inches = i;
	  objectCount++; }

	// mutator functions
	void setFeet(int f)
	{ feet = f; }

	void setInches(int i)
	{ inches = i;
	  simplify(); }

	// accessor functions
	int getFeet() const
	{ return feet; }

	int getInches() const
	{ return inches; }

	int getObjectCount() const
	{ return objectCount; }

	// overloaded operators
	FeetInches operator+(const FeetInches &);
	FeetInches operator-(const FeetInches &);
	//const FeetInches operator=(const FeetInches &);
};

#endif 


definition 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
#include "FeetInches.h"
#include <cstdlib>

int FeetInches::objectCount = 0;

void FeetInches::simplify()
{
	if(inches >= 12)
	{
		feet += inches/12;
		inches = (inches % 12);
	}
	else if(inches < 0)
	{
		feet -= (abs(inches)/12 + 1);
		inches = 12 - (abs(inches) % 12);
	}
}

FeetInches FeetInches::operator+(const FeetInches & right)
{
	FeetInches temp;

	temp.inches = inches + right.inches;
	temp.feet = feet + right.feet;
	temp.simplify();

	return temp;
}

FeetInches FeetInches::operator-(const FeetInches & right)
{
	FeetInches temp;

	temp.inches = inches - right.inches;
	temp.feet = feet - right.feet;
	temp.simplify();

	return temp;
}

/*const FeetInches FeetInches::operator=(const FeetInches & right)
{
	feet = right.feet;
	inches = right.inches;
	return *this;
}*/


main:
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
#include <iostream>
#include "FeetInches.h"

using namespace std;

int main()
{
	int feet, inches;

	FeetInches first, second, third;

	cout << "Enter a distance in feet and inches: ";
	cin >> feet >> inches;

	first.setFeet(feet);
	first.setInches(inches);

	cout << "Enter another distance in feet and inches: ";
	cin >> feet >> inches;

	second.setFeet(feet);
	second.setInches(inches);

	third = first + second;

	cout << "first + second = ";
	cout << third.getFeet() << " feet, ";
	cout << third.getInches() << " inches.\n";

	third = first - second;

	cout << "first - second = ";
	cout << third.getFeet() << " feet, ";
	cout << third.getInches() << " inches.\n\n";

	cout << "There were " << first.getObjectCount() << " objects\n";

	return 0;
}


note: i commented out the operator= because it apparently doesn't work
Your assignment operator does almost nothing that the default doesn't already do, so it should work just fine.

Define "doesn't work". Compiler error? Program crash? what?


The only thing I see is that you're returning an object copy from your assignment operator (when typically you should return a reference). But since you don't have a copy ctor I don't see why that would matter.
What Disch said. What exactly is your problem with that?
alright i don't know why it was'nt working before but it works now. Before it would compile and run just fine but i would get different answers when inputting the same data (compared to not using the overloaded = operator)

and now im getting the same answers so it works
Last edited on
and why would i need to return by reference? wouldn't return *this work just the same?
Yeah it'll work. It's just mostly a performance thing.

Returning by value returns a copy. This potentially creates another object, copies data to it, then (if it's not used) destroys it. Since the return value of assignment operators are rarely used, it's kind of a waste.
icic, so is the return value only used on something like this:
 
var1 = var2 = var3;
That's one way to do it, yeah. Another way is something like this:

 
if( (var1 = var2) == var3 )
Topic archived. No new replies allowed.