Question
Assignment 4 - INT and RINT classes with overloaded operators Necessary skills: class design with operators, operator overloading Description Part A . Define a class
Assignment 4 - INT and RINT classes with overloaded operators
Necessary skills: class design with operators, operator overloading
Description
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;. }
Testing
Use the attached INTDemo.cpp source file to test your INT class.
// INTDemo.cpp - INT class test code // // Adjust the includes and main declaration for your development environment...
#include "stdafx.h"
#include "INT.h"
#include
#include
// note - you may need to change the definition of the main function to // be consistent with what your C++ compiler expects. int _tmain(int argc, _TCHAR* argv[]) { cout << endl << "A test of the INT class (07/05/13)" << 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 << " (should be 0 and 6)" << endl; cout << "If INT w = 9 then w = " << w << " (should be 9)" << endl; cout << "If INT z = y; (z is instantiated as y) z = " << z << " (should be 6)" << endl; cout << "Here are the ints a = "<< a << " and b = " << b <
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) " <
cout << endl << "6: unary +-" << endl; a = +x; cout << "If a = +x then a = "<< a << " (should be 5) and x = " << x << " (should be 5)" << endl; a = -x; cout << "If a = -x then a = "<< a << " (should be -5) and x = " << x << " (should be 5)" << endl;
cout << endl; cout << "Testing increment/decrement operators: " << endl; cout << "--------------------------------------- " << endl; x = 5; cout << "Set x = 5 " << endl; b = x++; cout << "b = x++ so b = " << b << " (should be 5)" << endl << "\tand now x = " <
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 = " <
b = x--; cout << "b = x-- so b = " << b << " (should be 6)" << endl << "\tand now x = " <
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 " <
cout << endl; cout << "Testing call to functions: " << endl; cout << "-------------------------------------- " << endl; cout << "The absolute value of x is " << abs (x) << endl;
cout << endl << "Done..." << endl;
return 0; }
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 }
Testing
Use the attached RINTDemo.cpp source file to test your RINT class.
// RINTDemo.cpp - RINT class test code // // Adjust the includes and main declaration for your development environment..
#include "stdafx.h" #include "RINT.h"
#include
// note - you may need to change the definition of the main function to // be consistent with what your C++ compiler expects. int _tmain(int argc, _TCHAR* argv[]) { cout << endl << "A test of the RINT class (2/10/13)" << endl << endl;
RINT x, y = 4; int a = 5, b = 2;
cout << " Here are the RINTs x = " << x << " and y = " << y << endl; cout << " Here are the ints a = " << a << " and b = " << b << endl << endl;
cout << " Testing stream input for RINT - enter an int value: "; cin >> x; cout << endl << "You entered an RINT value of " << x << endl;
cout << " Testing copy ctor" << endl; RINT z = y; cout << "If z is instantiated as y, z = " << z << " (should be 4)" << endl;
cout << " TESTING BINARY +" << endl; x = 5; y = 6; z = x + y; cout << "If z = x + y and x = " << x << " and y = " << y << " then z = " << z << " (should be 11)" << endl; z = x + 10; cout << "If z = x + 10 and x = " << x << " then z = " << z << " (should be 15)" << endl; z = 1 + x; cout << "If z = 1 + x and x = " << x << " then z = " << z << " (should be 6)" << endl;
cout << " TESTING BINARY -" << endl; x = 24; y = 30; z = x - y; cout << "If z = x - y and x = " << x << " and y = " << y << " then z = " << z << " (should be -6)" << endl; z = x - 10; cout << "If z = x - 10 and x = " << x << " then z = " << z << " (should be 14)" << endl; z = 1 - x; cout << "If z = 1 - x and x = " << x << " then z = " << z << " (should be -23)" << endl;
cout << " TESTING UNARY + - " << endl; x = 1;
x = -x; cout << "x = -x so x now = " << x << " (should be -1)" << endl; x = +x; cout << "x = +x so x now = " << x << " (should be -1)" << endl;
// TODO - after this test code runs successfully, uncomment the following // two lines, compile, and include the compile time errors with your output...
//x++; //generates compile error //y += 3; //generates compile error
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started