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...
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)...
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 +...
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.
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...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2;...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2; long ris; public ProductThreads(String name, int [] v1, int [] v2, int begin, int end) { setName(name); this.v1 = v1; this.v2 = v2; this.begin = begin; this.end = end; this.ris = 0; } public void run() { System.out.println("Thread " + Thread.currentThread().getName() + "[" + begin + "," + end + "] started"); ris = 1; for(int i = begin; i <= end; i++) ris...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: //...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: // // 1. A constructor that takes a reference to an array, // and initializes an instance variable with this // array reference // // 2. An instance method named sum, which will calculate // the sum of the elements in the array. If the array // is empty (contains no elements), then this will // will return 0. You will need a loop for...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put code here return myint; } public int getInt() { int f = 0; // put code here return f; } } public class Assignment06 { public static void main(String args[]){ System.out.println("Assignmemnt 06 Written by Matt Weisfeld"); int myInt = 0; // create a Factorial object Factorial factorial = new Factorial(); // get a number from the console myInt = factorial.getInt(); System.out.println("Factorial of " +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT