![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
How It Works Lets begin by examining the header file VEHICLE.H. A class declaration in C++ begins with the keyword class, followed by an opening brace, one or more declarations, and ends with a closing brace and semicolon. Declaring a class is declaring a new type (user-defined type), just as an int or double. A class bundles together data and functionality into a self-contained package. The first source line within the class declaration is the access specifier public. The three access specifiers available are public, protected, and private. A class member that has private visibility can only be accessed by member functions of the class and friends of the class. A How-To in this chapter discusses the friend keyword and its use. Any class member declared with protected visibility is accessible by the member functions of the class and the classs descendents. Any member of a class with public visibility is accessible outside of the class. The default access for a class is private. You can also declare a user-defined type using the struct (versus class) keyword. The only difference between struct and class is that the default access for struct is public. The next declaration within the class: enum Switch { SWITCH_ON, SWITCH_OFF } ; defines an enumeration type and offers two values for the Vehicle class. This trick permits you to define constants that are only visible to the class; the definitions are not accessible outside the class. The advantage is that the global namespace is not polluted with excess constants. The next line Vehicle( ) ; is a declaration of a constructor for the class. The constructor is a special member function that is used to initialize an instance of a class. Whenever an object is created at runtime, the appropriate constructor for the class will be called to initialize the object. Notice I use the word appropriate because constructors can be overloaded. Operators, such as =, can also be overloaded for a class. To overload a member function is to use the same name for multiple versions of the function; each overloaded function must differ by its argument signature. For example, the following class declaration specifies three different constructors for a user-defined Date class: class Date { Date( ) ; Date( int year ) ; Date( int year, int month ) ; Date( int year, int month, int day ) ; //... } ; At the point of instantiation, you could call one of the constructors as appropriate. The following instantiation creates an object of type Date specifying the year and month: Date today( 1999, 1 ) ; The next declaration ~Vehicle( ) ; is the destructor for the class. The destructor is the compliment to the constructor; it is called when an object is destroyed. The name of the destructor is the name of the class with a tilde ~ character prepended to the name. A constructor and destructor for a class should be declared with public access. A constructor or destructor can be declared with protected or private access; demonstrating this and the reasons why you would do it are beyond the scope of this How-To. The next six declarations bool powerSwitch( Switch onOrOff ) ; unsigned int accelerate( unsigned int amount ) ; unsigned int decelerate( unsigned int amount ) ; bool isMoving( void ) const ; unsigned int getSpeed( void ) const ; unsigned int setSpeed( unsigned int speedIn ) ; declare member functions for the class. A member function has scope within the declaring class. This means that a member function cannot be accessed outside of the context of a class instance. The dot or arrow member access operator is used to access a member of a class. This will be demonstrated later in this How-To. The member functions of a class that have public visibility define the interface of the class. The interface of a class is the contract that a class makes with users of the class. The users of a class alter and retrieve an instance state through the interface. Users of a class should never have access to a classs data members. Each one of the member functions of Vehicle is used by users to get or set the instance data. For example, the declaration unsigned int getSpeed( void ) const ; returns the speed of the Vehicle instance. Notice that the function is declared with the const modifier. This modifier states that this function will not modify any data member of the class. This makes sense because this function simply returns the value of the speed data member. This type of member function is normally referred to as an accessor. A mutator function is used to alter one or more data members of a class. For example, the member function unsigned int setSpeed( unsigned int speedIn ) ; modifies the speed data member. Just after the member function declarations, you find the following two lines of code: protected: bool moving ; Again, any members declared after this access specifier are hidden from users of the class. Only member functions of the class and derived classes can access the members declared with protected visibility. Examine the next two lines of code: private: unsigned int speed ; Again, any members declared after this access specifier are hidden from users of the class. Only member functions of the class can access any members declared with private visibility. Next, examine the implementation for class Vehicle. The implementation is found within VEHICLE.CPP. The first member function definition is that of the classs constructor. The definition begins with the name of the class, followed by the scope resolution operator, the constructors name (again, the name of the class), and any arguments that might be declared. The definition of the constructor for the Vehicle class follows: Vehicle::Vehicle( ) : speed( 0 ), moving( false ) { }
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|