I am working on an assignment that basically requires a class INT that operates similar to the int standard class... Below is my code. All seems to be working, with the exception of handling this line:
INT x,y = 6;
should assign 6 to x and y. Assigns only to y. A point in the right direction would be appreciated.
// --INT.h header file--
#ifndef _INT_H
#define _INT_H
#include<iostream>
using std::ostream;
using std::istream;
class INT
{
friend ostream &operator<<(ostream &, const INT &); // << ostream
friend istream &operator>>(istream &, const INT &); // >> istream
public:
INT(int a = 0); // default ctor
INT(const INT&); // copy ctor
~INT(); // dtor
operator int();
const INT &operator =( const int & );
const INT &operator ,( const INT & );
const INT &operator +( const int & );
const INT &operator +( const INT & );
const INT &operator -( const int & );
const INT &operator -( const INT & );
const INT &operator *( const int & );
const INT &operator /( const int & );
INT &operator ++();
INT operator ++(int);
INT &operator --();
INT operator --(int);
const INT &operator +=( const INT & );
const INT &operator -=( const INT & );
private:
int X;
};
#endif // _INT_H
// INT.cpp file
#include "INT.h"
#include <iostream>
INT::INT(int a)
{
X = a;
}
INT::INT(const INT &a)
{
X = a.X;
}
INT::~INT()
{
}
INT::operator int()
{
return X;
}
const INT &INT::operator = ( const int &a)
{
X = a;
return *this;
}
const INT &INT::operator , ( const INT &a)
{
X = a.X;
return *this;
}
const INT &INT::operator + ( const int &a)
{
return X + a;
}
const INT &INT::operator + ( const INT &a)
{
INT temp = *this;
temp.X = X + a.X;
return temp;
}
const INT &INT::operator - ( const int &a)
{
return X - a;
}
const INT &INT::operator - ( const INT &a)
{
return X - a.X;
}
const INT &INT::operator * ( const int &a)
{
return X * a;
}
const INT &INT::operator / ( const int &a)
{
return X / a;
}
INT &INT::operator ++ ()
{
X = X + 1;
return *this;
}
INT INT::operator ++ (int a)
{
INT temp = *this;
X = X + 1;
return temp;
}
INT &INT::operator -- ()
{
X = X - 1;
return *this;
}
INT INT::operator -- (int a)
{
INT temp = *this;
temp.X = X - 1;
return temp;
}
const INT &INT::operator += ( const INT &a)
{
X = X + a.X;
return *this;
}
const INT &INT::operator -= ( const INT &a)
{
X = X - a.X;
return *this;
}
ostream &operator <<(ostream &output, const INT &a)
{
output << a.X;
return output;
}
istream &operator >>(istream &input, const INT &a)
{
input >> a.X;
return input;
}
// INTDemo.cpp - INT class test code
#include "INT.h"
#include <iostream>
using std::cout;
using std::endl;
#include <cstdlib>
using std::abs;
int main()
{
cout << endl << "A test of the INT class" << endl << endl;
INT x,y = 6;
INT w = 9;
int a = 5, b = 2;
INT z = y;
cout << "Testing default and copy ctor: " << endl;
cout << "--------------------------------------- " << endl;
cout << "If INT x,y = 6; then x = "<< x << " and y = " << y << endl;
cout << "If INT w = 9 then w = " << w << endl;
cout << "If INT z = y; (z is instantiated as y) z = " << z << endl;
cout << "Here are the ints a = "<< a << " and b = " << b << endl;
cout << endl;
cout << "Testing overloaded cast (int) operator: " << endl;
cout << "--------------------------------------- " << endl;
cout << "1: adding ints and INTS" << endl;
x = y + z;
cout << "If x = y + z, then x = "<< x << " (should be 12)" << endl;
x = a + y;
cout << "If x = a + y, then x = "<< x << " (should be 11)" << endl;
x = y + b;
cout << "If x = y + b, then x = "<< x <<" (should be 8)" << endl;
cout << endl <<"2: subtracting ints and INTS" << endl;
x = w - z;
cout << "If x = w - z, then x = "<< x <<" (should be 3)" << endl;
x = y - b;
cout << "If x = y - b, then x = "<< x <<" (should be 4)" << endl;
x = a-y;
cout << "If x = a-y, then x = "<< x <<" (should be -1)" << endl;
cout << endl << "3: multiplying ints and INTS" << endl;
x = w * z;
cout << "If x = w * z, then x = "<< x <<" (should be 54)" << endl;
x = y * b;
cout << "If x = y * b, then x = "<< x <<" (should be 12)" << endl;
x = a*y;
cout << "If x = a*y, then x = "<< x <<" (should be 30)" << endl;
cout << endl << "4: dividing ints and INTS" << endl;
x = w / z;
cout << "If x = w / z, then x = "<< x <<" (should be 1)" << endl;
x = y / b;
cout << "If x = y / b, then x = "<< x <<" (should be 3)" << endl;
x = (a + 7)/y;
cout << "If x = (a + 7)/y, then x = "<< x <<" (should be 2)" << endl;
cout << endl;
cout << endl << "5: assignment" << endl;
a = x;
cout << "If a = x then a = "<<a<< " (should be 2) " <<endl;
x = 5;
cout << "If x = 5 then x = "<<x<< " (should be 5) " <<endl;
cout << endl << "6: unary +-" << endl;
x = +a;
cout << "If x = +a then x = "<< x << " (should be 2) " <<endl;
x = -a;
cout << "If x = -a then x = "<< x << " (should be -2) " <<endl;
cout << endl;
cout << "Testing increment/decrement operators: " << endl;
cout << "--------------------------------------- " << endl;
x = 5;
cout << "Set x = 5 " << endl;
b= x;
b = x++;
cout << "b = x++ so b = " << b << " (should be 5)" << endl
<< "\tand now x = " <<x <<" (should be 6) " << endl;
b = ++x;
cout << "b = ++x so b = " << b << " (should be 7)" << endl
<< "\tand now x = " << x << " ( should be 7)" << endl;
b = --x;
cout << "b = --x so b = " << b << " (should be 6)" << endl
<< "\tand now x = " <<x <<" (should be 6) " << endl;
b = x--;
cout << "b = x-- so b = " << b << " (should be 6)" << endl
<< "\tand now x = " <<x <<" (should be 5) " << endl;
cout << endl;
cout << "Testing combined assignment operators: " << endl;
cout << "-------------------------------------- " << endl;
x = 5;
x += 6;
cout << "If set x = 5 then x += 6 so x now equals "
<< x << " (should be 11)" << endl;
a = 13;
// a += x + 43;
// cout << "If set a = 13 then a += x + 43 ; therefore a now equals " << a << " (should be 67)" << endl;
x = 33;
x -= 17;
cout << "If set x = 33 then x -= 17 so x now equals " << x << " (should be 16)" << endl;
cout << endl;
cout << "Testing call to functions: " << endl;
cout << "-------------------------------------- " << endl;
cout << "The absolute value of x is " << abs (x) << endl;
cout << endl << "Done..." << endl;
system("pause");
return 0;
}