Close
Close

Initialization List in C++


In the previous chapter, we learned about how classes and their objects can be created and the different ways their members can be accessed. We also saw how data members are initialized in the constructor of any class as shown below.

class Rectangle
{
    int length;
    int breadth;
    public:
        Rectangle()
        {
            length = 7;
            breadth = 4;
        }
};

In the above constructor, we assigned the values 7 and 4 to the data members length and breadth respectively. Note that, here we assigned the values to these variables, not initialized.

In the case of constructors having parameters, we can directly initialize our data members using initialization lists.

Using initialization list, we can write the above code as follows.

class Rectangle
{
    int length;
    int breadth;
    public:
        Rectangle() : length(7), breadth(4) // initializing data members
        {
            // no need to assign anything here
        }
};

An initializer list starts after the constructor name and its parameters and begins with a colon ( : ) followed by the list of variables which are to be initialized separated by a comma with their values in curly brackets.

Let's see an example for the above code.

#include <iostream>

using namespace std;

class Rectangle
{
	int length;
	int breadth;
	public:
		Rectangle() : length(7), breadth(4)
		{

		}
		int printArea()
		{
			return length * breadth;
		}
};

int main()
{
	Rectangle rt;
	cout << rt.printArea() << endl;
	return 0;
}
Output
28

Here we initialized the variables length and breadth directly using an initializer list in which we length and breadth were initialized to 7 and 4 respectively.
The rest of the code is the same.

Let's see another example of initialization list in case of parameterized constructor.

#include <iostream>

using namespace std;

class Rectangle
{
	int length;
	int breadth;
	public:
		Rectangle( int l, int b ) : length(l), breadth(b)
		{

		}
		int printArea()
		{
			return length * breadth;
		}
};

int main()
{
	Rectangle rt( 7, 4 );
	cout << rt.printArea() << endl;
	return 0;
}
Output
28

In this example, the initializer list is directly initializing the variables length and breadth with the values of l and b which are 7 and 4 respectively.

This is the same as the following code with the only difference that in the above example, we are directly initializing the variables while in the following code, the variables are being assigned the parameterized values.

class Rectangle
{
    int length;
    int breadth;
    public:
        Rectangle( int l, int b )
        {
            length = l;
            breadth = b;
        }
};

Need for Initialization List

Though we can use initialization list anytime as we did in the above examples, but there are certain cases where we have to use initialization list otherwise the code won't work.

If we declare any variable as const, then that variable can be initialized, but not assigned.

Any variable declared with const keyword before its data type is a const variable.

To initialize a const variable, we use initialization list. Since we are not allowed to assign any value to a const variable, so we cannot assign any value in the body of the constructor. So, this is the case where we need initialization list.

Following is an example initializing a const data member with initialization list.

#include <iostream>

using namespace std;

class Rectangle
{
	const int length;
	const int breadth;
	public:
		Rectangle( int l, int b ) : length(l), breadth(b)
		{

		}
		int printArea()
		{
			return length * breadth;
		}
};

int main()
{
	Rectangle rt( 7, 4 );
	cout << rt.printArea() << endl;
	return 0;
}
Output
28

In this example, the variables length and breadth are declared as constant and thus cannot be assigned any value in the body of the constructor. So, we initialized them in the initializer list.

If we try to assign values to a const variable, we will get an error. We will learn more about the const keyword in a later chapter.

One more case where we need initializer list is when we want to initialize base class members by creating an object of the subclass. We will see this in the next chapter.

There may be some other cases too where we would need initializer list but we can always use it as an alternative even when it's not necessary.

To learn from simple videos, you can always look at our C++ video course on CodesDope Pro. It has over 750 practice questions and over 200 solved examples.

Practice puts brain in your muscles.
-Sam Snead


Ask Yours
Post Yours
Doubt? Ask question