Composition Code Example
Composition Code Example
class Date
{
public:
static const int monthsPerYear = 12; // number of months in a year
Date( int mn, int dy, int yr );
void print() const; // print date in month/day/year format
~Date();
private:
int month; // 1-12 (January-December)
int day; // 1-31 based on month
int year; // any year
int checkDay( int ) const;
}; // end class Date
class Employee
{
public:
Employee( const string &, const string &, const Date &, const Date & );
void print() const;
~Employee(); // provided to confirm destruction order
private:
string firstName;
string lastName;
const Date birthDate; // composition: member object
const Date hireDate; // composition: member object
}; // end class Employee
return 0;
}
else
cout<<"Invalid day for current month and year";
} // end function checkDay
{
// output Employee object to show when constructor is called
cout << "Employee object constructor: "
<< firstName << ' ' << lastName << endl;
} // end Employee constructor