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”...
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.
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....
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,...
Java Programming In this lab students continue to write programs using multiple classes. By the end...
Java Programming In this lab students continue to write programs using multiple classes. By the end of this lab, students should be able to Write classes that use arrays and ArrayLists of objects as instance variables Write methods that process arrays and ArrayLists of objects Write getter and setter methods for instance variables Write methods using object parameters and primitive types Question- class StringSet. A StringSet object is given a series of up to 10 String objects. It stores these...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). Details and Requirements Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A...
C++ PROGRAMMING Assignment: For this assignment, you will construct a program which is capable of taking...
C++ PROGRAMMING Assignment: For this assignment, you will construct a program which is capable of taking a user-given number, and adding up all of the numbers between 1 and the given number. So if someone inputs 12, it should add 1 + 2 + 3 + 4 + … 9 + 10 + 11 + 12, and return the answer. However, you’ll be using two different methods to do this. The first method should utilize either a for loop or...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT