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

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 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...
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 " +...
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...
Consider the following class: class Person {         String name;         int age;        ...
Consider the following class: class Person {         String name;         int age;         Person(String name, int age){                this.name = name;                this.age = age;         } } Write a java program with two classes “Teacher” and “Student” that inherit the above class “Person”. Each class has three components: extra variable, constructor, and a method to print the student or the teacher info. The output may look like the following (Hint: you may need to use “super”...
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...
Write a class Electionresults. Write the input inData (int a, int b) that creates (for example...
Write a class Electionresults. Write the input inData (int a, int b) that creates (for example by random method) polls and returns two-dimensional arrays containing polls, where as the first dimension is the number of a political party and the second dimension is the constituency code. The inData entry method includes integers, number of categories and number of constituencies. Write the program countingCategory that includes a two-dimensional state with data on the number of votes, and returns the total number...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT