< previous page page_91 next page >

Page 91
You use access control keywords to declare sections of the class as public or private. Each keyword changes the access control from that point on to the end of the class or until the next access control keyword. Class declarations end with a closing brace and a semicolon.
Take a look at the following example:
class Cat
{
public:
    unsigned int Age;
    unsigned int Weight;
    void Meow();
};

Cat  Frisky;
Frisky.Age = 8;
Frisky.Weight = 18;
Frisky.Meow();
Now look at this example:
class Car
{
public:                            // the next five are public

    void Start();
    void Accelerate();
    void Brake();
    void SetYear(int year);
    int GetYear();

private:                           // the rest is private

    int Year;
    Char Model [255];
};                                 // end of class declaration

Car OldFaithful;                   // make an instance of car
int bought;                        // a local variable of type int
OldFaithful.SetYear(84) ;          // assign 84 to the year
bought = OldFaithful.GetYear();    // set bought to 84
OldFaithful.Start();               // call the start method
Implementing Class Methods
Every class method that you declare must also be defined.
New Term: A member function definition begins with the name of the class followed by two colons, the name of the function, and its parameters. Listing 6.1 shows the

 
< previous page page_91 next page >

If you like this book, buy it!