Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part A. Define a class INT that behaves like an int (i.e. can be used in express

ID: 672034 • Letter: P

Question

Part A. Define a class INT that behaves like an int (i.e. can be used in expressions with int values). To do so, you will need to define an INT::operator int() conversion operator. Also implement the postfix and prefix ++ and -- operators, and the += and -= assignment operators.

As an example, the following code would work if the class is defined correctly.

// note - you may need to change the definition of the main function to // be consistent with what your C++ compiler expects.

int main()

{

INT x;

INT y = 6;

int a = 5, b = 2;

x = a + b;

x += 43;

x -= 3;

cout << "x = " << x << " a = " << a << endl;

a = x + --y;

b = x++;

cout << "x = " << x << " y = " << y << endl;

cout << "a = " << a << " b = " << b << endl;

cout << "The absolute value of x is " << abs(x) << endl;.

}

Part B.

Define a class RINT (restricted integer) that behaves like an int except that the only operations allowed are + and - (both unary and binary operators), = for assignment of an int to an RINT, and overloaded stream input >> and output << operators for RINT values. Do not define RINT::operator int(). After you have the test execution output, uncomment the last lines in main, compile again, and also include the compiler error messages.

As an example, the following code would work if the class is defined correctly. // note - you may need to change the definition of the main function to // be consistent with what your C++ compiler expects.

int main()

{

RINT x, y = 4;

int a = 5, b = 2;

cout << x << endl;

cin >> x;

x = x + 1;

y = x - a;

//x++;

// should generate error

//y += 3;

// should generate error

}

Explanation / Answer

Program1:

#include <iostream>
#include <Math.h>
using namespace std;

class INT {
public:
int x;
INT(){}
INT(int x) {
this->x = x;
}
int operator+(INT y)
{
   return x-y.x;
   }
int operator+=(INT y)
{
   this->x+=y.x;
   return this->x;
   }
   int operator-=(INT y)
{
   this->x-=y.x;
   return this->x;
   }
  
   int operator++(int)
{
   return x++;
   }
  
   int operator--()
{
return x--;
   }
   friend ostream &operator<<( ostream &output, const INT &X )
{
output << X.x;
return output;
}
friend int abs(INT& X){
   return abs(X.x);
   }
};

int main()
{
INT x;
INT y = 6;
int a = 5, b = 2;
x = a + b;
x += 43;
x -= 3;
cout<< "x = " <<x<<" a = "<<a<< endl;
a =x + --y;
b = x++;
cout << "x = " << x << " y = " << y << endl;
cout << "a = " << a << " b = " << b << endl;
cout << "The absolute value of x is " << abs(x) << endl;
return 0;
}

--------------------------------

output1:

x = 47 a = 5
x = 48 y = 5
a = 41 b = 47
The absolute value of x is 48

--------------------------------

program2:

#include <iostream>
#include <Math.h>

using namespace std;

class RINT {
public:
int d;
RINT(){}
RINT(int x) {
this->d = x;
}
int operator+(int y)
{
   return (d+y);
   }

   int operator-(int y)
{
   return (d-y);
   }

   friend ostream &operator<<( ostream &output, const RINT &X )
{
output << X.d;
return output;
}
friend istream &operator>>( istream &input, RINT &X )
{
input >> X.d;
return input;
}
friend int abs(RINT& X){
   return abs(X.d);
   }
};

int main()
{
RINT x, y = 4;
int a = 5, b = 2;
cin >> x;
cout << x << endl;
x = x + 1;
y = x - a;
//x++;
// should generate error RINTPrg.cpp   [Error] no 'operator++(int)' declared for postfix '++' [-fpermissive]
//y += 3;
// should generate error RINTPrg.cpp   [Error] no match for 'operator+=' (operand types are 'RINT' and 'int')
return 0;
}

--------------------------------

output2:

100

100