C++ Initializer Lists

You may have worked on list-type variables in many programming languages to be used in the code. Lists are one of the few data structure variables used to store more than one value at a time. However, have you used the list to initialize some class data members while coding? The initializer lists are those data structures used in programming to initialize some data members of the specific class. If you haven’t done it so far and you are looking for something like this, then this article guide will give you a whole new experience. This article will demonstrate several simple examples to see the function of using initializer lists in C++. One should have a G++ compiler configured on their system if working on the Ubuntu 20.04 system. Begin by launching the “terminal” application found in the Ubuntu system.

Example 01

Start your first C++ example for the initializer list with the creation of a new C++ document. The even green “touch” word can be used here to generate a file named “initlist.cc”. You can find this file in the home folder by opening the file explorer from the side application bar on Ubuntu 20.04.  GNU Nano editor is here to help and launch the “initlist.cc” file. Your file will be opened in the Editor quickly within the shell:

The C++ code must contain the standard namespace and “io” stream within it, as we have done below. The “Test” class is initialized with two private data members, “a” and “b”.  The initializer list has been used with the public type constructor “Test()” to make a list of both data members “a” and “b”. The colon has been used to separate the constructor and the initializer list. Then, two user-defined functions, i.e., geta() and getb(), are initialized to get the values for both data members separately from the initializer list used with the constructor and return the result to the main function. The class ends here, and the main() function starts. Within the main() method, we have created an object “t” of class “test”. The object takes two parametric values of integer types, and the constructor will be called automatically to set the data member values with the initializer list.

The standard cout clauses are used after that to display both data member values on the shell using the object “t” to call the functions “geta()” and “getb()” within it. The main function closes here, and the code is ended. Save your code and run it in the shell:

We have compiled the previous code with the G++ compiler package. In execution, we have both the data member values separately on the shell:

Example 02

Within our second example, we will be using the initializer list for the initialization of some non-static constant type data members in C++ code. After the namespace, the class named “New” has been initialized with a private constant type integer variable “n”. The public type constructor function is used along with the initializer list to initialize a constant data member “n”. A new “get()” function on the integer return type has been used to return the data member “n” value to the main function. The main()function is creating an object n1 of class “New” while passing it a value “13” as an argument.

The constructor will be called, and the initializer list will set the value of data member “n”. The get() function has been called within the cout clause to display the set value of data members in the shell using the object “n1”. The main function and the programs end here:

We use the initializer list to initialize the value for some non-static constant data members in C++. The output for this code has been showing the constant value 13 on the shell:

Example 03

Users can also use the initializer list with the constructor to initialize some reference data members. Within the class “New”, we have used the reference variable “n” of integer type. The public type constructor has been utilizing the initializer list to initialize the reference data member with some value. The “get()” function is again utilized to get the value from an initializer list and return it to the terminal. Within the main function, we have initialized an integer “a” with value 13.

The class object “n1” has been passed with the variable “a”, and the constructor would be called to initialize the reference data member using the initializer list. The cout statement is getting the initialized reference data member value using the “get()” method with the help of an object “n1”. The variable “a” has been updated with the new value, i.e., 23. The cout statement is used once again to get the updated value from the get() function. The variable “a” has been bound with the reference variable “n”. So, every time we update the value, it will output the new one on the shell:

Compile the code first and execute it after that. It outputs the first value of the reference variable and then, the updated one on the shell. This is how the initializer list works for reference data members:

Example 04

The initializer list can also be used for the object initialization while working in more than one class in C++. So, we have used the Two classes, One and Two, in our newly created code. The first class named ”One” contains the integer variable “val”. It also includes the definition prototype of constructor function “One”, taking some integer value. The class has been closed, and it doesn’t have any default constructor in it. The constructor has been initialized outside of this class “a”, getting the integer type value from the main function directly. This value has been displayed using the cout statement using the variable “val” in it. Another class, “Two”, has been defined with a class “One” object, i.e., “a”. This class also contains the definition prototype of the constructor.

After this class, we have used its class constructor outside of it using the initializer list to get the value for an object “a” of class “One” directly. The cout statement has indicated that we are working in the class “Two” constructor. Within the main function, we have created the object of class “Two” and passed it a value 13 to its initializer list held with its constructor. This initializer list will call the object “a” of class “One” and then, pass the value to the class “One” constructor. The constructor will be executed, and the value will be saved into the variable “val” and displayed on the shell. The control came back to the class “Two” constructor, and the cout statement will be executed:

In return, we have got the following result. It outputs the variable “val” value on the shell along with the statement that it’s been running in the class “One” or “Two” constructor.

Conclusion

This article contains several examples for the use of initializer lists in C++ programs. We have seen the use of an initializer list to initialize simple data members, constant type data members, reference data members, and object initialization. We hope this article will work for you. Please check out more informative articles at LinuxHint.



from https://ift.tt/3IlP2PK

Post a Comment

0 Comments