Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need help with a computer science C++ lassignment we need to Create a new class

ID: 3887234 • Letter: N

Question

need help with a computer science C++ lassignment

we need to

Create a new class called Calculator. A calculator should be able to add, subtract, multiply, divide and clear. Test your calculator by writing a main program incorporating the test code below:

Calculator mycalc;
mycalc.clear();
mycalc.add(4.52);
mycalc.add(3.789);
mycalc.divide(2.6);
mycalc.multiply(3.12);
mycalc.subtract(2.678);
cout << mycalc.display() << endl;       // prints out "7.2928"
mycalc.clear();
mycalc.add(5.0);
cout << mycalc.display() << endl;       // prints out "5"

//advanced stuff #1: add a constructor

Calculator calc1;

cout << calc1.display() << endl;  //prints out 0

//advanced stuff #2: add a parameterized constructor

Calculator calc2(5);

cout << calc2.display() << endl; //prints out 5

//advanced stuff #3: Define calculator addition (overload the '+' operator) (see: http://lmgtfy.com/?q=+c%2B%2B+operator+overloading)

Calculator calc3(7);

calc1 = calc2 + calc3;

cout << calc1.display() << endl;  //prints out 12

//advanced stuff #4 (10 points extra credit): Create an 'undo' method for the calculator

mycalc.undo();

mycalc.undo();

cout << mycalc.display()<< endl;  //prints out 7.2928

//advanced stuff #5 (10 points extra credit): Create a 'redo' method for the calculator

mycalc.redo();

mycalc.redo();

cout << mycalc.display()<< endl;  //prints out 5

Explanation / Answer

Note: I will provide the implementations for the undo and redo methods also...Thank You...

__________________

#include <iostream>

#include <iomanip>

using namespace std;

class Calculator

{

private:

//Declaring variable

double num;

public:

//Zero argumented constructor

Calculator()

{

this->num=0;

}

//Parameterized constructor

Calculator(double val)

{

this->num=val;

}

//This method will sets the variable to zero

void clear()

{

this->num=0;

}

//This method will performs addition

void add(double val)

{

num+=val;

}

//This method will performs division

void divide(double val)

{

num/=val;

}

//This method will performs multiplication

void multiply(double val)

{

num*=val;

}

//This method will performs subtraction

void subtract(double val)

{

num-=val;

}

//This method will displays the variable value

double display()

{

return num;

}

Calculator operator+(Calculator c)

{

Calculator a;   

a.num=num+c.num;

return a;

}

};

int main()

{

Calculator mycalc;

mycalc.clear();

mycalc.add(4.52);

mycalc.add(3.789);

mycalc.divide(2.6);

mycalc.multiply(3.12);

mycalc.subtract(2.678);

cout << mycalc.display() << endl; // prints out "7.2928"

mycalc.clear();

mycalc.add(5.0);

cout << mycalc.display() << endl; // prints out "5"

// advanced stuff #1: add a constructor

Calculator calc1;

cout << calc1.display() << endl; // prints out 0

// advanced stuff #2: add a parameterized constructor

Calculator calc2(5);

cout << calc2.display() << endl; // prints out 5

// advanced stuff #3: Define calculator addition (overload the '+' operator) (see:

// http://lmgtfy.com/?q=+c%2B%2B+operator+overloading)

Calculator calc3(7);

calc1 = calc2 + calc3;

cout << calc1.display() << endl; // prints out 12

/*

// advanced stuff #4 (10 points extra credit): Create an 'undo' method for the calculator

mycalc.undo();

mycalc.undo();

cout << mycalc.display() << endl; // prints out 7.2928

// advanced stuff #5 (10 points extra credit): Create a 'redo' method for the calculator

mycalc.redo();

mycalc.redo();

cout << mycalc.display() << endl; // prints out 5

*/

return 0;

}

_______________________