In: Computer Science
You will work with MyInteger class (like an int, but it will be an object) by obtaining the two files, MyInteger.h and testMyInteger.cpp. Create the file MyInteger.cpp by implementing the member and friend functions for the class.
==============MyInteger.h==============
#ifndef MYINTEGER_H
#define MYINTEGER_H
#include <iostream>
using namespace std;
class MyInteger
{
public:
MyInteger(int v = 0);
void setValue(int v);
int getValue() const;
MyInteger operator+(const MyInteger &r) const;
MyInteger operator-(const MyInteger &r) const;
bool operator==(const MyInteger &r) const;
friend ostream &operator<<(ostream &out, const MyInteger &r);
friend istream &operator>>(istream &in, MyInteger &r);
private:
int value;
};
#endif
==============testMyInteger.cpp==============
// A driver to test MyInteger class
#include <iostream>
#include "MyInteger.h"
using namespace std;
int main()
{
MyInteger i1; // 0
MyInteger i2(5); // 5
MyInteger i3 = i2; // 5
cout << "i1: " << i1 << endl;
cout << "i2: " << i2.getValue() << endl;
cout << "i3: " << i3 << endl;
i1.setValue(-4);
i3 = i1 + i2;
cout << "i3: " << i3 << endl; // 1
cout << "i2 - i1: " << i2 - i1 << endl; // 9
cout << "Enter an integer: ";
cin >> i1; // enter 123
if (i1 == i2) // different
cout << "same" << endl;
else
cout << "different" << endl;
i2 = i1;
if (i1 == i2) // same
cout << "same" << endl;
else
cout << "different" << endl;
i2.setValue(25);
cout << "i1: " << i1 << endl; // 123
cout << "i2: " << i2 << endl; // 25
// feel free to add more test cases below
return 0;
}
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU
NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR
YOU
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MyInteger.cpp
#include "MyInteger.h"
void MyInteger::setValue(int v)
{
value = v;
}
int MyInteger::getValue() const
{
return value;
}
MyInteger MyInteger::operator+(const MyInteger &r) const
{
MyInteger m;
m.value = value + r.value;
return m;
}
MyInteger MyInteger::operator-(const MyInteger &r) const
{
MyInteger m;
m.value = value - r.value;
return m;
}
bool MyInteger::operator==(const MyInteger &r) const
{
if (value == r.value)
return true;
return false;
}
ostream &operator<<(ostream &out, const MyInteger &r)
{
out << r.value << endl;
return out;
}
istream &operator>>(istream &in, MyInteger &r)
{
in >> r.value;
return in;
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MyInteger.h
#ifndef MYINTEGER_H
#define MYINTEGER_H
#include <iostream>
using namespace std;
class MyInteger
{
public:
MyInteger(int v = 0){
value = v;
}
void setValue(int v);
int getValue() const;
MyInteger operator+(const MyInteger &r) const;
MyInteger operator-(const MyInteger &r) const;
bool operator==(const MyInteger &r) const;
friend ostream &operator<<(ostream &out, const MyInteger &r);
friend istream &operator>>(istream &in, MyInteger &r);
private:
int value;
};
#endif
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
testMyInteger.cpp
#include <iostream>
#include "MyInteger.cpp"
using namespace std;
int main()
{
MyInteger i1; // 0
MyInteger i2(5); // 5
MyInteger i3 = i2; // 5
cout << "i1: " << i1 << endl;
cout << "i2: " << i2.getValue() << endl;
cout << "i3: " << i3 << endl;
i1.setValue(-4);
i3 = i1 + i2;
cout << "i3: " << i3 << endl; // 1
cout << "i2 - i1: " << i2 - i1 << endl; // 9
cout << "Enter an integer: ";
cin >> i1; // enter 123
if (i1 == i2) // different
cout << "same" << endl;
else
cout << "different" << endl;
i2 = i1;
if (i1 == i2) // same
cout << "same" << endl;
else
cout << "different" << endl;
i2.setValue(25);
cout << "i1: " << i1 << endl; // 123
cout << "i2: " << i2 << endl; // 25
// feel free to add more test cases below
return 0;
}