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++
//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
Get Answers For Free
Most questions answered within 1 hours.