Enums Inside Classes


You can place an enum definition within a class, and have the enum values follow the standard class access rules of private, public, and protected. Once defined, you access the enum as any other member, but for public members outside the class you must use the scope resolution operator:

 #include <iostream> 
class MDate
{
public:
enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };
MDate() { M=0; D=0; Y=0; WeekDay=Sun; }; //
Use 'Sun' constant value
Day GetWeekDay( void ) { return( WeekDay ); }; //
Use 'Day' data type
private:
Day WeekDay; // Use of Day data type
char M, Y, D;
};
void main( void )
{
MDate Today;
MDate::Day D; // Note scope resolution operator
D = Today.GetWeekDay();
if( D == MDate::Sun ) // Note scope resolution operator
cout << "All is well\n";
}



OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net