# Lab: Classes

## Learning Objectives

- Learn how to implement classes (user defined data types).
- Understand the concept of encapsulation (hiding internal details to make usage of a class more obvious).
- Understand how to implement and use accessor and mutator functions.
- Understand how to implement constructors.
- Understand how to overload member functions.
- Learn how to break the source code for a program into separate files (compilation units).
- Learn how to solve programming problems with classes.

## Exercise 1: A number class (20 points)

Develop a class called `Number`

that represents an integer value.
The class should have a constructor
that takes the integer value as its only argument.
The constructor should store this value in a private member variable.

Define a public member function
called `isPrime`

that takes no argument and returns a bool.
If the number is a prime number,
then `isPrime`

returns true, otherwise it returns false.

Define another member function called `isDivisibleBy`

that takes a single integer argument and returns a bool.
If the integer passed into `isDivisibleBy`

is a divisor for the number,
then the function returns true, otherwise it returns false.

Test your implementation of the Number class using the following test code.

#include <iostream> #include <cassert> #include "Number.h" using namespace std; int main(int argc, char * argv[]) { Number nine(9); assert(!nine.isPrime()); assert( nine.isDivisibleBy(1)); assert(!nine.isDivisibleBy(2)); assert( nine.isDivisibleBy(3)); assert(!nine.isDivisibleBy(8)); assert( nine.isDivisibleBy(9)); Number seven(7); assert( seven.isPrime()); assert( seven.isDivisibleBy(1)); assert(!seven.isDivisibleBy(2)); assert(!seven.isDivisibleBy(3)); assert( seven.isDivisibleBy(7)); Number seventySeven(77); assert(!seventySeven.isPrime()); Number seventyThree(73); assert(seventyThree.isPrime()); Number twentySeven(27); assert(!twentySeven.isPrime()); assert(!twentySeven.isDivisibleBy(2)); assert(twentySeven.isDivisibleBy(3)); assert(twentySeven.isDivisibleBy(9)); cout << "All tests passed.\n"; cin.get(); // Use if debugging program in Visual Studio. }

Your solution to this exercise should contain the following three files.

- ex1.cpp
- contains the test code given above
- Number.h
- contains the
`Number`

class declaration - Number.cpp
- contains the implementation of the constructor and
`isPrime`

Include a UML class diagram within a multi-line comment in *Number.h*.

## Exercise 2: Listing prime numbers with the number class (20 points)

Write a program that prompts the user to enter a number larger than 2. The program should use the Number class you developed in exercise 1 to determine the prime numbers between 2 and the number entered, and display them in the console window.

This is a problem you did in a previous lab. However, this time you will make use of your Number class in the solution.

## Exercise 3: An Egg Carton Class (20 points)

Provide an implementation of the egg carton class described bellow. Develop test code that tests your implementation.

+----------------------------------------------------------------+ | | | EggCarton | | | +----------------------------------------------------------------+ | | | - brownEggs : int | | - whiteEggs : int | | | +----------------------------------------------------------------+ | | | + EggCarton(brownEggs : int, whiteEggs : int) | | + addBrownEggs(n : int) : bool | | + addWhiteEggs(n : int) : bool | | + getTotalEggs() : int | | | +----------------------------------------------------------------+

Figure 1

The UML class diagram for the EggCarton class is shown in Figure 1. Instances of this class represent egg cartons that can hold up to 12 eggs. The class provides functions to add white and brown eggs. All of the functions that modify the number of eggs in the carton return false if the operation can not be done exactly as requested. For example, if there are 3 white eggs and 0 brown eggs, then addBrownEggs(10) returns false and the number of white eggs remains at 3 and brown eggs at 0. There is a member function called getTotalEggs, which returns the total number of white and brown eggs together. For example, if the carton has 1 brown and 2 white eggs, then getTotalEggs returns 3.