Name:

Sec: 07/03/2008

Programming Assignment #5

Defining and Using C++ Classes


Recall that the two structured data types we have studied so far are arrays, which have elements all of the same type, and structures, which have elements of differing types. Another structured data type is a class, which is specifically designed to combine data and functions in a single unit. A class is a collection of a fixed number of components. The components of a class are called the members of the class. If a member of a class is a variable, you define it just like any other variable, but you cannot initialize it at definition. If a member of a class is a function, you typically use the function prototype to define that member. Member functions can directly access any data member of the class without passing that data member as an argument.


No memory is allocated in a class definition. Instead, memory is allocated when the class is instantiated (an object is created). Additionally, the semicolon (;) is part of the syntax at the end of the class definition. The members of a class are classified into three categories: private, public, and protected. By default, all members of a class are private. Private members cannot be accessed outside of the class. A public member is accessible outside the class.

Objectives


In this assignment you will learn how to define a class and declare and use instances of the class.


After completing this assignment, you will be able to:


Instructions


Part 1: Download, compile and run the DayType stub


In order to make getting started with this assignment a little easier, I have provided 3 files for you that take care of some of the details you will need when creating a class. The files are called DayType.h, DayType.cpp and DayTypeTest.cpp. You should download these files, create a project in the IDE of your choice, and then add and make sure you can compile and run the stub program. The DayType class I provide doesn't do anything yet, you will have to fill in the details for your part of the assignment.


Part 2: Implement the DayType class


For your assignment, you should implement the following functionality for the DayType class. The class DayType should store the day, such as Sun for Sunday, Mon for Monday, etc. (use a string or maybe even better an enumerated type see pg. 420). The program should be able to perform the following operations on an object of type DayType (these will all be member functions you need to implement):


  1. Set the day (setter method)

  2. Print the day

  3. Return the day (getter method)

  4. Return the next day

  5. Return the previous day


Your should also implement the appropriate constructors for you DayType class (at a minimum a default constructor and a constructor accepting a single parameter indicating the initial day should be implemented).

Assignment 5 Finished


You have now completed Assignment 5. If your program compiles and runs correctly and you have successfully uploaded your source file to the eCollege online submission site, then you are done.