I need help with this assignment. Define a class RINT (restricted integer) that
ID: 672243 • Letter: I
Question
I need help with this assignment.
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.
There is another file to test but the code should work initially with the main function above.
Explanation / Answer
#pragma once
#include <iostream>
class RINT
{
public:
RINT(int);
RINT(void);
RINT operator--();
RINT operator+(RINT);
RINT operator++ () ;
RINT operator-- (int);
RINT operator+= (int);
RINT operator-= (int);
RINT operator-(RINT);
RINT operator=(int);
ostream &operator<<(RINT &c);
istream & operator>>(RINT &c);
~RINT(void);
private:
int a;
};
#include "stdafx.h"
#include "RINT.h"
#include <iostream>
using namespace std;
RINT::RINT()
{
a = NULL ;
}
RINT::RINT(int num)
{
if(a == NULL)
{
a = 0;
}
else
{
a = num;
}
}
RINT RINT::operator+(RINT num)
{
RINT ans = *this + num;
return ans;
}
RINT RINT::operator-(RINT num)
{
RINT ans = *this - num;
return ans;
}
RINT RINT::operator=(int num)
{
RINT ans = num;
return ans;
}
RINT RINT::operator+= (int num)
{
RINT ans = num + a;
return ans;
}
RINT RINT::operator-= (int num)
{
RINT ans = a - num;
return ans;
}
ostream &operator<<(ostream &out, const RINT &obj)
{
obj.print(out);
}
RINT::~RINT(void)
{
}
#include <iostream>
#include "RINT.h"
using namespace std;
int main()
{
RINT x, y = 4;
int a = 5, b = 2;
cout << x << endl;
cin >> x;
x = x + 1;
y = x - a;
return 0;
}