Problems with defining an increment function

First of all I'd like to say 95% of this is working perfectly, I just need a few small tweaks. I'm having a little trouble with defining an incrementing function and I need some opinions or suggestions for what I'm doing wrong here. When I call x++; in my main my value of x isn't changing. Here is my header, definitions, and my main program:


HEADER
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
#ifndef _MYINTTHIS_H
#define _MYINTTHIS_H
#include <iostream>
using namespace std;

class myint
{
	int myi, x, y, z;

public:
	myint (); // Default constructor
	myint (int); // Parameter constructor
	myint (const myint &); // Copy constructor
	void display(); // Display 
	myint operator+(myint); // Operator constructor for using a plus sign
	myint operator-(myint); // Operator constructor for using a minus sign
	myint operator*(myint); // Operator constructor for using a multiplication
	myint operator/(myint); // Operator constructor for using a division sign
	bool operator<(myint);  // Operator constructor for using less than
	bool operator>(myint);  // Operator constructor for using greater than
	bool operator=(myint);  // Operator constructor for using equals
	friend myint operator+(int, myint); // Friend function for addition
	myint operator++();     // Operator constructor for incrementing
	myint operator++(int notused); // Overloading the postfix form of ++
	myint operator--();     // Operator construcfor for decrementing
	myint operator--(int notused); // Overloading the postfix form of --
	void show();
};
#endif 


DEFINITIONS
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "myintThis.h"

myint::myint() // Default constructor
{
	this->myi = 0;
}

myint::myint(int x) // Parameter constructor: Taking in an integer and setting it to an attribute (x)
{
	this->myi = x;
}

myint::myint(const myint &x) // Copy constructor
{
	this->myi = x.myi;
}

void myint::display() // Display
{
	cout << this->myi << endl;
}

myint myint::operator+(myint x) // Constructor for using a plus sign
{
	myint y;
	y.myi = this->myi + x.myi;
	return y;
}

myint myint::operator-(myint x) // Constructor for using a minus
{
	myint y;
	y.myi = this->myi - x.myi;
	return y;
}

myint myint::operator*(myint x) // Constructor for using a multiplication sign
{
	myint y;
	y.myi = this->myi * x.myi;
	return y;
}

myint myint::operator/(myint x) // Constructor for using a division sign
{
	myint y;
	y.myi = this->myi / x.myi;
	return y;
}

bool myint::operator<(myint x) // Constructor for using less than
{
	if (this->myi < x.myi)
		return true;
	else
		return false;
}

bool myint::operator>(myint x) // Constructor for using greater than
{
	if (this->myi > x.myi)
		return true;
	else
		return false;
}

bool myint::operator=(myint x) // Constructor for using equals
{
	if (this->myi = x.myi)
		return true;
	else
		return false;
}

myint operator+(int x, myint y) // Constructor for the friend function of addition
{
	myint z;
	z.myi = x + y.myi;
	return z;
}

myint myint::operator++()     // Constructor for incrementing
{
	x++;
	y++;
	z++;
	return *this;
}

myint myint::operator++(int notused)
{
	myint temp = *this;
	x++;
	y++;
	z++;
	return temp;
}

myint myint::operator--()     // Constructor for decrementing
{
	x--;
	y--;
	z--;
	return *this;
}

myint myint::operator--(int notused)
{
	myint temp = *this;
	x--;
	y--;
	z--;
	return temp;
}

void myint::show()
{
	cout << x << ", ";
	cout << y << ", ";
	cout << z << "\n";
}


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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "myintThis.h"

int main()
{
	myint x(3);  // Creating a constructor 'x' with a value of 3
	myint z(5);  // Creating a constructor 'z' with a value of 5
	myint y(z);  // Creating a constructor 'y' copying the value from 'z'

	cout << "The value of x is: ";
	x.display(); // Will display 3
	cout << "The value of y is: ";
	z.display(); // Will display 5
	cout << "The value of z is: ";
	y.display(); // Will display 5
	
	cout << "\n\n";

	cout << "The value of x after x++: ";
	x++;
	x.display();

	cout << "\n\n";

	z = (x+y);   // Think as if x.operator+(y), x is calling the plus sign operator
	cout << "z = (x + y): ";
	z.display(); // Will display 8 (3+8)

	z = (x-y);   
	cout << "z = (x - y): ";
	z.display(); // Will display -2 (3-5)

	z = (x*y);   
	cout << "z = (x * y): ";
	z.display(); // Will display 15 (3*5)

	z = (x/y);   
	cout << "z = (x / y): ";
	z.display(); // Will display 0 (3/5)

	z = (7+y);   // Using the friend function for addition
	cout << "z = (7 + y): ";
	z.display(); // Will display 12 (7+5)

	cout << "\n\n";

	if (x < y)
		cout << "Testing if (x < y): True \n";
	else
		cout << "Testing if (x < y): False \n"; 

	
	 
	return 0;
}


So again, when I call x++; the value stays the same. Also, my x.show(); or y.show(); etc. I get huge negative values and I'm not sure what I'm doing wrong here. If anybody could hint me in the right direction that would be awesome.
bool operator=(myint);

I think what you meant here was

bool operator==(myint);

I think fixing that function should also fix this:

1
2
3
4
5
6
7
8
myint myint::operator++(int notused)
{
	myint temp = *this;
	x++;
	y++;
	z++;
	return temp;
}

I think what you meant here was
 
bool operator==(myint);


I think fixing that function should also fix this:
1
2
3
4
5
6
7
8
myint myint::operator++(int notused)
{
	myint temp = *this;
	x++;
	y++;
	z++;
	return temp;
}



Just tried this quick fix but I'm still getting 3 as the value of x after x++. Thanks for the quick response though
I notice in your increment and decrement functions, you are changing the values of the members x, y, and z. But in your display(), you are outputting the value of myi.

Since the increment and decrement functions don't change myi, then I don't see why calling them any number of times should change what is displayed with display().

You may also want to read this on operator overloading:
http://stackoverflow.com/questions/4421706/operator-overloading
Last edited on
I changed both of those functions to the following and they are now working:

1
2
3
4
void myint::display() // Display
{
	cout << this->myi << endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
myint myint::operator++()     // Constructor for incrementing
{
	this->myi++;
	return *this;
}

myint myint::operator++(int notused)
{
	myint tmp(*this);
        operator++();
        return tmp;
}


Thanks for your help fg109, much appreciated
Topic archived. No new replies allowed.