0% found this document useful (0 votes)
50 views

Class 3

The document describes 8 programming assignments involving object-oriented design in C++. Assignment 1 involves creating an Employee class and derived ProductionWorker class. Assignment 2 extends this to create a TeamLeader class. Assignment 3 creates a MilTime class to convert between military and standard time formats. Assignment 4 builds on this with a TimeClock class. Assignment 5 creates PersonData and derived CustomerData classes. Assignment 6 adds a PreferredCustomer class. Assignment 7 defines Ship and derived CruiseShip and CargoShip classes. Assignment 8 defines a pure abstract BasicShape class and derived Circle and Rectangle classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Class 3

The document describes 8 programming assignments involving object-oriented design in C++. Assignment 1 involves creating an Employee class and derived ProductionWorker class. Assignment 2 extends this to create a TeamLeader class. Assignment 3 creates a MilTime class to convert between military and standard time formats. Assignment 4 builds on this with a TimeClock class. Assignment 5 creates PersonData and derived CustomerData classes. Assignment 6 adds a PreferredCustomer class. Assignment 7 defines Ship and derived CruiseShip and CargoShip classes. Assignment 8 defines a pure abstract BasicShape class and derived Circle and Rectangle classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 3

1. Employee and ProductionWorker Classes

Design a class named Employee. The class should keep the following information in

- Employee name
- Employee number
- Hire date

Write one or more constructors and the appropriate accessor and mutator functions for the class.

Next, write a class named ProductionWorker that is derived from the Employee class. The
ProductionWorker class should have member variables to hold the following information:

- Shift (an integer)


- Hourly pay rate (a double )

The workday is divided into two shifts: day and night. The shift variable will hold an integer value
representing the shift that the employee works. The day shift is shift 1, and the night shift is shift 2. Write
one or more constructors and the appropriate accessor and mutator functions for the class. Demonstrate
the classes by writing a program that uses a ProductionWorker object.

2. TeamLeader Class

In a particular factory, a team leader is an hourly paid production worker who leads a small team. In
addition to hourly pay, team leaders earn a fixed monthly bonus. Team leaders are required to attend a
minimum number of hours of training per year. Design a TeamLeader class that extends the
ProductionWorker class you designed in Problem 1. The TeamLeader class should have member
variables for the monthly bonus amount, the required number of training hours, and the number of
training hours that the team leader has attended. Write one or more constructors and the appropriate
accessor and mutator functions for the class. Demonstrate the class by writing a program that uses a
TeamLeader object.

3. Time Format

Design a class called MilTime that is derived from the Time class below. The MilTime class should
convert time in military (24-hour) format to the standard time format used by the Time class. The class
should have the following member variables:

milHours Contains the hour in 24-hour format. For example, 1:00 pm would be stored as
1300 hours, and 4:30 pm would be stored as 1630 hours.

milSeconds Contains the seconds in standard format


The class should have the following member functions:

Constructor The constructor should accept arguments for the hour and seconds, in military
format. The time should then be converted to standard time and stored in the
hours, min, and sec variables of the Time class.

setTime Accepts arguments to be stored in the milHours and milSeconds variables. The
time should then be converted to standard time and stored in the hours, min, and
sec variables of the Time class

getHour Returns the hour in military format.

getStandHr Returns the hour in standard format

Demonstrate the class in a program that asks the user to enter the time in military format. The program
should then display the time in both military and standard format.

Input Validation: The MilTime class should not accept hours greater than 2359, or less than 0. It should
not accept seconds greater than 59 or less than 0.

Contents of Time.h
// Specification file for the Time class
#ifndef TIME_H
#define TIME_H
class Time
{
protected:
int hour;
int min;
int sec;
public:
// Default constructor
Time()
{ hour = 0; min = 0; sec = 0; }

// Constructor
Time(int h, int m, int s)
{ hour = h; min = m; sec = s; }

// Accessor functions
int getHour() const
{ return hour; }

int getMin() const


{ return min; }

int getSec() const


{ return sec; }
};
#endif

4. Time Clock

Design a class named TimeClock. The class should be derived from the MilTime class you designed in
Problem 3. The class should allow the programmer to pass two times to it: starting time and ending time.
The class should have a member function that returns the amount of time elapsed between the two times.
For example, if the starting time is 900 hours (9:00 am), and the ending time is 1300 hours (1:00 pm), the
elapsed time is 4 hours.

