-----

C++ Classes

-----

A simple class

class Myclass {
 private 
    char *name;
    int ID;
    float xsize;
    float ysize;
    Myclass *next;  // pointer to next object
 public:
};

Allocating and deallocating space

Examples of creating and destroying objects of class Myclass. Notice that new returns a pointer pointing to a new object of the specified class.

Myclass *temp;
temp = new Myclass;

.....

delete temp;

To create and destroy an array of objects (in this case, floats):

float *temp;
temp = new float[37];

....

delete [] temp;

Notice the brackets in the call to delete. This tells delete to remove the entire array, rather than just its first element.

Features of the C++ class system

protection levels
friend declarations
give functions from outside a class access to your class's private data and private functions, without making them public
constructors
C++ calls a class's constructor (if there is one) after creating a new instance of the class. Constructors are often used to set up initial values for data in the new object and to allocate space for sub-objects (e.g. for an array contained in the new object). A class can have multiple constructors, taking different type of arguments.
copy constructor
a special name for a constructor that takes an existing instance of the class as input. This constructor is called if an object is initialized with another object. See Stroustrup section 10.4.4.1.
destructor
when delete is called on an object, C++ calls its destructor function (if one is defined) before destroying the object. The destructor can deallocate storage within the object (e.g. an array contained in one of its fields) or perform other clean-up operations. A class cannot have more than one destructor.
template classes and functions
with one definition, create a whole set of classes or functions. These classes and functions are similar, except for the type of one or more variables. Example: a general definition of "linked list" which can be used both for lists of integers and lists of floats.
inheritance
build a new class as a more specialized version of an existing class (Stroustrup chapter 12, esp. 12.2 and following). existing class. These subclasses and their functions have access to protected parts of the original (base) class, in addition to the public parts that everyone can access.
inline declarations for functions
a suggestion to the compiler that the function's code be substituted into functions which call it, rather than using an actual function call. Useful for speeding up functions whose code is very simple.

If the function's code is given within the class definition, not as a separate block, C++ will try to inline the function: an explicit inline declaration is unnecessary.

operator overloading
supply new definitions of basic C++ operations such as arithmetic (e.g. +), I/O (e.g. >>), array access ([]) and assignment (=). This is more convenient for people using the class than having to remember and type names for these functions, e.g. add, output, get_element, copy.

Only a fixed list of operators can be overloaded. They are listed on p. 262 of Stroustrup. Stroustrup (p. 262 and following) also details the specifications you must satisfy if you overload certain operators, e.g. precisely what input types they must accept and what output type they must return.

overloading the assignment operator
by default, assignment copies all the fields in the class. If a field is a pointer, this means that it copies the pointer (address), not the object at that address. This sub-object is often an array or string. Access to the sub-object will be shared. If you want the new object to have its own private copy of the sub-object, you must overload the assignment operator.
this
when you are running a class function, the variable this contains a pointer to the object on which this function was invoked.
static
for most class variables, each instance of the class has its own copy of the variable. If a variable is declared static, all instances share the same variable.


This page is maintained by Geoff Kuenning.