One of the most important programming elements in C++ is called a class. In most object-oriented programming languages such as C++, a class is a user-defined type that includes both (1) a set of variables associated with it, as well as (2) a set of member functions. Note that the C structure or "struct" that we saw in our previous assignment only includes a set of variables but does not have any functions contained in it. However, the idea of including member functions make intuitive sense. For example, a "Time" structure should correspond to the way people record the time of day, and the operations we defined correspond to the sorts of things people do with recorded times. Similarly, a "Point" structure could correspond to the mathematical concept of a point, and operations such as calculating the distance between the two are important and should be closely associated with it.
In object oriented programming, programs are made up of a collection of classes and member function definitions, where the functions operate on specific kinds of objects of that class. Each class definition corresponds to some concept in the real world, and the functions that operate on an object of that class correspond to the ways real-world objects interact.
Member functions of a class differ from the other C++ functions in only two main ways:
// Define a structure that records the minutes, hours and seconds for
// a time period
struct Time {
int hour, minute;
double second;
};
|
// A function that takes as input a Time structure and outputs the time
// stored in it.
void printTime(Time mytime) {
cout << mytime.hour << ":" << mytime.minute << ":" << mytime.second << endl;
}
|
Time, we
would have to pass currentTime as an argument to the printTime
function:
Time currentTime = { 9, 14, 30.0 };
printTime( currentTime );
|
Time that includes
a member function called print() that does the exact same thing.
printTime
(that is currently independent of any classes) to Time::print.
The scope resolution operator, ::, separates the name of the
class from the name of the function; together they indicate that this is a
function named print() that is closely associated with the
Time class object.
print is a member of the class that is printing itself, so
we eliminate the parameter.
In doing this, we are going to invoke the function on an object rather than pass
an object as an argument to the function.
This function will no longer have a parameter named myTime but will refer
to the object as current object.
The kind of member variable access seen in the print() member function
is called implicit because the name of the class object does not appear in
the member function explicitly.
Features like this are one reason member functions are often more concise than
other functions.
const").
It is not obvious where we should put information about a parameter that does not
exist, but it is after the parameter list (which is empty in this case).
class Time {
int hour, minute;
double second;
void Time::print() const {
cout << hour << ":" << minute << ":" << second << endl;
}
};
|
class Time {
int hour, minute;
double second;
void Time::print() const;
};
// some extra code
// Function definition. Notice that the variables hour, minute and second are
// resolved using the Time:: resolution, so the compiler knows where they are
// defined
void Time::print() const {
cout << hour << ":" << minute << ":" << second << endl;
}
|
print() function, we will
have to invoke it on a Time object:
currentTime.print(); // This tells the object to "print itself" |
convertToSeconds(...) function that works on a
Time structure could look like this:
double convertToSeconds(Time myTime) {
int minutes = myTime.hour * 60 + myTime.minute;
double seconds = minutes * 60 + myTime.second;
return seconds;
}
|
double Time::convertToSeconds() const {
int minutes = hour * 60 + minute;
double seconds = minutes * 60 + second;
return seconds;
}
|
const indicates that none of the member variables are
modified.
Time testTime = {9, 14, 30.0}; // initial values of 9 hours, 14 minutes, 30.0 seconds
|
If Time is a structure, we can give use a makeTime(...)
function to take as input the number of seconds and create a Time
structure with the appropriate values:
Time makeTime(double secs) {
Time mytime;
mytime.hour = int(secs / 3600.0);
secs -= mytime.hour * 3600.0;
mytime.minute = int(secs / 60.0);
secs -= mytime.minute * 60.0;
mytime.second = secs;
return mytime; // Note this is what the function should return
}
|
Time::Time(double secs) {
hour = int(secs / 3600.0);
secs -= hour * 3600.0;
minute = int(secs / 60.0);
secs -= minute * 60.0;
second = secs;
}
|
To invoke the constructor, you use syntax that is a cross between a variable declaration and a function call:
Time bedtime(seconds); |
Time and calls the constructor, passing the value of seconds
as an argument.
The system allocates space for the new object and the constructor
initializes its instance variables.
Time structures using curly-braces:
Time bedTime = { 3, 35, 0.0 };
|
Time bedtime(seconds); |
Fortunately, it is legal to overload constructors in the same way as overloading functions. In other words, there can be more than one constructor with the same "name," as long as they take different parameters. Initializing a new object involves finding a constructor that takes the appropriate parameters. For example, it is common to have a constructor that takes one parameter for each instance variable and assigns them the appropriate values:
Time::Time(int h, int m, double s) {
hour = h;
minute = m;
second = s;
}
|
Time currentTime(9, 14, 30.0); |
Time class defined so far.
It contains three instance variables for the hour, minute and second values and
functions one can use to initialize, modify or display this information:
class Time {
int hour, minute;
double second;
Time(int h, int m, double s); // constructor
Time(double secs); // constructor
void print() const; // print out information
double convertToSeconds() const; // output the time in number of seconds
};
|
print()
exists for the Time object that takes no parameters and returns
no values, but the details of how this function works is hidden.
Encapsulation for the data (member variables) essentially hide what they are
from anything that is external to the object.
There are often two sections of a class definition, a part indicated using the keyword private that is restricted from outside access and the part indicated by the public keyword that is available to the outside world. Member functions are usually public, which means that they can be invoked by anything outside the function. The member variables are usually private, which means that they can be read and written only by class member functions. Totally hiding member variables without any way to modify them is too restrictive, so one often creates accessor functions that returns or sets the value of a variable. Of course, constructors must be public functions.
Note that the default designation for any parts of a class that is not explicity
set to be private nor public.
Thus, our Time class in its entirety could be declared as:
class Time {
private:
int hour, minute;
double second;
public:
Time(int h, int m, double s); // constructor
Time(double secs); // constructor
void print() const; // print out information
double convertToSeconds() const; // output the time in number of seconds
};
|
Your task is to explore and use a C++ class that manages dates.
| Note: | You are to learn to use a given class here, so do not try to write your own class or to circumvent using the given Date class. |
|---|
Again, USE the given Date class--this will not only make the assignment easier but it will give you experience using a class.
In later chapters, we will learn how to locate a C++ class outside of the file that contains the main program, so users would typically use the class without modifying it. Thus, use the given Date class WITHOUT ANY MODIFICATION TO THE CLASS or to its public functions. Note that you will need to copy the entire class as well as the public functions used by the class.
Of course, you are free to modify the existing or create your own
main() or any new function that is not part of the
Date class.
x is not a good name, but InputLetter is.
// Course: CSC 306 Introduction to Programming with C++
// Name: Your Name
// Assignment #13: <Put a brief sentence about your program here.>
/*
Purpose: <Put a more in-depth description of the program here.>
*/