This lab is about Classes and Objects in C++.
Exercise 1 of 6) Define a class named Money that stores a monetary amount. The class should have two private integer variables, one to store the number of dollars and another to store the number of cents. Add accessor and mutator functions to read and set both member variables. Add another function that returns the monetary amount as a double. Write a program that tests all of your functions with at least two different Money objects.
Exercise 2 of 6) Do the Exercise 1, the definition of a Money class, except create a default constructor that sets the monetary amount to 0 dollars and 0 cents, and create a second constructor with input parameters for the amount of the dollars and cents variables. Modify your test code to invoke the constructors.
Some Key Points:
Function Inline:
Placing the keyword inline before the declaration of a function is a hint to the compiler to put the body of the function in the code stream at the place of the call (with arguments appropriately substituted for the parameters). The compiler may or may not do this, and some compilers have strong restrictions on what function bodies will be placed inline. In order to generate an executable of reasonable size, it is, therefore in practice, used for short functions in terms of the number of instructions in functions.
The inline keyword has the advantage of speeding up a program if it regularly calls the inline function. It makes it possible to substantially condense the code, in particular for the accessors of a class. A class accessor is typically a member function which gets the values of class member.
The inline allows to declare and implement functions directly in a header (.h) Function const:
In C ++, const at the end of the declaration of a function means that the function is not supposed to change the values of the class member variables.
Constructor Definitions:
A constructor is a member function having the same name as the class name. The purpose of a class constructor is automatic allocation and initialization of resources involved in the definition of class objects. Constructors are called automatically at the definition of objects. Special declarator syntax for constructors uses the class name followed by a parameter list but no return type.
Constructor initialization section:
The implementation of the constructor can have an initialization section:
classname:: classname (list of arguments): a(0), b(1)
{ //empty or implementation }
In the literature, this feature is sometimes called a member initializer list. The purpose of a member initializer list is to initialize the class data members. Only constructors may have member initializer lists.
Exercise 3 of 6) Define a class called StartTime with the following attributes and methods
char d_hour; char d_minutes;
inline void get(char& _hour, char& _minutes) const; inline void set(char _hour, char _minutes);
inline StartTime(char _hour, char _minutes);
inline void print() const;
This class will be placed in the starttime.h file.
Header files are a good place to define classes.
Exercise 4 of 6) Define the class CourseActivity containing a pointer to a StartTime, a duration in minutes, and a location. These attributes must be private. A public method for changing the start time, a display method, and a full constructor are also required. The signatures of these methods and the type of the attributes are:
StartTime* d_start; double duration; std::string location; CourseActivity( const StartTime* _start, double _duration, const std::string _location ); void reschedule( StartTime* _newStart ); void print() const; |
This class will be placed in the activity.h file.
Define three public subclasses of CourseActivity (in the same cpp file activity.h). These classes are: Lecture, Laboratory and Tutorial. Each of these classes should define a method void print(); and must include a std::string to contain a topic, an assignment, and an exercise. The class definition must be in the activity.h file, while the methods will be defined in activity.cpp
Exercise 5 of 6) Add the following constructors to the classes defined in exercise 4.
Lecture( const StartTime* _start, double _duration, const string location, const string& _topic ); Lecture( const CourseActivity& _oActivity, const string& topic ); Laboratory( const StartTime& _start, double _duration, const string location, const string& _assignment ); Tutorial( const StartTime& _start, double _duration, const string location, const string& _exercise ); |
Exercise 6 of 6) Define a main() function in lab5.cpp. Create three instances of
StartTime for 11:30, 13:00 and 16:00. With these, create a Lecture, a
Laboratory and a Tutorial with a duration of 90 minutes. Then display each of these activities.
Construct two additional Lecture instances from Tutorial and Laboratory with
the constructor Lecture(const CourseActivity & _oActivity, const std::string & topic); Display these five activities. Then change the time of two conflicting lectures for 19:00 and 7:00, respectively. Display the activities again.
Reviews
There are no reviews yet.