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:
#ifndef _MYINTTHIS_H
#define _MYINTTHIS_H
#include <iostream>
usingnamespace 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
booloperator<(myint); // Operator constructor for using less than
booloperator>(myint); // Operator constructor for using greater than
booloperator=(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
#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.
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().