Question

In: Computer Science

You will work with MyInteger class (like an int, but it will be an object) by...

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;

}

Solutions

Expert Solution

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;
}

Related Solutions

C++ How to make this code take a class object instead of int for the vector...
C++ How to make this code take a class object instead of int for the vector queue and be able to enqueue and dequeue the object? #include <iostream> #include <float.h> #include <bits/stdc++.h> using namespace std; void print_queue(queue<int> Q) //This function is used to print queue { while (!Q.empty()) { cout<< Q.front() << " "; Q.pop(); } } int main() { int n = 10; vector< queue<int> > array_queues(n); //Create vector of queues of size 10, each entry has a queue...
Ruby programming I'd like you to create the class Holiday as described below. An object of...
Ruby programming I'd like you to create the class Holiday as described below. An object of class Holiday represents a holiday during a calendar year. This class has three private member variables: - mName, a string, representing the name of the holiday - mDay, an integer, representing the day of the month of the holiday - mMonth, a string, representing the month the holiday is in Create this class in the file Holiday.rb. Put your main driver code in the...
Person class You will implement the Person Class in Visual Studio. A person object may be...
Person class You will implement the Person Class in Visual Studio. A person object may be associated with multiple accounts. A person initiates activities (deposits or withdrawal) against an account that is captured in a transaction object. A short description of each class member is given below: Person Class Fields -   password : string Properties +   «C# property, setter private» IsAuthenticated : bool +   «C# property, setter absent» SIN : string +   «C# property, setter absent» Name : string Methods...
import java.util.*; class A { int i, j, k; public A(int i, int j, int k)...
import java.util.*; class A { int i, j, k; public A(int i, int j, int k) { this.i=i; this.j=j; this.k=k; } public String toString() { return "A("+i+","+j+","+k+")"; } } class Main { public static void main(String[] args) { ArrayList<A> aL=new ArrayList<A>(); Random rand= new Random(1000); //1000 is a seed value for (int p=0; p<10; p++) { int i = rand.nextInt(100); int j = rand.nextInt(200); int k = rand.nextInt(300); aL.add(new A(i, j, k)); } System.out.println("----- Original arraylist------"); for (A a: aL)...
Does the Ritz-Carlton work environment sound like the type of place you would like to work?...
Does the Ritz-Carlton work environment sound like the type of place you would like to work? Why or why not? 2. In an environment that acknowledges mistakes and encourages open communication, discuss the types of risks a team member might take to accommodate a guest? Make sure to check out the links to the full articles listed in the Source section for fuller-understanding. Please remember to vote your webpage and references.
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int sum = 0; int i; for(i = 0; i < arr.length; i++){ sum += arr[1]; } return sum; } public int getAverageValueInArray(int[] arr){ // return the average value of array elements int value = 0; for(int i = 0; i < arr.length; i++){ double average = value/ arr.length; } return value; } public int getNumberOfEvens(int[] arr){ //return the number of even values found in...
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
You will generate a People class of object and load an ArrayList with person objects, then...
You will generate a People class of object and load an ArrayList with person objects, then report the contents of that ArrayList. To do so, you must perform the following: (30 pts.) A) (15/30 pts. (line break, 11 pt) ) - Create a class file “People.java” (which will generate People.class upon compile). People.java will have eight(8) methods to 1) read a .txt file ‘people.txt’ 2) generate: ▪ List of all students AND teachers ▪ List of all students OR teachers...
In this assignment you are going to create a Student class to demonstrate a Student object....
In this assignment you are going to create a Student class to demonstrate a Student object. Then create a StudentTest class to place the main method in, which demonstrates the Student object. Student class should have the following instance variables: firstName, middleName, lastName (String) id (int) grade (int) Student class should have the following methods: Set and Get methods for each variable Constructor that sets all variables using Set methods that you defined. Design the application in main that you...
I am in a statistics class. When I graduate, I would like to work in an...
I am in a statistics class. When I graduate, I would like to work in an area where I can help restructure businesses that are failing and make them successful. My professor has asked me where in my career that I think I will use the process of data collection. He has asked me to provide 2 examples. This is my first statistics class, so I don't really know of how to give him any examples. Can you please give...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT