< previous page page_868 next page >

Page 868
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
then the effect depends upon what constructors (if any) the class provides.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
If the class has no constructors at all, memory is allocated for anObject but its private data members are in an uninitialized state.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
If the class does have constructors, then the default (parameterless) constructor is invoked if there is one. If there is no default constructor, a syntax error occurs.
5. If a class has at least one constructor, and an array of class objects is declared:
SomeClass arr[10];
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
then one of the constructors must be the default (parameterless) constructor. This constructor is invoked for each element of the array. There is no way to pass parameters to a constructor when creating an array of class objects.
Before leaving the topic of constructors, we give you a brief preview of another special member function supported by C++: the class destructor. Just as a constructor is implicitly invoked when a class object is created, a destructor is implicitly invoked when a class object is destroyedfor example, when control leaves the block in which a local object is declared. A class destructor is named the same as a constructor except that the first character is a tilde (~):
class SomeClass
{
public:
    .
    .
    .
    SomeClass();   // Constructor
    ~SomeClass();   // Destructor
private:
    .
    .
    .
};
In this chapter and the next, we won't be using destructors; the kinds of classes we'll be writing have no need to perform special actions at the moment a class object is destroyed. In Chapter 17, we explore destructors in detail and describe the situations in which you need to use them.

 
< previous page page_868 next page >