< previous page page_87 next page >

Page 87
New Term: Encapsulation is bundling together all the information, capabilities, and responsibilities of an entity into a single object.
Encapsulating everything you know about a car into one class has a number of advantages for a programmer. Everything is in one place, which makes it easy to refer to, copy, and manipulate the data.
New Term: Clients of your class are other classes or functions that make use of your class. Encapsulation allows the clients of your class to use it without knowing or caring about how it works. You can drive a car without understanding how the internal combustion engine operates, and clients of your class can use your class without knowing how your class does what it does. They only need to know what it does, not how it does it.
A class can consist of any combination of the variable types and also other class types. The variables in the class are referred to as the member variables or data members. A Car class might have member variables representing the seats, radio type, tires, and so forth.
New Term: Member variables, also known as data members, are the variables in your class. Member variables are part of your class, just like the wheels and engine are part of your car.
New Term: The functions in the class typically manipulate the member variables. They are referred to as member functions or methods of the class. Methods of the Car class might include Start() and Brake(). A Cat class might have data members that represent age and weight; its methods might include Sleep(), Meow(), and ChaseMice().
Member functions, also known as methods, are the functions in your class. Member functions are as much a part of your class as the member variables. They determine what the objects of your class can do.
Declaring a Class
To declare a class, you use the class keyword followed by an opening brace and then list the data members and methods of that class. End the declaration with a closing brace and a semicolon. Here's the declaration of a class called Cat:
class Cat
{
public:
    unsigned int  itsAge;
    unsigned int  itsWeight;
    Meow();
};
Declaring this class doesn't allocate memory for a Cat. It just tells the compiler what a Cat is, what data it contains (itsAge and itsWeight), and what it can do (Meow()). It

 
< previous page page_87 next page >

If you like this book, buy it!