|
|
|
|
|
|
|
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. |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
In the same way, to call the Meow() function, you would write |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|