P12_ex Example DateTime Class Inheritance |
|
Description:
The purpose of this C++ programming example is to familiarize students with
class inheritance and the testing of the inheritance with a driver program.
Sample Output:
P12ex Juan Marquez
Enter Month: 09
Enter Day: 13
Enter Year: 1982
Enter Hour: 9
Enter Minute: 13
Enter Second: 22
9/13/1982 9:13:22
10/31/1983 10:31:23
11/15/1984 11:15:24
DateTime object going out of scope. month = 12
DateMDY object going out of scope. month = 12
12/20/1985 12:20:25
DateTime object going out of scope. month = 12
DateMDY object going out of scope. month = 12
DateTime object going out of scope. month = 10
DateMDY object going out of scope. month = 10
Press any key to continue
Source Code:
//P12ex Inheritance DateTime Class - Juan Marquez
//
//The new class DateTime is created by inheriting the DateMDY class
//and then adding the hour, minute, and second properties. The
//required operations to support the time properties are also added.
//
#include <iostream> // cin and cout
using namespace std;
class DateMDY
{
private:
protected:
//protected allows for inheritance
//member variables require accessors
int month;
int day;
int year;
public:
//default constructor - call input functions
DateMDY();
//overloaded constructor
DateMDY(int m, int d, int y);
//destructor
~DateMDY();
//accessors
void setMonth(int m);
void setDay(int d);
void setYear(int y);
int getMonth();
int getDay();
int getYear();
void inputMonth();
void inputDay();
void inputYear();
};
class DateTime : public DateMDY //inheritance
{
private:
int hour;
int minute;
int second;
public:
//default constructor - call input functions and base constructor
DateTime( );
//overload constructor - need to also take in values for base class
DateTime(int m, int d, int y, int h, int n, int s);
//destructor
~DateTime();
//accessors
void setHour(int h);
void setMinute(int n);
void setSecond(int s);
int getHour();
int getMinute();
int getSecond();
void inputHour();
void inputMinute();
void inputSecond();
};
//The driver to test class DateTime begins here.
void displayDate(DateTime& date); //call-by-reference
void main()
{
cout << "\nP12ex Juan Marquez \n\n";
//Input 09/13/1982 9:13:22
DateTime bday; //using default constructor
displayDate(bday);
bday.setMonth(10); //use accessors
bday.setDay(31);
bday.setYear(1983);
bday.setHour(10);
bday.setMinute(31);
bday.setSecond(23);
displayDate(bday);
DateTime payDay(11, 15, 1984, 11, 15, 24); //overloaded constructor
displayDate(payDay);
payDay = DateTime(12, 20, 1985, 12, 20, 25); //explicit call to overloaded
//calls destructor right after returning from constructor and before
//assignment statement(=), that is why 12 is displayed on sample output
displayDate(payDay);
return;
}
void displayDate(DateTime& date)
{
//declare local variables
int month;
int day;
int year;
int hour;
int minute;
int second;
//get values stored in private variables
month = date.getMonth();
day = date.getDay();
year = date.getYear();
hour = date.getHour();
minute = date.getMinute();
second = date.getSecond();
cout << month << "/" << day << "/" << year << " "
<< hour << ":" << minute << ":" << second
<< endl << endl;
}
//Class functions
//default constructor
DateMDY::DateMDY()
{
inputMonth();
inputDay();
inputYear();
}
//overloaded constructor
DateMDY::DateMDY(int m, int d, int y)
{
month = m;
day = d;
year = y;
}
//destructor
DateMDY::~DateMDY()
{
cout << "DateMDY object going out of scope. month = "
<< month << endl;
}
//accessor definitions
void DateMDY::setMonth(int m)
{
month = m;
}
void DateMDY::setDay(int d)
{
day = d;
}
void DateMDY::setYear(int y)
{
year= y;
}
int DateMDY::getMonth()
{
return month;
}
int DateMDY::getDay()
{
return day;
}
int DateMDY::getYear()
{
return year;
}
void DateMDY::inputMonth()
{
cout << "Enter Month: ";
cin >> month;
}
void DateMDY::inputDay()
{
cout << "Enter Day: ";
cin >> day;
}
void DateMDY::inputYear()
{
cout << "Enter Year: ";
cin >> year;
}
//default constructor
DateTime::DateTime()
: DateMDY() //call constructor in base class which calls input functions
{
inputHour();
inputMinute();
inputSecond();
}
//overload constructor
DateTime::DateTime(int m, int d, int y, int h, int n, int s)
: DateMDY (m, d, y) //call constructor in base class and pass its values
{
hour = h;
minute = n;
second = s;
}
//destructor
DateTime::~DateTime()
{
cout << "\nDateTime object going out of scope. month = "
<< month << endl;
}
//accessors
void DateTime::setHour(int h)
{
hour = h;
}
void DateTime::setMinute(int n)
{
minute = n;
}
void DateTime::setSecond(int s)
{
second = s;
}
int DateTime::getHour()
{
return hour;
}
int DateTime::getMinute()
{
return minute;
}
int DateTime::getSecond()
{
return second;
}
void DateTime::inputHour()
{
cout << "Enter Hour: ";
cin >> hour;
}
void DateTime::inputMinute()
{
cout << "Enter Minute: ";
cin >> minute;
}
void DateTime::inputSecond()
{
cout << "Enter Second: ";
cin >> second;
cout << endl;
}
//end of P12ex.cpp