Prev |
Introduction to classes | ||||
---|---|---|---|---|
A class is a factory for producing objects. It also provides the implementations of the operations, usually called methods, that are done by the objects in the class. A class is similar to an abstract data type, and we will use classes to implement abstract data types in a secure manner, hiding what needs to be hidden.
|
The class interface | ||||
---|---|---|---|---|
Ideally, the class interface would tell only the class name and the operations that its objects support. Unfortunately, for reasons similar to those that required structures to be put in header files, you must provide information about the data representation in the header file. A feature of classes that partially counteracts this is that you can indicate that the variables are private. Private variables can only be used by the member functions of the class. This gives you a much more secure implementation of an ADT than would be obtained by making the variables visible to everybody. A class is similar in form to a structure. In the class, you list the variables and give prototypes of the member functions. Each member function, however, comes with an implicit parameter: the object that performs the function. So you typically have one less explicit parameter than you would using a structure-based implementation. Here is an interface, given in file cintstack.h, of a stack of integers ADT. This is a class-based ADT similar to the structure-based ADT IntStack of part 4 of these notes. Notice that the function prototypes are inside the class definition, and that they have one less parameter (the stack) that in the IntStack ADT.
The missing parameter, the stack that is performing the operation, is implicitly of type CIntStack&. In the case of function isEmpty, the missing parameter is indicated to have the const attribute by placing const after the prototype. To use this class, we use object notation. Here is an example. Compare it to the example for the structure-based implementation IntStack. Line CIntStack st;creates a CIntStack object called st. This is where the class CIntStack is used like a factory.
If function main had attempted to use st.top, the compiler would have flagged the use as an error, since the top variable is private to the class, and so cannot be used by functions that are not members of the class. |
The class implementation | ||
---|---|---|
To implement a class, you must implement each of its member functions. You must indicate, in each function definition, that your are creating a member function of this class. You do that by adding the class name, followed by ::, to the function name. For example, to define function Empty in class CIntStack, you define CIntStack::Empty. Inside the definition of a member function, you can refer to the object that is performing the method by pointer value this. That is, this is the name of the implicit parameter. For class CIntStack, this has type CIntStack*. You can refer to the variables of this, and ask this to perform methods, without explicitly writing this. For example, top abbreviates this->top and Push(2) abbreviates this->Push(2). Here is an implementation of class CIntStack. Compare it to file intstack.cpp.
|
More on constructors | ||||
---|---|---|---|---|
Constructors are used for initialization of objects, and always have the same name as the class. When you define a constructor, you do not give a return type. Constructors are often written inside the class itself. But you may write them in the .cpp file if you like. A constructor for class Queue is called Queue::Queue in the .cpp file. There are a few technical problems associated with constructors that are dealt with here.
|
Another example | ||
---|---|---|
A queue also supports insertion and removal, but it is first-in-first-out. This example implements queues using stacks to help. So it both implements an ADT and uses another ADT. This example also uses a private function, Normalize.
|
Class methods and variables |
---|
When you declare a variable or function in a class, you normally intend to describe a feature of the objects that the class produces. The objects of a class are sometimes called instances of the class, so these variables and functions are called instance variables and instance functions, or instance methods. It is possible to create variables and functions that are associated with the class itself, not with the objects. A class variable is shared by all objects, as is a class function. To declare a class variable or function, precede it by the modifier static. For example, class MyClass { private: static int count; ... };creates a class MyClass that has a class variable called count. Use class variables and methods whenever you want to think of the class itself as a special object, and to give it attributes, or when you need a variable that is shared by the instances of a class. |
Prev |