< previous page page_89 next page >

Page 89
of a cat, and each individual Cat object. Thus, Frisky is an object of type Cat in the same way in which GrossWeight is a variable of type unsigned int.
An object is simply an individual instance of a class.
Accessing Class Members
After you define an actual Cat object (for example, Frisky), you use the dot operator (.) to access the members of that object. Therefore, to assign 50 to Frisky's itsWeight member variable, you would write
Frisky.itsWeight = 50;
In the same way, to call the Meow() function, you would write
Frisky.Meow();
Assign to Objects, Not to Classes
In C++ you don't assign values to types; you assign values to variables. For example, you would never write
int = 5;               // wrong
The compiler would flag this as an error because you can't assign 5 to an integer. Rather, you must define an integer variable and assign 5 to that variable. For example:
int  x;                // define x to be an int
x = 5;                 // set x's value to 5
This is a shorthand way of saying, Assign 5 to the variable x, which is of type int. In the same way, you wouldn't write
Cat.age=5;
You must define a Cat object and assign 5 to that object. For example,
Cat Frisky;            // just like  int x;
Frisky.itsAge = 5;        // just like  x = 5;
Private Versus Public
New Term: Other keywords are used in the declaration of a class. Two of the most important are public and private.
All members of a classdata and methodsare private by default. Private members can be accessed only within methods of the class itself. Public members can be accessed

 
< previous page page_89 next page >

If you like this book, buy it!