// datatypes10.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program shows how "enum" type can be used as a part of a class. // Key Points: 1) "enum" data type. // 2) You must insure being in the correct range; otherwise // logic errors occur. // Note: The "char*" type used in the program can be read as the String type // described in your text book. #include "datatypes10.h" Year::Year( Months mn = Nov ) { mon = mn; switch (mon) { case April: case May: case June: seas = Spring; color = lightgreen; break; case July: case Aug: case Sept: seas = Summer; color = green; break; case Oct: case Nov: case Dec: seas = Fall; color = red; break; case Jan: case Feb: case Mar: seas = Winter; color = white; break; } } void Year::BadShowTreeColor ( void ) { cout << ". The trees color at this season is: " << color << endl; } void Year::ShowTreeColor ( void ) { if ( color == lightgreen ) cout << ". The trees color at this season is: " << "lightgreen" << endl; else if ( color == green ) cout << ". The trees color at this season is: " << "green" << endl; else if ( color == white) cout << ". The trees color at this season is: " << "white" << endl; else if ( color == red) cout << ". The trees color at this season is: " << "red" << endl; } char* Year::FindSeason ( void ) // char* can be thought as the String type { // used in your text book. It is a type to if ( seas == Winter ) // hold a string. return "Winter"; else if ( seas == Spring ) return "Spring"; else if ( seas == Summer ) return "Summer"; else return "Fall"; } Year::Seasons Year::BadFindSeason ( void ) { return seas; } void Year::SetMonth ( Months mn ) { mon = mn; if ((mon == April) || (mon == May) || (mon == June) ) { seas = Spring; color = lightgreen; } else if ((mon == July) || (mon == Aug) || (mon == Sept) ) { seas = Summer; color = green; } else if ((mon == Oct) || (mon == Nov) || (mon == Dec) ) { seas = Fall; color = red; } else if ((mon == Jan) || (mon == Feb) || (mon == Mar) ) { seas = Winter; color = white; } } int main () { Year yr1, yr2(Year::Feb), yr3(Year::Oct), yr4(Year::July); cout << "\n The information of the object yr1 is as follows: " << endl; cout << " The object season is " << yr1.FindSeason(); yr1.BadShowTreeColor(); cout << "\n The information of the object yr2 is as follows: " << endl; cout << " The object season is " << yr2.FindSeason(); yr2.BadShowTreeColor(); cout << "\n The information of the object yr3 is as follows: " << endl; cout << " The object season is " << yr3.FindSeason(); yr3.ShowTreeColor(); cout << "\n The information of the object yr4 is as follows: " << endl; cout << " The object season is " << yr4.BadFindSeason(); yr4.BadShowTreeColor(); cout << "\n Changing the month of object yr4" << endl; yr4.SetMonth( Year::April ); cout << " The information of the object yr4 now is as follows: " << endl; cout << " The object season is " << yr4.BadFindSeason(); yr4.ShowTreeColor(); cout << "\n Changing the month of object yr4 again" << endl; yr4.SetMonth( Year::Nov ); cout << " The information of the object yr4 now is as follows: " << endl; cout << " The object season is " << yr4.BadFindSeason(); yr4.ShowTreeColor(); cout << "\n Changing the month of object yr4 again" << endl; yr4.SetMonth( Year::Feb ); cout << " The information of the object yr4 now is as follows: " << endl; cout << " The object season is " << yr4.BadFindSeason(); yr4.ShowTreeColor(); return 0; } // The output of the program is: /* The information of the object yr1 is as follows: The object season is Fall. The trees color at this season is: 1 The information of the object yr2 is as follows: The object season is Winter. The trees color at this season is: 3 The information of the object yr3 is as follows: The object season is Fall. The trees color at this season is: red The information of the object yr4 is as follows: The object season is 3. The trees color at this season is: 0 Changing the month of object yr4 The information of the object yr4 now is as follows: The object season is 2. The trees color at this season is: lightgreen Changing the month of object yr4 again The information of the object yr4 now is as follows: The object season is 0. The trees color at this season is: red Changing the month of object yr4 again The information of the object yr4 now is as follows: The object season is 1. The trees color at this season is: white */