Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

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 SimpleCalculators 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() << " ";

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions