constructors are specialized class methods whose job is to instantiate objects of the class type with initial values of the data members. Constructors can have a variety of arguments passed to them, but no construcors have a return type, not even void. Constructors that take no parameters are called default constructors. Constructors that create one object of the class by copying the data from another object of the class is called a copy constructor. You can create other constructors as needed, for example, constructors that take one or more parameters whose values will be assigned to data members of the class as the initial values for the class member(s). Each class should have a default constructor and a copy constructor, and if you don't declare any constructors explicitly, the compiler will create default versions of a default constructor and copy constructor for you. However, it is better if you do it. You should be aware that if you do explicitly create a constructor, then the compiler won't create any default versions of either the default constructor or the copy constructor.
Likewise, every class must have a destructor as well. If you don't define it explicitly, the compiler will do it for you, but it's better if you do it. The primary job of the destructor is to release any dynamic memory that has been declared by the class methods, if it hasn't already been released by the time the destructor is called. However, other tasks can be assigned to the destructor as well. Like constructors, destructors have no return type, but unlike constructors, I've never seen a class with more than one destructor, though I don't know if there is rule against that or not.
Despite all this work, you will (almost) never explicitly call a constructor or a destructor within your code. The compiler will call them for you based on the code you write.
Below is an untested/uncompiled version of a simple Student class. It has a default constructor, a non-default constructor, a copy constructor, a destructor, and two data members. The default constructor initializes age to zero and gender to a default value of * within the body of the constructor. The prefered way, however, is to initialize members using an initialization list as in the non-default constructor. The copy constructor copies the value of age and gender in rhs into the new Student object. The destructor is just given an inane statement to show you that it is used, even though the code doesn't call it. In the body of main() I've declared three Student objects, Judy using the default constructor, Sally using the non-default constructor, and Mary using the copy constructor making Mary a copy of Sally (identical twin?). To prove that the cosntructors are called by the compiler eventhough they aren't called explicitly I've provided public accessor functions to demonstrate that the data members have been initialized as expected. Since I haven't provided any public mutators, the data members will never be changed throughout the course of the program. This may or may not be the desired state of affairs in any given program for any given member.
Code:
class Student
{
public:
Student();
Student(int, char);
Student(const Student &);
~Student();
int getAge() const;
char getGender() const;
private:
int age;
char gender;
};
Student::Student()
{
int = 0;
char = '*';
}
Student::Student(int i, char c) : age(i), gender(c)
{}
Student::Student(const Student & rhs)
{
age = rhs.age;
gender = rhs.gender;
}
~Student::Student {cout << "destructor called" << endl;}
int Student::getAge() const {return age;}
char Student::getGender() const {return gender;}
int main()
{
Student Judy();
Student Sally(18, 'f');
cout << Judy.getAge() << ' ' << Judy.getGender() << endl;
cout << Sally.getAge() << ' ' << Sally.getGender() << endl;
Student Mary(Sally);
cout << Mary.getAge() << ' ' << Mary.getGender();
}