Question

Create a new class called Calculator. A calculator should be able to add, subtract, multiply, divide...

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

c++

Homework Answers

Answer #1

//header file
#include<iostream>
using namespace std;
//class Calculator
class Calculator
{
//instance variable number which will store the calculated result
float number;
public:
//default constructor
Calculator()
{
//setting number to 0.0
number=0.0;
}
//parameterized constructor
Calculator(float n)
{
//setting number to argument n
number=n;
}
//add function
void add(float a)
{
//adding parameter a to number and storing back in number
number=number+a;
}
//subtract function
void subtract(float a)
{
//subtracting parameter a from number and storing back in number
number=number-a;
}
//divide function
void divide(float a)
{
//dividing number by parameter a and storing back in number
number=number/a;
}
//multiply function
void multiply(float a)
{
//multiplying parameter a by number and storing back in number
number=number*a;
}
//display function
float display()
{
//returning number
return number;
}
//clear function
void clear()
{
//setting number to 0 so as to clear it
number=0.0;
}
};//class ends

//main function
int main()
{
//creating object mycalc of calculator class
Calculator mycalc;
//clear its value
mycalc.clear();
//calling add function by passing 4.52 to it
mycalc.add(4.52);
//calling add function by passing 3.789 to it
mycalc.add(3.789);
//calling divide function by passing 2.6 to it
mycalc.divide(2.6);
//calling multiply function by passing 3.12to it
mycalc.multiply(3.12);
//calling subtract function by passing 2.678 to it
mycalc.subtract(2.678);
//calling the display function and printing the returned value
cout << mycalc.display() << endl; // prints out "7.2928"
//clear the number value for mycalc object
mycalc.clear();
//calling add function by passing 5.2 to it
mycalc.add(5.0);
//calling the display function and printing the returned value
cout << mycalc.display() << endl; // prints out "5"
//advanced stuff #1: add a constructor
//creating object calc1 of Calculator class
Calculator calc1;
//calling the display function using calc1 and printing the returned value
cout << calc1.display() << endl; //prints out 0

//advanced stuff #2: add a parameterized constructor
//creating object calc2 of Calculator class by passing it 5
Calculator calc2(5);
//calling the display function using calc2 and printing the returned value
cout << calc2.display() << endl; //prints out 5
return 0;
}//main ends

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
PHP calculator problem Create a calculator class that will add, subtract, multiply, and divide two numbers....
PHP calculator problem Create a calculator class that will add, subtract, multiply, and divide two numbers. It will have a method that will accept three arguments consisting of a string and two numbers example ("+", 4, 5) where the string is the operator and the numbers are what will be used in the calculation. It doesn't need an HTML form, all the arguments are put in through the method. The class must check for a correct operator (+,*,-,/), and a...
I need the java code for a 4-function calculator app on android studio. Please make sure...
I need the java code for a 4-function calculator app on android studio. Please make sure all the requirements shown below are followed (such as the error portion and etc). The topic of this app is to simply create a 4 function calculator which calculates math expressions (add, subtract, multiply, and divide numbers). The requirements are the following : - The only buttons needed are 0-9, *, /, +, -, a clear, and enter button - Implement the onclicklistener on...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that declares and implements the IntNode class (just copy it exactly as it is below) as well as declares the IntList Class interface only. You are also required to come up with a separate implementation file (IntList.cpp) that implements the member functions of the IntList class. While developing your IntList class you must write your own test harness (within a file named main.cpp). Never implement...
This will be my third time submitting this question. THERE SHOULD BE NO USE OF CSS...
This will be my third time submitting this question. THERE SHOULD BE NO USE OF CSS OR SWITCH STATEMENTS IN THE JAVASCRIPT. Even though there is stylesheet in the HTML file do no create a new one. Project Standards: Students will use click events to capture user input. Students will use variables to store information needed by their application and keep track of their program’s state. Students will use conditionals to control project flow. Project Task You will be building...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write an implementation file to implement the namespace declared in the attached CSCI361Proj5.h. Name the implementation file as YourNameProj5.cpp and add it to the project. Run the project to see your grade. .h file: // Provided by: ____________(your name here)__________ // Email Address: ____________(your email address here)________ // FILE: link.h // PROVIDES: A toolkit of 14 functions for manipulating linked lists. Each // node of...