Q:

What is the initializer list in C++?

0

What is the initializer list in C++?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

The initializer list is used to initialize data members of the class. The syntax of the initializer list begins with a colon(:) and then each variable along with its value separated by a comma.

Note: The initializer list does not end in a semicolon.

Let see an example to understand the initializer list  in C++,

In the below code, the member variable value is initialized by the initializer list.

#include<iostream>
using namespace std;
class Demo
{
public:
    // initialization List
    Demo(int value):value(value)
    {
        cout << "Value is " << value;
    }
private:
    int value;
};
int main()
{
    Demo obj(10);
    
    return 0;
}

Output: Value is 10

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Interview Questions For Experienced

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
When do we use the Initializer List in C++?... >>
<< Can a constructor throw an exception? How to handl...