CIS 22B - Notes for Monday 11/12

Announcements and Reminders

Class Examples

Another look at  The Card and Deck classes

More Class Concepts

enums and classes

Nested classes

Constructors and Destructors

Notes from CIS27

Constructors are special class member functions that are used to create class objects.  They execute automatically when an instance of a class is created.  Constructors are used to initialize class data members.  They are also used to allocate memory for a class.  A constructor’s name is the same as the class name. 

Destructors are functions that also execute automatically when the object goes out of scope (existence). Destructors, too, have a special name.  It is the class name preceded by a ~ (tilde).  Destructors are used to release dynamically allocated memory and to perform other "cleanup" activities.


class xyz
    xyz();         // constructor prototype
    ~xyz();        // destructor prototype

xyz::xyz()         // constructor definition

xyz::~xyz()        // destructor definition
int main(void)
  xyz thing;       // the construction is called now
  return 0;        // the destructor is called now


  • Constructors and destructors are usually placed in the public part of class definition. 

  • Both the constructor and the destructor have no return type, nor a return statement.

  • Destructors cannot have arguments.  Constructors can.  They can have several arguments, including default arguments.

  • Constructors are not usually called explicitly.  They are called automatically.  Destructors are not usually called explicitly.

  • A class may have several constructors.  If a class has multiple constructors, the argument list, including default arguments, must be unique.

  • Ctor and Dtor are abbreviations for constructor and destructor.

  • Every object must have a constructor.  If you do not provide one, the compiler will create one for you.  This constructor is a default constructor.  Default constructor also refers to a constructor without arguments.

  • Destructors are automatically called when a class object is deleted.


Example 4-1 Constructor/Destructor - circle class
Example 4-2 Constructor/Destructor - scope
Example 4-3 Constructor/Destructor - person class
Example 4-4 Constructor/Destructor - card and deck (1)
Example 4-5 Constructor/Destructor - card and deck (2)



Lab Exercise #8

Put your name, the compiler and operating system used, and Lab Exercise #8 in a comment at the top of your program. Email your source code.

Write a program with a Square class containing two data members, side (type int) and color (type string).  Write a constructor, only one, and member function to be used with the following main() function.  Note, your constructor shoiuld be able to handle the 3 instantiations of the Square class.

int main()
    Square object1;
    Square object2(6,"blue");
    Square object3(10,"yellow");


Program output:

I am a green Square with an area of 16
I am a blue Square with an area of 36
I am a yellow Square with an area of 100