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 */

The body of the Frame class's definition will be seen shortly. The class definition ends with a semicolon.

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"

These classes will be seen and used shortly.

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(...);

This declares an object identified by the variable named display. The ellipsis (i.e., the ...) is not part of C++ syntax, but is used to denote the values that are typically required to initialize the object being declared. The values that should be given here will be seen when the complete Frame class definition is presented.

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(...);

These two declarations create three Frame objects. Notice that several objects can be created with one declaration, as is done with display and viewer. A comma must separate adjacent names in the same declaration. Also notice that, as in this example, the same class can be used in different declarations.

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.


  1. Is it correct to have many classes with different names?
  2. Is it correct have many objects of the same class?
  3. Is it correct to define "class int {...}; "?
  4. Is it correct to declare "Frame Frame;"? That is, can an object and a class have the same name?
  5. Is it correct to declare "Frame frame;"?
  6. Is it correct to declare "Frame aFrame;"?
  7. Write three declarations that create four Frame objects and two Message objects. Use the ellipsis notation to indicate where the initializing values would appear.
  8. Write two declarations that create four Frame objects and two Message objects. Use the ellipsis notation to indicate where the initializing values would appear.
  9. Name five other classes that might be part of a graphical user interface system. Present your answer in the form class ClassName {...}; where "ClassName" is the name of the class that you have chosen.
  10. Give the declarations for the system described below in what you consider the best style.

    An application has two windows, one for receiving user commands and one for displaying status information. Each window has a message that identifies the window. The command window has two areas where editable text can be displayed, one area for a command and one for command options. The command window has two buttons, one used to execute the command and one to stop the command's execution. The status window has a second message that is used to display any error messages that result from a command's execution.

  11. Compare your answer to the last question with someone who has a different style. Identify the ways in which the styles are different.

©1998 Prentice-Hall, Inc.
A Simon & Schuster Company
Upper Saddle River, New Jersey 07458

Legal Statement