3 Classes

Contents of this section

We're always interested in getting feedback. E-mail us if you like this guide, if you think that important material is omitted, if you encounter errors in the code examples or in the documentation, if you find any typos, or generally just if you feel like e-mailing. Mail to Karel Kubat (karel@icce.rug.nl) or use an e-mail form . Please state the concerned document version, found in the title. If you're interested in a printable PostScript copy, use the form . or better yet, pick up your own copy via ftp at ftp.icce.rug.nl/pub/http ,

The usage of classes is further explained in this chapter. Two special member functions, the constructors and the destructor, are introduced.

In steps we will construct a class Person, which could be used in a database application to store a name, an address and a phone number.

The definition of a Person so far is as follows:

    class Person
    {
        public:                 // interface functions
            void setname (char const *n);
            void setaddress (char const *a);
            void setphone (char const *p);

            char const *getname (void);
            char const *getaddress (void);
            char const *getphone (void);

        private:                // data fields
            char *name;         // name of person
            char *address;      // address field
            char *phone;        // telephone number
    };

The data fields in this class are name, address and phone. The fields are char*s which point to allocated memory. The data are private, which means that they can only be accessed by the functions of the class Person.

The data are manipulated by interface functions which take care of all communication with code outside the class; either to set the data fields to a given value (e.g., setname()) or to inspect the data (e.g., getname()).

3.1 Constructors and destructors

A class in C++ may contain two special functions which are involved in the internal workings of the class. These functions are the constructors and the destructor.

The constructor

