Given main(), complete the Calculator class (in files Calculator.h and Calculator.cpp) that emulates basic functions of a calculator: add, subtract, multiple, divide, and clear. The class has one private data member called value for the calculator's current value. Implement the following constructor and public member functions as listed below: Calculator() - default constructor to set the data member to 0.0 void Add(double val) - add the parameter to the data member void Subtract(double val) - subtract the parameter from the data member void Multiply(double val) - multiply the data member by the parameter void Divide(double val) - divide the data member by the parameter void Clear( ) - set the data member to 0.0 double GetValue( ) - return the data member

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Answer this iin C++:

Given main(), complete the Calculator class (in files Calculator.h and Calculator.cpp) that emulates basic functions of a calculator: add, subtract, multiple, divide, and clear. The class has one private data member called value for the calculator's current value. Implement the following constructor and public member functions as listed below:

  • Calculator() - default constructor to set the data member to 0.0
  • void Add(double val) - add the parameter to the data member
  • void Subtract(double val) - subtract the parameter from the data member
  • void Multiply(double val) - multiply the data member by the parameter
  • void Divide(double val) - divide the data member by the parameter
  • void Clear( ) - set the data member to 0.0
  • double GetValue( ) - return the data member

Given two double input values num1 and num2, the program outputs the following values:

  1. The initial value of the data member, value
  2. The value after adding num1
  3. The value after multiplying by 3
  4. The value after subtracting num2
  5. The value after dividing by 2
  6. The value after calling the clear() method

Ex: If the input is:

10.0

5.0

the output is:

0.0

10.0

30.0

25.0

12.5

0.0

 

main.cpp

#include <iostream>
#include <iomanip>
#include "Calculator.h"
using namespace std;
 
int main() {
   Calculator calc;
double num1;
double num2; 
 
cin >> num1;
cin >> num2;
 
cout << fixed << setprecision(1);
// 1. The initial value
cout << calc.GetValue() << endl;
 
// 2. The value after adding num1
calc.Add(num1);
cout << calc.GetValue() << endl;
 
// 3. The value after multiplying by 3
calc.Multiply(3);
cout << calc.GetValue() << endl;
 
// 4. The value after subtracting num2
calc.Subtract(num2);
cout << calc.GetValue() << endl;
 
// 5. The value after dividing by 2
calc.Divide(2);
cout << calc.GetValue() << endl;
 
// 6. The value after calling the clear() method
calc.Clear();
cout << calc.GetValue() << endl;
 
   return 0;
}
 
Calculator.h
 
#ifndef CALCULATORH
#define CALCULATORH
 
class Calculator {
public:
  // TODO: Declare default constructor
 
  // TODO: Declare member functions - 
  //       Add(), Subtract(), Multiply(), Divide(), Clear(), GetValue()
 
private:
  // TODO: Declare private data member - value
};
 
#endif
 
Calculator.cpp
 
#include <iostream>
#include "Calculator.h"
using namespace std;
 
// TODO: Define default constructor
 
// TODO: Define member functions - 
//       Add(), Subtract(), Multiply(), Divide(), Clear(), GetValue()
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY