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 */ };
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"
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;
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 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.
|
Exercises |
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.