๐Ÿงพ Unit 5: Constructor & Destructor [3 Hrs]


๐Ÿ“˜ โœ๏ธ Theoretical Explanation

๐Ÿ”น Constructor

A constructor is a special member function that is automatically called when an object is created. It is used to initialize objects.

๐Ÿงฉ Characteristics of Constructors:

๐Ÿ”น Types of Constructors:

  1. Default Constructor โ€“ A default constructor is a constructor that takes no parameters. It is automatically invoked when an object is created. Useful for setting default values.
  2. Parameterized Constructor โ€“ A parameterized constructor allows values to be passed during object creation. This is useful to initialize an object with specific values.
  3. Copy Constructor โ€“ A copy constructor creates a new object by copying an existing one. It is useful when you want to make a clone of an object.

๐Ÿ”น Destructor

A destructor is a special member function that is automatically called when an object is destroyed. It is used to release resources. It's useful for releasing memory or closing files.

It deallocates all the memory previously used by the object of the class so that there will be no memory leaks.

๐Ÿงฉ Characteristics of Destructors: