O'Reilly logo

Sams Teach Yourself C++ in 24 Hours, Third Edition by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Accessing Class Members

After you define an actual Cat object (for example, Frisky), you use the dot operator (.) to access the member functions 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required