Question

Lab Objectives This lab was designed to inforce the following programming concepts: • Using classes to...

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

Homework Answers

Answer #1


#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";
}

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
c++ C++ CLASSES and objects DO ADD COMMENTS DISPLAY OUTPUT First make three files: episode.cpp, episode.h...
c++ C++ CLASSES and objects DO ADD COMMENTS DISPLAY OUTPUT First make three files: episode.cpp, episode.h andRun.cpp to separate class header and implementation. In this program, we are going to create a small scale Telivision Management System. A) Create a class Episode with following variables: char* episode_name, char* episode_venue, char episode_date[22] and char episode_time[18]. Input format for episode_date: dd-mm-yyyy Input format for episode_time: hh:mm am/pm B) Implement default constructor and overloaded constructor. Print “Default Constructor Called” and “Overloaded Constructor Called”...
in C++ Define a class bank account with proper constructors, accessors, mutators, and other member functions....
in C++ Define a class bank account with proper constructors, accessors, mutators, and other member functions. The class will be store money expressed in dollars and cents. The class should have following data members (fname, lname, accountNum, balance, dollars, cents).Write A function to print out the object bank account info.The class should implement two member functions for deposit and withdrawal. Overload the + operator, so that adding two objects of this class works. please respond quickly and clearly, thank you.
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields and appropriate types and permissions diceType numSides sideUp The class should have A 0 argument (default) constructor default values are diceType: d6, numSides: 6, sideUp: randomValue 1 argument constructor for the number of sides default values are diceType: d{numSides}, sideUp: randomValue 2 argument constructor for the number of sides and the diceType appropriate accessors and mutators *theoretical question: can you change the number of...
Program: 6: Function overloading AIM: To write a C++ program to illustrate the concept of function...
Program: 6: Function overloading AIM: To write a C++ program to illustrate the concept of function overloading. PSEUDOCODE: Declare necessary variables. Create a class with add(int,int) ,add(float,float) as member functions and necessary variable. add(int,int) is used to add two integer values. add(float,float) is used to add two float values. Using object call the required function with corresponding input. Display the output.
Create a class called time that has three data members hours, minutes, and seconds. The class...
Create a class called time that has three data members hours, minutes, and seconds. The class has the following member functions: - Overloaded Constructors - Display function to display it, in 11:59:59 format. - SetTime() - IsValid(): Check if the time is valid or not - AddTime(Time t1, Time t2): Add two time values passed as a parameter leaving the result in third time variable. - Increment(): Tells what will be time after a second - Decrement(): Tells what was...
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
For this question you will write two (2) constructors in the implementation file: CylinderType.cpp (Assume that...
For this question you will write two (2) constructors in the implementation file: CylinderType.cpp (Assume that the base class CircleType has a default constructor & assigns 1.0 to its private double radius) part (a) // The first constructor is a default constructor. Please use height as the CylinderType's private member name in the body. // It will create a CylinderType object with a radius of 1.0 and height of 1.0 part (b)    // The second constructor should have two...
4. Programming Problem: In this problem we will keep the Mechanism interface and the two classes...
4. Programming Problem: In this problem we will keep the Mechanism interface and the two classes Car and Computer from the previous problem. In addition, we will add a new class called Mechanic. We will also create a new TestMechanic class that will contain the main method. Create a class called Mechanic with following instance variable and constants: private int cost; public static int TEST_PRICE = 10; public static int REPAIR_PRICE = 50; Have a default constructor that will set...
Design a class with the following requirements: 1- give the class a name from your choice....
Design a class with the following requirements: 1- give the class a name from your choice. 2- write some code that creates three instance variables, choose a name and type for each one of them. (must have two private and one public variables) 3- Initialize the variables using two different constructors. 4- Use mutator and accessor methods (set and get) for the private data members. 5- Display the value of each member variable using a method. 6- In the main,...
Design a class with the following requirements: 1- give the class a name from your choice....
Design a class with the following requirements: 1- give the class a name from your choice. 2- write some code that creates three instance variables, choose a name and type for each one of them. (must have two private and one public variables) 3- Initialize the variables using two different constructors. 4- Use mutator and accessor methods (set and get) for the private data members. 5- Display the value of each member variable using a method. 6- In the main,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT