You are on page 1of 2

Object Oriented Programming with C++

Exercise 3: Classes

Exercise 1 10 marks

Your task is to create a class named as Calculator.

Part 1

 The Calculator class contains 2 private variables of type double – A and B. A and B

store the numbers on which we are required to perform the operation.

 The Calculator class contains 7 public member functions.

1. set(): This function is used to input 2 numbers. The numbers are stored in

private variables A and B.

2. add(): This function returns A + B.

3. sub(): This function returns A – B.

4. mul(): This function returns A * B.

5. div(): This function returns A / B. It also checks if B is 0 or not.

6. exp(): This function returns AB. It also checks if A is 0 or not.

7. Class destructor function

Part 2

 In this part, instead of taking input of 2 numbers. The parameters of functions

will be objects. Now there are five functions:

1. add(Calculator A, Calculator B)

2. sub(Calculator A, Calculator B)
3. mul(Calculator A, Calculator B)

4. div(Calculator A, Calculator B)

5. exp(Calculator A, Calculator B)

Bonus Points: Your Program should be able to do the following:

 3*2–5

 6/3+3

 5–3/2

Exercise 2 10 marks

Part 1

Your first task is to write a Time class for a 24-hour clock.


 The time is constructed from three field variables int hour, int minute and int
second.
 Write the corresponding constructor public Time(int hour, int minute, int second)
ensuring that it only allows valid inputs for a 24 hour clock. If the input is invalid, set
the value to 0.
 Implement getter methods public int getHour(), public int getMinute() and public
int getSecond()

Part 2
Write a method for the Time class public void increaseSecond() that increases the current
time by one second.

You might also like