Initializer list in C++
In C++, an initializer list is a powerful feature that allows developers to initialize class members and objects quickly and efficiently. The feature was introduced in C++11 and has become a popular way to initialize data members in classes and objects.
Syntax of Initializer List
The syntax of initializer list in C++ is straightforward. An initializer list is enclosed in curly braces and separated by commas. It can be used to initialize an array, class members, or object.
type object_name{value1, value2, ..., valueN};
Here, "type" is the data type of the object, "object_name" is the name of the object, and "value1, value2, ..., valueN" are the values to be assigned to the object.
Initializer lists can also be used to initialize an array of objects.
type array_name[] = {object1, object2, ..., objectN};
In this case, "type" is the data type of the objects in the array, "array_name" is the name of the array, and "object1, object2, ..., objectN" are the objects to be assigned to the array.
Example of Initializer List
Let's take an example to understand how the initializer list works. Suppose we have a class called "Person" that has two data members "name" and "age". We can use the initializer list to initialize these data members as follows:
#include <iostream>
#include <string>
class Person {
public:
std::string name;
int age;
// constructor with initializer list
Person(std::string n, int a) : name{n}, age{a} {}
};
int main() {
Person p{"John", 25};
std::cout<< "Name: " << p.name <<std::endl;
std::cout<< "Age: " <<p.age<<std::endl;
return 0;
}
Output:
When we run this program, it will output the following:
Name: John
Age: 25
Explanation
In this example, we have defined a class called "Person" with two data members "name" and "age". We have also defined a constructor for this class that takes two arguments "n" and "a" and initializes the data members "name" and "age" using the initializer list.
In the main function, we have created an object "p" of the class "Person" using the initializer list. We have assigned the value "John" to the data member "name" and the value "25" to the data member "age".
As we can see from the output, the data members "name" and "age" have been initialized using the initializer list.
Initializer Lists with Inheritance
Initializer lists can also be used with inheritance. Suppose we have a base class called "Shape" and two derived classes called "Circle" and "Rectangle". We can use the initializer list to initialize the data members of the base class and derived classes as follows:
#include <iostream>
class Shape {
public:
Shape(int x, int y) :m_x{x}, m_y{y} {}
protected:
int m_x;
int m_y;
};
class Circle : public Shape {
public:
Circle(int x, int y, int r) :Shape{x, y}, m_radius{r} {}
void print() {
std::cout<< "Circle(" <<m_x<< ", " <<m_y<< ", " <<m_radius<< ")" <<std::endl;
}
private:
int m_radius;
};
class Rectangle : public Shape {
public:
Rectangle(int x, int y, int w, int h) :Shape{x, y}, m_width{w}, m_height{h} {}
void print() {
std::cout<< "Rectangle(" <<m_x<< ", " <<m_y<< ", " <<m_width<< ", " <<m_height<< ")" <<std::endl;
}
private:
int m_width;
int m_height;
};
int main() {
Circle c{10, 20, 30};
Rectangle r{40, 50, 60, 70};
c.print();
r.print();
return 0;
}
Output:
When we run this program, it will output the following:
Circle(10, 20, 30)
Rectangle(40, 50, 60, 70)
Explanation
In this example, we have defined a base class called "Shape" with two data members "m_x" and "m_y". We have also defined two derived classes called "Circle" and "Rectangle" that inherit from the base class "Shape".
We have used the initializer list to initialize the data members of the base class and derived classes in the constructors of the derived classes.
In the main function, we have created two objects "c" and "r" of the classes "Circle" and "Rectangle" respectively. We have initialized the data members of these objects using the initializer list.
As we can see from the output, the data members of the base class and derived classes have been initialized using the initializer list.
Conclusion
In summary, initializer list is a powerful feature in C++ that allows developers to initialize class members and objects quickly and efficiently. It is easy to use and can be used with inheritance. With initializer list, C++ developers can write cleaner and more concise code.