The constructor function has by definition the same name as the corresponding class. The constructor has no return value specification, not even void. E.g., for the class Person the constructor is Person::Person(). The C++ run-time system makes sure that the constructor of a class, if defined, is called when an object of the class is created. It is of course possible to define a class which has no constructor at all; in that case the run-time system either calls no function or it calls a dummy constructor (i.e., which performs no actions) when a corresponding object is created. The actual generated code of course depends on the compiler. (A compiler-supplied constructor in a class which contains composed objects (see section Composition ) will `automatically' call the member initializers, and therefore does perform some actions. We postpone the discussion of such constructors to MemberInitializers .)

When an object is a local non-static variable in a function, the constructor is called when the function is executed. When an object is a global or a static variable, the constructor is called when the program starts; even before main() gets executed.

This is illustrated in the following listing:

    #include <stdio.h>

    // a class Test with a constructor function
    class Test
    {
        public:                 // 'public' function:
            Test ();            // the constructor
    };

    Test::Test ()               // here is the
    {                           // definition
        puts ("constructor of class Test called");
    }

    // and here is the test program:
    Test                
        g;                      // global object

    void func ()
    {
        Test                    // local object
            l;                  // in function func()

        puts ("here's function func()");
    }

    int main ()
    {
        Test                    // local object
            x;                  // in function main()

        puts ("main() function");
        func ();
        return (0);
    }

The listing shows how a class Test is defined which consists of only one function: the constructor. The constructor performs only one action; a message is printed. The program contains three objects of the class Test: one global object, one local object in main() and one local object in func().

Concerning the definition of a constructor we remark the following:

The constructor of the three objects of the class Test in the above listing are called in the following order:

As expected, the program yields therefore the following output (the text in parentheses is for illustratory purposes):

    constructor of class Test called        (global object g)
    constructor of class Test called        (object x in main())
    main() function
    constructor of class Test called        (object l in func())
    here's function func()

The destructor

The second special function is the destructor. This function is the opposite of the constructor in the sense that it is invoked when an object ceases to exist. For objects which are local non-static variables, the destructor is called when the function in which the object is defined is about to return; for static or global variables the destructor is called before the program terminates. Even when a program is interrupted using an exit() call, the destructors are called for objects which exist at that time.

When defining a destructor for a given class the following rules apply:

A destructor for the class Test from the previous section could be declared as follows:

    class Test
    {
        public:
            Test ();                    // constructor
            ~Test ();                   // destructor
            .
            .
    };

A first application

One of the applications of constructors and destructors is the management of memory allocation. This is illustrated using the class Person.

As illustrated at the beginning of this chapter, the class Person contains three private pointers, all char*s. These data members are manipulated by the interface functions. The internal workings of the class are as follows: when a name, address or phone number of a Person is defined, memory is allocated to store these data. An obvious setup is described below:

The set...() functions are illustrated below. Strings are duplicated in this example by an imaginary function xstrdup(), which would duplicate a string or terminate the program when the memory pool is exhausted.

    // interface functions set...()
    void Person::setname (char const *n)
    {
        free (name);
        name = xstrdup (n);
    }

    void Person::setaddress (char const *n)
    {
        free (address);
        address = xstrdup (n);
    }

    void Person::setphone (char const *n)
    {
        free (phone);
        phone = xstrdup (n);
    }

Note that the statements free(...) in the above listing are executed unconditionally. This never leads to incorrect actions: when a name, address or phone number is defined, the corresponding pointers address previously allocated memory which should be freed. When the data are not (yet) defined, then the corresponding pointer is a 0-pointer; and free(0) performs no action. Furthermore it should be noted that this code example uses the standard C function free() which should be familiar to most C programmers. The delete statement, which has more `C++ flavor', will be discussed later.

The interface functions get...() are listed below:

        // interface functions get...()
    char const *Person::getname ()
    {
        return (name);
    }

    char const *Person::getaddress ()
    {
       return (address);
    }

    char const *Person::getphone ()
    {
       return (phone);
    }

Finally the destructor, constructor and the class definition are given below:

    // class definition
    class Person
    {
        public:
            Person ();          // constructor
            ~Person ();         // destructor

            // functions to set fields
            void setname (char const *n);
            void setaddress (char const *a);
            void setphone (char const *p);

            // functions to inspect fields
            char const *getname (void);
            char const *getaddress (void);
            char const *getphone (void);

        private:
            char *name;             // name of person
            char *address;          // address field
            char *phone;            // telephone number
    };

    // constructor
    Person::Person ()
    {
        name = address = phone = 0;
    }

    // destructor
    Person::~Person ()
    {
        free (name);
        free (address);
        free (phone);
    }

To demonstrate the usage of the class Person, a code example follows below. An object is initialized and passed to a function printperson(), which prints the contained data. Note also the usage of the reference operator & in the argument list of the function printperson(). This way only a reference to a Person object is passed, instead of a whole object. The fact that printperson() does not modify its argument is evident from the fact that the argument is declared const. Also note that the example doesn't show where the destructor is called; this action occurs implicitly when the below function main() terminates and hence when its local variable p ceases to exist.

It should also be noted that the function printperson() could be defined as a public member function of the class Person.

    void printperson (Person const &p)
    {
        printf ("Name    : %s\n"
                "Address : %s\n"
                "Phone   : %s\n",
        p.getname (), p.getaddress (), p.getphone ());
    }

    int main ()
    {
        Person
            p;

        p.setname ("Linus Torvalds");
        p.setaddress ("E-mail: Torvalds@cs.helsinki.fi");
        p.setphone (" - not sure - ");

        printperson (p);
        return (0);
    }

At this point it we should also note that the above code fragment can only serve as an example: most C++ compilers will actually fail to parse the code. The reason for this is that the function printperson() receives a const argument, but calls functions for this argument which might or might not modify it (these are the functions getname(), getaddress() and getphone()). Given this setup, the `constness' of the argument to printperson() cannot be guaranteed -- and hence, the compiler will not produce working code. The solution would of course be to tell the compiler that getname(), getaddress() and getphone() will not modify the object at hand: but we postpone this modification to section ConstFunctions .

When printperson() receives a fully defined Person object (i.e., containing a name, address and phone number), the data are correctly printed. However, when a Person object is only partially filled, e.g. with only a name, printperson() passes 0-pointers to printf(). This anesthetic feature can be remedied with a little more code:

    void printperson (Person const &p)
    {
        if (p.getname ())
            printf ("Name   : %s\n", p.getname ());
        if (p.getaddress ())
            printf ("Address : %s\n", p.getaddress ());
        if (p.getphone ())
            printf ("Phone  : %s\n", p.getphone ());
    }

Constructors with arguments

In the above definition of the class Person the constructor and destructor have no arguments. C++ allows the constructor to be defined with an argument list which is supplied when an object is created.

For the class Person a constructor may be handy which expects three strings: the name, address and phone number. Such a constructor is shown below:

    Person::Person (char const *n, char const *a, char const *p)
    {
        name = xstrdup (n);
        address = xstrdup (a);
        phone = xstrdup (p);
    }

The constructor must be included in the class definition. A declaration in, e.g., a header file, would then look as follows:

    class Person
    {
        public:
            Person::Person (char const *n,
                char const *a, char const *p);
            .
            .
            .
    };

Since C++ allows function overloading, such a declaration of a constructor can co-exist with a constructor without arguments. The class Person would thus have two constructors.

The usage of a constructor with arguments is illustrated in the following code fragment. The object a is initialized at its definition:

    int main ()
    {
        Person
            a ("Karel", "Rietveldlaan 37", "426044"),
            b;

        .
        .
    }

The order of construction

The possibility to define arguments with constructors offers us the chance to monitor at which exact moment in a program's execution an object is created or destroyed. This is shown in the below listing, using a class Test:

    class Test
    {
        public:
            // constructors:
            Test ();                    // argument-free
            Test (char const *name);    // with a name argument
            // destructor:
            ~Test ();

        private:
            // data:
            char *n;                    // name field
    };

    Test::Test ()
    {
        n = strdup ("without name");
        printf ("Test object without name created\n");
    }

    Test::Test (char const *name)
    {
        n = strdup (name);
        printf ("Test object %s created\n", n);
    }

    Test::~Test ()
    {
        printf ("Test object %s destroyed\n", n);
        free (n);
    }

By defining objects of the class Test with specific names, the construction and destruction of these objects can be monitored:

    Test
        globaltest ("global");

    void func ()
    {
        Test
            functest ("func");
    }

    int main ()
    {
        Test
            maintest ("main");

        func ();
        return (0);
    }

This test program leads to the following (and expected) output:

    Test object global created
    Test object main created
    Test object func created
    Test object func destroyed
    Test object main destroyed
    Test object global destroyed

3.2 Const member functions and const objects

The keyword const is often seen in the declarations of member functions following the argument list. This keyword is used to indicate that a member function does not alter the data fields of its object, but only inspects them. Using the example of the class Person, the get...() functions could be declared const:

    class Person
    {
        public:
            .
            .
            // functions to inspect fields
            char const *getname (void) const;
            char const *getaddress (void) const;
            char const *getphone (void) const;

        private:
            .
            .
    };

As is illustrated in this fragment, the keyword const occurs following the argument list of functions. Again the rule of thumb from section ConstRule applies: whichever appears before the keyword const, may not be altered or doesn't alter data.

The same specification must be repeated in the definition of member functions:

    char const *Person::getname () const
    {
        return (name);
    }

A member function which is declared and defined as const may not alter any data fields of its class. In other words, a statement like

    name = 0;

in the above const function getname() would lead to a compilation error.

The purpose of const functions lies in the fact that C++ allows const objects to be created. For such objects only the functions which do not modify it; i.e., the const member functions, may be called. The only exception to the rule are the constructor and destructor: these are called `automatically'. This feature is comparable to the definition of a variable int const max = 10: such a variable may be initialized at its definition. Analogously the constructor can initialize its object at the definition, but subsequent assignments may not take place.

The following example shows how a const objects of the class Person can be defined. At the definition of an object the data fields are initialized (this is an action of the constructor):

    Person
        const me ("Karel", "karel@icce.rug.nl", "426044");

Following this definition it would be illegal to try to redefine the name, address or phone number for the object me: a statement as

    me.setname ("Lerak");

would not be accepted by the compiler.

Generally it is a good habit to declare member functions which do not modify their object as const. This subsequently allows the definition of const objects.

3.3 The operators new and delete

The C++ language defines two operators which are specific for the allocation and deallocation of memory. These operators are new and delete.

The most basic example of the usage of these operators is given below. A pointer variable to an int is used to point memory to which is allocated by new. This memory is later released by the operator delete.

    int
        *ip;

    ip = new int;
    .
    .
    delete ip;

Note that new and delete are operators and therefore do not require parentheses, such as is the case with functions like malloc() and free().

Allocating and deallocating arrays

When the operator new is used to allocate an array, the size of the variable is placed between square brackets following the type:

    int
        *intarr;

    intarr = new int [20];    // allocates 20 ints

The syntactical rule for the operator new is that this operator must be followed by a type, optionally followed by a number in square brackets. The type and number specification lead to an expression which is used by the compiler to deduce the size; in C an expression like sizeof(int[20]) might be used.

An array is deallocated by using the operator delete:

    delete [] intarr;

In this statement the array operators [] indicate that an array is being deallocated. The rule of thumb is here: whenever new is followed by [], delete should be followed by it too.

New and delete and object pointers

The operators new and delete are also used when an object of a given class is allocated. The advantage of the operators over functions as malloc() and free() lies in the fact that new and delete call the corresponding constructor or destructor. This is illustrated in the below example:

    Person
        *pp;                    // ptr to Person object

    pp = new Person;            // now constructed
    .
    .
    delete pp;                  // now destroyed

The allocation of a new Person object pointed to by pp is a two-step process. First, the memory for the object itself is allocated. Second, the constructor is called which initializes the object. In the above example the constructor is the argument-free version; it is however also possible to choose an explicit constructor:

    pp = new Person ("Frank", "Oostumerweg 17", "05903-2223");
    .
    .
    delete pp;

Note that, analogously to the construction of an object, the destruction is also a two-step process: first, the destructor of the class is called to deallocate the memory which the object uses. Then the memory which is used by the object itself is freed.

Dynamically allocated arrays of objects can also be manipulated with new and delete. In this case the size of the array is given between the [] when the array is created:

    Person
        *personarray;

    personarray = new Person [10];

The compiler will generate code to call the default constructor for each object which is created. To release such an array, the array operators [] must be used with the delete operator:

    delete [] personarray;

The presence of the [] ensures that the destructor is called for each object in the array. Note that delete personarray would only release the memory of the array itself.

The function set_new_handler()

The C++ run-time system makes sure that when memory allocation fails, an error function is activated. By default this function returns the value 0 to the caller of new, so that the pointer which is assigned by new is set to zero. The error function can be redefined, but it must comply with a few prerequisites, which are, unfortunately, compiler-dependent. E.g., for the Microsoft C/C++ compiler version 7, the prerequisites are:

The Gnu C/C++ compiler gcc, which is present on many Unix platforms, requires that the error handler:

The redefined error function might, e.g., print a message and terminate the program. The error function is included in the allocation system by the function set_new_handler(), defined in the header file new.h. On some compilers, notably the Microsoft C/C++ 7 compiler, the installing function is called _set_new_handler() (note the leading underscore).

The implementation of an error function is illustrated below. This implementation applies to the Microsoft C/C++ requirements:

    #include <new.h>
    #include <stdlib.h>
    #include <stdio.h>

    int out_of_memory (size_t sz)
    {
        printf ("Memory exhausted, can't allocate %u bytes\n", sz);
        exit (1);

        return (0);     // return an int to satisfy the
                        // declaration
    }

    int main ()
    {
        int
            *ip;
        long
            total_allocated = 0L;
            
        // install error function
        set_new_handler (out_of_memory);

        // eat up all memory
        puts ("Ok, allocating..");
        while (1)
        {
            ip = new int [100];
            total_allocated += 100L;
            printf ("Now got a total of %ld bytes\n",
                    total_allocated);
        }

        return (0);
    }

The advantage of an allocation error function lies in the fact that once installed, new can be used without wondering whether the allocation succeeded or not: upon failure the error function is automatically invoked and the program exits. It is good practice to install a new handler in each C++ program, even when the actual code of the program does not allocate memory. Memory allocation can also fail in not directly visible code, e.g., when streams are used or when strings are duplicated by low-level functions.

Often, even standard C functions which allocate memory, such as strdup(), malloc(), realloc() etc. trigger the new handler when memory allocation fails. This means that once a new handler is installed, such functions can be used in a C++ program without testing for errors. However compilers exist where the C functions do not trigger the new handler.

3.4 The keyword inline

Let us take another look at the implementation of the function Person::getname():

    char const *Person::getname () const
    {
        return (name);
    }

This function is used to retrieve the name field of an object of the class Person. In a code fragment, like:

    Person
        frank ("Frank", "Oostumerweg 23", "2223");

    puts (frank.getname ());

the following actions take place:

Especially the first part of these actions leads to time loss, since an extra function call is necessary to retrieve the value of the name field. Sometimes a faster process may be desirable, in which the name field becomes immediately available; thus avoiding the call to getname(). This can be realized with inline functions, which can be defined in two ways.

Inline functions within class definitions

Using the first method to implement inline functions, the code of a function is defined in a class definition itself. For the class Person this would lead to the following implementation of getname():

    class Person
    {
        public:
            .
            .
            char const *getname (void) const
                { return (name); }
            .
            .
    };

Note that the code of the function getname() now literally occurs in the definition of the class Person. The keyword const occurs after the function declaration, and before the code block.

The effect of this is the following. When getname() is called in a program statement, the compiler generates the code of the function instead of a call. This construction is called an inline function because the compiler as it were `inserts' the actual code of the function.

Inline functions outside of class definitions

The second way to implement inline functions leaves a class definition intact, but mentions the keyword inline in the function definition. The class and function definitions then are:

    class Person
    {
        public:
            .
            .
            char const *getname (void) const;
            .
        private:
            .
            .
    };

    inline char const *Person::getname () const
    {
        return (name);
    }

Again the compiler will insert the code of the function getname() instead of generating a call.

When to use inline functions

When should inline functions be used, and when not? There are a number of simple rules of thumb:

inline functions have one disadvantage: the actual code is inserted by the compiler and must therefore be known compile-time. Therefore an inline function can never be located in a run-time library. Practically this means that an inline function is placed near the definition of a class, usually in the same header file. The result is a header file which not only shows the declaration of a class, but also part of its implementation.

3.5 Objects in objects: composition

An often recurring situation is one where objects are used as data fields in class definitions. This is referred to as composition.

For example, the class Person could hold information about the name, address and phone number, but additionally a class Date could be used to include information about the birth date:

    class Person
    {
        public:
            // constructor and destructor
            Person ();
            Person (char const *nm, char const *adr,
                    char const *ph);
            ~Person ();

            // interface functions
            void setname (char const *n);
            void setaddress (char const *a);
            void setphone (char const *p);
            void setbirthday (int yr, int mnth, int d);

            char const *getname () const;
            char const *getaddress () const;
            char const *getphone () const;
            int getbirthyear () const;
            int getbirthmonth () const;
            int getbirthday () const;

        private:
            // data fields
            char *name, *address, *phone;
            Date birthday;
    };          

We shall not further elaborate on the class Date: this class could, e.g., consist of three int data fields to store a year, month and day. These data fields would be set and inspected using interface functions setyear(), getyear() etc..

The interface functions of the class Person would then use Date's interface functions to manipulate the birth date. As an example the function getbirthyear() of the class Person is given below:

    int Person::getbirthyear () const
    {
        return (birthday.getyear ());
    }

Composition is not extraordinary or C++ specific: in C it is quite common to include structs or unions in other compound types.

Composition and const objects: member initializers

Composition of objects has an important consequence for the constructor functions of the `composed' (embedded) object. Unless explicitely instructed otherwise, the compiler generates code to call the default constructors of all composed classes in the constructor of the composing class.

Often it is desirable to initialize a composed object from the constructor of the composing class. This is illustrated below for the composed class Date in a Person. In this fragment it assumed that a constructor for a Person should be defined expecting six arguments: the name, address and phone number plus the year, month and day of the birth date. It is furthermore assumed that the composed class Date has a constructor with three int arguments for the year, month and day:

    Person::Person (char const *nm, char const *adr,
                    char const *ph,
                    int d, int m, int y)
        : birthday (d, m, y)
    {
        name = strdup (nm);
        address = strdup (adr);
        phone = strdup (ph);
    }

Note that following the argument list of the constructor Person::Person(), the constructor of the data field Date is specifically called, supplied with three arguments. This constructor is explicitly called for the composed object birthday. This occurs even before the code block of Person::Person() is executed. This means that when a Person object is constructed and when six arguments are supplied to the constructor, the birthday field of the object is initialized even before Person's own data fields are set to their values. The constructor of the composed data member is also referred to as member initializer.

When several composed data members of a class exist, all member initializers can be called by using a `constructor list': this list consists of the constructors of all composed objects, separated by commas.

When member initializers are not used, the compiler automatically supplies a call to the default constructor (i.e., the constructor without arguments). In this case a default constructor must be defined in the composed class.

Not using member initializers can also lead to inefficient code. E.g., consider the following code fragment where the birthday field is not initialized by the Date constructor, but instead the setday(), setmonth() and setyear() functions are called:

    Person::Person (char const *nm, char const *adr,
                    char const *ph,
                    int d, int m, int y)
    {
        name = strdup (nm);
        address = strdup (adr);
        phone = strdup (ph);

        birthday.setday (d);
        birthday.setmonth (m);
        birthday.setyear (y);
    }

This code is inefficient because:

This method is not only inefficient, but may even not work when the composed object is declared as const. A data field like birthday is a good candidate for being const, since a person's birthday is not likely to change.

This means that when the definition of a Person is changed so that the data member birthday is declared as const, the implementation of the constructor Person::Person() with six arguments must use member initializers. The call to birthday.set...() would be illegal, since this is no const function.

Concluding, the rule of thumb is the following: when composition of objects is used, the member initializer method is preferred to explicit initialization of the composed object. This not only leads to more efficient code, but also allows the composed object to be declared as const.

3.6 Friend functions and friend classes

As we have seen in the previous sections, private data or function members are normally only accessible by the code which is part of the corresponding class. However, situations may arise in which it is desirable to allow the explicit access to private members of one class to one or more other classless functions or member functions of classes.

E.g., consider the following code example (all functions are inline for purposes of brevity):

    class A                                 // class A: just stores an
    {                                       // int value via the constructor
        public:                             // and can retrieve it via
            A (int v)                       // getval
                { value = v; }
            int getval ()
                { return (value); }

        private:
            int value;
    };

    void decrement (A &a)               // function decrement: tries
    {                                       // to alter A's private data
        a.value--;
    }

    class B                                 // class B: tries to touch
    {                                       // A's private parts
        public:
            void touch (A &a)
                { a.value++; }
    };

This code will not compile, since the function decrement() and the function touch() of the class B attempt to access a private datamember of A.

We can explicitely allow decrement() to access A's data, and we can explicitely allow the class B to access these data. To accomplish this, the offending classless function decrement() and the class B are declared to be friends of A:

    class A
    {
        public:
            friend class B;             // B's my buddy, I trust him

            friend void decrement (A    // decrement() is also a good pal
                &what);
            .
            .
    };

Concerning friendship between classes, we remark the following:

Next Chapter, Previous Chapter

Table of contents of this chapter, General table of contents

Top of the document, Beginning of this Chapter