![]() |
|||||||||
2.1 Classes and Objects |
|||||||||
|
In C++, the keyword class is used to define a new class. It is an error in C++ to define
two different classes with the same name or to define the same
class twice. The following code defines a class named Frame that
might represent the outer boundary of a window in a graphical
user-interface system.
class Frame { // represent a graphical user interface window
/* the body of the class definition
goes in here between the curly braces */
};
Two different forms of comments are illustrated in the Frame class definition above. An adjacent pair of slash marks // introduces a comment that ends at the end of the current line. A multiline comment begins with the pair of characters /* and ends with the matching pair of characters */. An object-oriented program typically involves several, and perhaps
many, different classes. Other classes related to a windowing
system might be:
class Message {...}; // an unchanging line of text
class TextBox {...}; // editable lines of text
class Button {...}; // a selector that can be "pushed"
The simplest way to create an object of a class is to declare
a variable using the class's name. For example, using the Frame
class defined above, a Frame object can be declared by: Frame display(...);
As in C and many other languages, C++ requires that the definition of a class precede the use of that class in a declaration so that the C++ compiler can more easily check that the definition of a class and its use in declarations and other statements are consistent. This is part of the character of C++ as a statically-typed object-oriented language. The programmer must insure that the declaration-before-use rule is met by the organization of the program's text. It will be seen later how to achieve this organization in a straightforward way. The declaraction of an object illustrates the strong connection between the concept of a type and the concept of a class. Compare, for example, the following two declarations:
int counter;
Frame display(...);
The first declaration creates a variable whose type is int and which is named by the identifier counter. The type int determines what operations can be applied to the variable. For example, +, -, <, and = are some of the valid operations. The compiler will issue warnings or error messages if invalid operations are attempted. Similarly, the second declaration creates a variable (an object) whose type is Frame and which is named by the identifier display. As with all types, the compiler will check that the operations applied to display are appropriate. Since Frame is a programmer-defined type, the valid operations on objects are exactly those given in the definition of the Frame class. Many objects can be created from the same class. For example,
several Frame objects can be created as follows: Frame display(...), viewer(...); Frame editor(...);
In C++, a class is a type. The declaration of a variable that names an object is syntactically the same as the declaration of a variable that names a predefined, or built-in, type such as int, char, or float. The rules of type checking apply to objects just as they do to predefined types. For example, one object may be assigned to another object only (at least for now) if they are of the same class. For example, it is permissible to assign the value of viewer (defined above) to display. However, if msg is an object of class Message, then msg cannot be assigned to viewer and viewer can not be assigned to msg - in each case the two variables are of different classes, and equivalently of different types.
Tasks
|
|||||||||
|
|||||||||