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.For example:

   class Frame {	// represent a graphical user interface window
   
      /* the body of the class definition
         goes in here between the curly braces */
   };

defines a class named Frame that might represent the outer boundary of a window in a graphical user interface system. The body of a class 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 multi-line comment begins with the pair of characters "/*" and end with the matching pair of characters "*/".

An object-oriented program typically involves several, 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 defines an object identified by the variable name "display". The properties of this object are exactly those described in the Frame class definition.

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 declaration 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 like "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, equivalently of different types.


Next Stop


Exercises

  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.

  8. Write a different answer to the last question.

  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 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 they styles are different.

Last Updated: August 20, 1995 / kafura@cs.vt.edu