Write a simplecalculator class that has public methods

Assignment Help Computer Engineering
Reference no: EM131984772

Lab Assignment

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

1- 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.

Reference no: EM131984772

Questions Cloud

Effectiveness of human resources planning in organizations : Project - XXSE60108 - Effectiveness of Human resources planning in organizations - concepts relating to the topic with appropriate in-text citations
The family should spend each month on mortgage payment : What is the maximum amount the family should spend each month on a mortgage payment?
Treaty of versailles : What further details about the President Woodrow Wilson introduced the "Treaty of Versailles" that brought the end of First World War.
Why do the people gather in the woods : 1. Why do the people gather in the woods? Why do they attend the ceremony?
Write a simplecalculator class that has public methods : Write a SimpleCalculator class that has public methods for adding, subtracting, multiplying and dividing two doubles.
Research tools available through discount broker accounts : Research tools available through discount broker accounts. Given the Pros and Cons of using the internet as an investment tool?
When evaluating analogical arguments : When evaluating analogical arguments, you need to consider 3 things. What are those things, and how do they need to be considered?
Moderate growth company paid dividend last year : Moderate growth company paid a dividend last year of $3.00. the expected ROE for next year is 13%.
What is open entry and open exit : What is open entry and open exit English as s Second Language programs? and what are the advantages and disadvantages of enrolling in these programs?

Reviews

Write a Review

Computer Engineering Questions & Answers

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd