Lab Objectives
This lab was designed to inforce the following programming concepts:
• Using classes to create a data type SimpleCalculator capable of performing arithmetic operations.
• Creating const member functions to enforce the principle of least privilege.
The follow-up questions and activities also will give you practice:
• Using constructors to specify initial values for data members of a programmer-defined class.
Description of the Problem
Write a SimpleCalculator class that has public methods for adding, subtracting, multiplying and dividing two doubles. A sample call is as follows:
double answer = sc.add( a, b );
Object sc is of type SimpleCalculator. Member function add returns the result of adding its two arguments. All of SimpleCalculator’s member functions should have return type double. SimpleCalculator does not have a constructor because it does not have any data members. The purpose of a constructor is to initialize all the data members of a class, therefore, SimpleCalculator does not need a constructor.
Sample Output
The value of a is: 10
The value of b is: 20
Adding a and b yields 30
Subtracting b from a yields -10
Multiplying a by b yields 200
Dividing a by b yields 0.5
2- Modify your class so that SimpleCalculator has a private data member called answer. After performing an operation, assign the result to answer. Add a member function named getAnswer to retrieve the result of the last arithmetic operation performed by the object. Also, add a constructor for class SimpleCalculator that initializes the value of answer to 0.
Sample Output
The value of a is: 10
The value of b is: 20
Adding a and b yields 30
Subtracting b from a yields -10
Multiplying a by b yields 200
Dividing a by b yields 0.5
3- Modify the program so that the SimpleCalculator class has an input member function that allows the userto input two doubles. The function should then store the values that were input in private data members. Use these two values for each of the arithmetic calculations. Create two constructors for this class, one that takes no arguments and initializes a and b to 0 and another that takes two doubles and initializes a and b to those values. Finally, create a member function printValues that displays the values of a and b. A segment of the driver program might now look like this:
SimpleCalculator sc; // instantiate object
sc.input();
sc.printValues();
cout << "Adding a and b yields " << sc.add() << "\n";
Sample Output
Enter the value of a: 15
Enter the value of b: 30
The value of a is: 15
The value of b is: 30
Adding a and b yields 45
#include <iostream>
using namespace std;
class SimpleCalculator{
public:
double add(double a,double b){return a+b;}
double substract(double a,double b){return a-b;}
double multiply(double a,double b){return a*b;}
double divide(double a,double b){return a/b;}
};
int main(){
double a,b,answer;
cout<<"The value of a is ";
cin>>a;
cout<<"\n The value of b is";
cin>>b;
SimpleCalculator sc;
answer=sc.add(a,b);
cout<<"\n Adding of a and b yeilds "<<answer;
cout<<"\n Substracting of b from a yeilds
"<<sc.substract(a,b);
cout<<"\n Multiplying of a by b yeilds
"<<sc.multiply(a,b);
cout<<"\n Dividing a by b yeilds
"<<sc.divide(a,b)<<"\n";
return 0;
}
/*-------2nd---------*/
#include<iostream>
using namespace std;
class SimpleCalculator{
private :
double answer;//private data member
public:
SimpleCalculator(double ans=0){answer=ans;}//constructor
void add(double a,double b){ //public func with double as return
type
answer= a+b;
}
void subtract(double a,double b){answer= a-b;}
void multiply(double a,double b){answer= a*b;}
void divide(double a,double b){answer= a/b;}
double getAnswer(){return answer;}
};
int main(){
double a,b;
SimpleCalculator sc(0);
cout<<"The value of a is ";
cin>>a;
cout<<"The value of b is ";
cin>>b;
sc.add(a,b);
cout<<"\nAdding of a and b yeilds
"<<sc.getAnswer();
sc.subtract(a,b);
cout<<"\nSubtracting b from a yeilds
"<<sc.getAnswer();
sc.multiply(a,b);
cout<<"\nMultiplying a by b yeilds
"<<sc.getAnswer();
sc.divide(a,b);
cout<<"\nDividing a by b yeilds
"<<sc.getAnswer()<<"\n";
}
/*----3rd------*/
#include<iostream>
using namespace std;
class SimpleCalculator{
private :
double a,b;
public:
SimpleCalculator(void){a=0,b=0;}//0 value constructor
void input(){
cout<<"The value of a is ";
cin>>a;
cout<<"The value of b is ";
cin>>b;
}
SimpleCalculator(double ,double
){a=this->a,b=this->b;}//initializing with the values
void printValues(){
cout<<"The value of a and b are: "<<a<<"and "
<<b<<"\n";
}
double add(){//double add return double datatype value from the add
method
return a+b;}
double subtract(){return a-b;}
double multiply(){return a*b;}
double divide(){return a/b;}
};
int main(){
double a,b;
SimpleCalculator sc;
sc.input();
sc.printValues();
cout<<"Adding of a and b yeilds
"<<sc.add()<<"\n";
cout<<"Subtracting of b from a yeilds
"<<sc.subtract()<<"\n";
cout<<"Multiplying of a by b yeilds
"<<sc.multiply()<<"\n";
cout<<"Dividing of a by b yeilds
"<<sc.divide()<<"\n";
}
Get Answers For Free
Most questions answered within 1 hours.