Announcements and Reminders
More Class Examples
More Class Concepts
Constructors and DestructorsNotes 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.
Lab Exercise #8
Put your name, the compiler used, and Lab Exercise #8 in a comment at the top of your program. Email your source code. This lab exercise is due Friday, March 3rd at 1:30 pm. You may email your lab solution prior to that.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.
I am a red square with area 1
I am a blue square with area 25
I am a green square with area 100