When you create an object, you need to initialize its data members. As you know, private and protected data members can be accessed outside the class by using public member functions of the class. C++ offers a special function, called constructor, which makes the initialization of an object. A constructor is a special function that is called every time you create an object.
A constructor is a public member function that has the same name as the name of the class. For example, if you want to create a constructor for Person class from the previous topics:
class Person { private: string firstName; string lastName; int yearOfBirth; };
Name of the constructor will be Person.
Default Constructor
A default construct is a constructor, which does not have any parameters. It is called once you declare an object. Here is an example of a default constructor for Person class:
Person::Person() { cout << "Default constructor is starting" << endl; }
Now we can declare an object of a person class:
Person person1;//declare a person
In above statement, Default constructor will be called and you will see the following output:
Default constructor is starting
Default constructor can be used to prepare some resources or for allocation of memory before you will start working with an object.
Parameterized Constructor
The constructor with parameters can be used to initialize data members of the object. To create a constructor with parameters you have to specify its parameters in the parentheses as we do to specify parameters of a function. This is an example of a constructor with three parameters for Person class:
Person::Person(string fName, string lName, int year) { cout << "Constructor with 3 parameters is starting" << endl; //set data members according to the parameters firstName = fName; lastName = lName; yearOfBirth = year; }
Now you can initialize objects of Person class once they are created:
//use constructor with three parameters Person person2("Smith", "James", 1992);
Notice that parentheses are used after object declaration. You are passing parameters to the constructor after declaration in the same way as you call a function with parameters. In this case, you don’t need to call constructor explicitly. We pass parameters right after we have declared an object.
Once this line of code will be executed, you will see the following output:
Constructor with 3 parameters is starting
We can add a simple print member function to check if constructor works properly:
void Person::print() { cout << "First name " << firstName << endl; cout << "Last name " << lastName << endl; cout << "Year of Birth " << yearOfBirth << endl; }
Now call print function after person2 was created:
person2.print();
You can see the following output:
Constructor with three parameters is starting
First name Smith
Last name James
Year of Birth 1992
Overloaded Constructor
We can also overload constructors. It means that you can create multiple constructors with different parameters. Here is an example of another constructor of Person class, which takes only two arguments:
Person::Person(string fName, int year) { firstName = fName; lastName = "Unknown"; yearOfBirth = year; }
As you can see, this constructor sets only two data members. The lastName is unknown. The value is set in this case by default to “unknown”. For such purposes, you can use default values for constructor parameters. For example, you want to set the default value for yearOfBirth to 1990. In this case, the declaration of constructor of person class with three parameters will look in the following way:
Person(string fName, string lName, int year = 1990);
Now you can create Person object only with two string parameters. In this case, year will be set to 1900:
Person person3("Green", "Alan"); person3.print();
You can see that year is set automatically to 1990:
Constructor with 3 parameters is starting
First name Green
Last name Alan
Year of Birth 1990