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

  What is a brute-force attack

What are the current OWASP Top 10 - What is a brute-force attack and how can the risks of these attacks be mitigated and What could be the impact

  Explain the concept of workpiece squaring

Explain the concept of workpiece squaring. Why is squaring important? What is machine zero? Why is it important? What is a burr? Why does it occur?

  What current certifications that companies are looking for

Scan your local newspaper's classified job listings. What are the current certifications that companies are looking for with respect to network administrators?

  What is the total power consumption of the system

What is the total power consumption of the system. in kilowatts? How many domestic irons, operating together. will dissipate this much power?

  Develop a web page that lists at least five useful resources

Develop a Web page that lists at least five useful resources along with a brief description of each. Organize your Web page with a table.

  Explain how you ensured the antivirus program is updated

Explain how you ensured the antivirus program is updated and running a full scan of the system; describe anything significant you observed?

  Design program to keep track of a hardware store''s inventory

Write down a program to keep track of a hardware store's inventory. The sore sell various items. For each item in the store the following information is kept.

  Securing the personal data

Because of the information systems are capable of storing and compiling the large amount of the data, several people worry that, and their private data will end up in the wrong hands.

  Identifying and explaining the key components of a

identifying and describing the key components of a comprehensive disaster recovery plan for any incident that could

  Some programming languages a comment can be enclosed

In some programming languages a comment can be enclosed either in braces {} or in the symbols (* *). How do you think a scanner will group the four symbols {, }, (*, *) for purposes of classification i.e. would each symbol be given its own classif..

  Make a visual rendering depicting your design via the use

as the lead software engineer for a medium-sized hospital you have been asked to spearhead an effort to improve the

  Write a two-page summary of your findings

Write a two-page summary of your findings. Guidelines for your interview and sample questions are available on the companion Web site.

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