MC logo

Construction and Destruction I (Answer)

  Practice Questions

  1. The constructors for class C are named C.

  2. A constructor is called whenever an object of the class is created. Here are the possibilities, not all of which we have discussed in class. An object of class C is created
    • By a declaration, like C x; or C x(args);.

    • When creating an object with new, as new C or new C(args).

    • The copy constructor (if any) is called when the system must make a copy of a C object, most often when passing by value or returning such an object.

    • When a temporary object is created by an expression of the form C(args).

    • When a temporary object is created as part of an automatic conversion.

  3. The destructor for class C is called ~C. (The ~ character is called a tilde.)

  4. The destructor for class C is called whenever an object of class C ceases to exist. For named variables and parameters of class C, this is at the closing }. For temporary objects, this is largely up to the compiler. Objects created by new are destroyed when they are deleted.

  5. A copy constructor for class C is a constructor which takes one argument which is a reference to C. It is called whenever the system needs to copy an object of type C, as described above.

  6. A default constructor is simply a constructor which takes no arguments, and is called whenever a construction is required and no arguments are provided.

<<Operator Overloading Question Construction and Destruction II>>