Constructors and Destructors
Constructors are class functions used to initialize an object. Whenever an object is created, a constructor is called. Conversely, a destructor is called whenever an object is destroyed. Constructors differ from normal member functions in that they have the same name as the class they belong to. They do not have a return type and, as mentioned previously, they are called automatically whenever an instance of the class they belong to is created.
This section will cover three different types of constructors:
- Default constructors
- Parameterized constructors
- Copy constructors
These types of constructors will be covered in order by creating a simple song track listing class that holds various information about a particular track.
A default constructor is a constructor that takes no parameters, or a constructor where all parameters have default values. Let's look at a very simple class...