C++

DEFINING AN OBJECT

You define an object of your new type just as you define an integer variable:

unsigned int GrossWeight; // define an unsigned integer

Cat Frisky; // define a Cat

This code defines a variable called Gross Weight whose type is an unsigned integer. It also defines Frisky, which is an object whose class (or type) is Cat.

Classes Versus Objects

You never pet the definition of a cat; you pet individual cats. You draw a distinction between the idea of a cat, and the particular cat that right now is shedding all over your living room. In the same way, C++ differentiates between the class Cat, which is the idea 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.

New Term: An object is an individual instance of a class.

Accessing Class Members

Once 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 Weight member variable, you would write

Frisky.Weight = 50;

In the same way, to call the Meow() function, you would write

Frisky.Meow();

When you use a class method, you call the method. In this example, you are calling Meow() on Frisky.

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; // wrong  ???

The compiler would flag this as an error, because you can’t assign 5 to the age part of a Cat. Rather, you must define a Cat object and assign 5 to that object. For example,

Cat Frisky; // just like int x;

Frisky.age = 5; // just like x = 5;

If You Dont Declare It, Your Class Wont Have It

Try this experiment: Walk up to a three-year-old and show her a cat. Then say, "This is Frisky. Frisky knows a trick. Frisky, bark." The child will giggle and say, "No, silly, cats can’t bark."

If you wrote

Cat Frisky; // make a Cat named Frisky

Frisky.Bark() // tell Frisky to bark

the compiler would say, No, silly, Cats can’t bark. (Your compiler’s wording may vary). The compiler knows that Frisky can’t bark because the Cat class doesn’t have a Bark() function. The compiler wouldn’t even let Frisky meow if you didn’t define a Meow() function.

DO use the keyword class to declare a class. DON’T confuse a declaration with a definition. A declaration says what a class is. A definition sets aside memory for an object. DON’T confuse a class with an object. DON’T assign values to a class. Assign values to the data members of an object. DO use the dot operator (.) to access class members and functions.

Back to Index