0% found this document useful (0 votes)
57 views

Composition Code Example

This document defines two C++ classes - Date and Employee. The Date class represents a calendar date with methods to initialize, print, and destruct a date object. The Employee class uses the Date class to store an employee's birthdate and hire date, and has methods to initialize, print, and destruct an Employee object. The main function creates a Date for an employee's birth and hire dates, uses them to initialize an Employee object, and prints the employee details.

Uploaded by

REBEL USMAN
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Composition Code Example

This document defines two C++ classes - Date and Employee. The Date class represents a calendar date with methods to initialize, print, and destruct a date object. The Employee class uses the Date class to store an employee's birthdate and hire date, and has methods to initialize, print, and destruct an Employee object. The main function creates a Date for an employee's birth and hire dates, uses them to initialize an Employee object, and prints the employee details.

Uploaded by

REBEL USMAN
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

using namespace std;


/* run this program using the console pauser or add your own getch, system("pause")
or input loop */

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

int main(int argc, char** argv) {

Date birth( 7, 24, 1990 );


Date hire( 3, 12, 1988 );
Employee manager( "Nadeem", "Waseem", birth, hire );
cout << endl;
manager.print();

return 0;
}

Date::Date( int mn, int dy, int yr )


{
if ( mn > 0 && mn <= monthsPerYear ) // validate the month
month = mn;
else
cout<< " Please enter a valid month that must be 1-12";

year = yr; // could validate yr


day = checkDay( dy ); // validate the day

// output Date object to show when its constructor is called


cout << "Date object constructor for date ";
print();
cout << endl;
} // end Date constructor
// print Date object in form month/day/year
void Date::print() const
{
cout << month << '/' << day << '/' << year;
} // end function print

// output Date object to show when its destructor is called


Date::~Date()
{
cout << "Date object destructor for date ";
print();
cout << endl;
} // end ~Date destructor

// utility function to confirm proper day value based on


// month and year; handles leap years, too
int Date::checkDay( int testDay ) const
{
static const int daysPerMonth[ monthsPerYear + 1 ] =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

// determine whether testDay is valid for specified month


if ( testDay > 0 && testDay <= daysPerMonth[ month])
return testDay;

// February 29 check for leap year


if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||
( year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;

else
cout<<"Invalid day for current month and year";
} // end function checkDay

// Definition of employee class


// constructor uses member initializer list to pass initializer
// values to constructors of member objects
Employee::Employee( const string &first, const string &last, const Date
&dateOfBirth, const Date &dateOfHire )
: firstName( first ), // initialize firstName
lastName( last ), // initialize lastName
birthDate( dateOfBirth ), // initialize birthDate
hireDate( dateOfHire ) // initialize hireDate

{
// output Employee object to show when constructor is called
cout << "Employee object constructor: "
<< firstName << ' ' << lastName << endl;
} // end Employee constructor

// print Employee object


void Employee::print() const
{
cout << lastName << ", " << firstName << " Hired: ";
hireDate.print();
cout << " Birthday: ";
birthDate.print();
cout << endl;
} // end function print

// output Employee object to show when its destructor is called


Employee::~Employee()
{
cout << "Employee object destructor: "
<< lastName << ", " << firstName << endl;
} // end ~Employee destructor

You might also like