C++ examples for Class:Class Creation
Use Date and Employee class to demonstrate composition.
class Date /*from w w w . ja v a2 s . c om*/ { public: static const int monthsPerYear = 12; // number of months in a year Date( int = 1, int = 1, int = 1900 ); // default constructor void print() const; // print date in month/day/year format ~Date(); // provided to confirm destruction order private: int month; // 1-12 (January-December) int day; // 1-31 based on month int year; // any year // utility function to check if day is proper for month and year int checkDay( int ) const; }; // end class Date #include <iostream> #include <stdexcept> using namespace std; Date::Date( int mn, int dy, int yr ) { if ( mn > 0 && mn <= monthsPerYear ) // validate the month month = mn; else throw invalid_argument( "month 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; } void Date::print() const { cout << month << '/' << day << '/' << year; } // output Date object to show when its destructor is called Date::~Date() { cout << "Date object destructor for date "; print(); cout << endl; } 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; throw invalid_argument( "Invalid day for current month and year" ); } // end function checkDay #include <string> using namespace std; class Employee { public: Employee( const string &, const string &, const Date &, const Date & ) ; void print() const; ~Employee(); // provided to confirm destruction order private: string firstName; // composition: member object string lastName; // composition: member object const Date birthDate; // composition: member object const Date hireDate; // composition: member object }; // end class Employee #include <iostream> using namespace std; 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 { cout << "Employee object constructor: " << firstName << ' ' << lastName << endl; } void Employee::print() const { cout << lastName << ", " << firstName << " Hired: "; hireDate.print(); cout << " Birthday: "; birthDate.print(); cout << endl; } Employee::~Employee() { cout << "Employee object destructor: " << lastName << ", " << firstName << endl; } #include <iostream> using namespace std; int main() { Date birth( 7, 24, 1949 ); Date hire( 3, 12, 1988 ); Employee manager( "Bob", "Blue", birth, hire ); cout << endl; manager.print(); }