Input Validation: The class should not accept hours greater than 2359 or less than 0.

5. PersonData and CustomerData classes

Design a class named PersonData with the following member variables:

- lastName
- firstName
- address
- city
- state
- zip
- phone

Write the appropriate accessor and mutator functions for these member variables. Next, design a class
named CustomerData, which is derived from the PersonData class. The CustomerData class should
have the following member variables:

- customerNumber
- mailingList

The customerNumber variable will be used to hold a unique integer for each customer. The
mailingList variable should be a bool. It will be set to true if the customer wishes to be on a mailing
list, or false if the customer does not wish to be on a mailing list. Write appropriate accessor and mutator
functions for these member variables. Demonstrate an object of the CustomerData class in a simple
program.

6. PreferredCustomer Class

A retail store has a preferred customer plan where customers may earn discounts on all their purchases.
The amount of a customer’s discount is determined by the amount of the customer’s cumulative
purchases in the store.

- When a preferred customer spends $500, he or she gets a 5% discount on all future purchases.
- When a preferred customer spends $1,000, he or she gets a 6% discount on all future purchases.
- When a preferred customer spends $1,500, he or she gets a 7% discount on all future purchases.
- When a preferred customer spends $2,000 or more, he or she gets a 10% discount on all future
purchases.

Design a class named PreferredCustomer, which is derived from the CustomerData class you created
in Problem 5. The PreferredCustomer class should have the following member variables:

- purchasesAmount (a double )
- discountLevel (a double )

The purchasesAmount variable holds the total of a customer’s purchases to date. The discountLevel
variable should be set to the correct discount percentage, according to the store’s preferred customer
plan. Write appropriate member functions for this class and demonstrate it in a simple program.

Input Validation: Do not accept negative values for any sales figures.

7. Ship, CruiseShip, and CargoShip Classes

Design a Ship class that has the following members:

- A member variable for the name of the ship (a string)


- A member variable for the year that the ship was built (a string)
- A constructor and appropriate accessors and mutators
- A virtual print function that displays the ship’s name and the year it was built.
Design a CruiseShip class that is derived from the Ship class. The CruiseShip class should have the
following members:

- A member variable for the maximum number of passengers (an int)


- A constructor and appropriate accessors and mutators
- A print function that overrides the print function in the base class. The CruiseShip class’s
print function should display only the ship’s name and the maximum number of passengers.

Design a CargoShip class that is derived from the Ship class. The CargoShip class should have the
following members:

- A member variable for the cargo capacity in tonnage (an int).


- A constructor and appropriate accessors and mutators.
- A print function that overrides the print function in the base class. The CargoShip class’s print
function should display only the ship’s name and the ship’s cargo capacity.

Demonstrate the classes in a program that has an array of Ship pointers. The array elements should be
initialized with the addresses of dynamically allocated Ship, CruiseShip, and CargoShip objects. The
program should then step through the array, calling each object’s print function.

8. Pure Abstract Base Class Project

Define a pure abstract base class called BasicShape. The BasicShape class should have the following
members:

Private Member Variable:

area, a double used to hold the shape's area.

Public Member Functions:

getArea. This function should return the value in the member variable area.

calcArea. This function should be a pure virtual function.

Next, define a class named Circle. It should be derived from the BasicShape class. It should have the
following members:

Private Member Variables:

centerX, a long integer used to hold the x coordinate of the circle’s center.

centerY, a long integer used to hold the y coordinate of the circle’s center.

radius, a double used to hold the circle's radius.

Public Member Functions:

constructor—accepts values for centerX, centerY, and radius. Should call the overridden
calcArea function described below.

getCenterX—returns the value in centerX.


getCenterY—returns the value in centerY.

calcArea—calculates the area of the circle (area = 3.14159 * radius * radius) and stores the
result in the inherited member area.

Next, define a class named Rectangle. It should be derived from the BasicShape class. It should have
the following members:

Private Member Variables:

width, a long integer used to hold the width of the rectangle.

length, a long integer used to hold the length of the rectangle.

Public Member Functions:

constructor—accepts values for width and length. Should call the overridden calcArea function
described below.

getWidth—returns the value in width.

getLength—returns the value in length.

calcArea—calculates the area of the rectangle (area = length * width) and stores the result in
the inherited member area.

After you have created these classes, create a driver program that defines a Circle object and a
Rectangle object. Demonstrate that each object properly calculates and reports its area.

You might also like