What is a constructor initializer list and when we use
constructor initializer list?

Answer Posted / sachin mahajan

Main purpose of the contstuctor is to initialize the data
members with some valid values. This can be done in two ways
class MyClass{
int I,J;
public:
MyClass(int i,int j )
{
I=i;J=j;
}
};
Above the most common way to initialize data members .Other
way is
MyClass(int i,int j):I(i),J(j)
{
}
i(0),j(0) is the initialization list.

Constuctor Initialization list is used when we want to pass
some data to the constructor the parent class.
Below is the example:
class Parent
{
int I;
public:
Parent(int i)
{
I=i;
}
};
class Child:public Parent
{
int J;
public:
Child(int i,int j):Parent(i),J(j)
{

}

};

main()
{
Child c(1,2);
//now 1 is passed to parent and 2 is passed to Child
}

Is This Answer Correct ?    12 Yes 5 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the isa and hasa class relationships. How would you implement each?

724


What are features of c++?

742


What is a virtual destructor? Explain the use of it?

635


What is the difference between a definition and a declaration?

662


What are pointer-to-members in C++? Give their syntax.

726






Is java based off c++?

615


What is the difference between multiple and multilevel inheritance in c++?

718


find the two largest values among the 6 numbers using control structures : do-while,for,if else,nestedif- else ,while. one or two of them.

2092


What problems might the following macro bring to the application?

706


If a function doesn’t return a value, how do you declare the function?

705


Do you know about C++ 11 standard?

733


What is diamond problem in c++?

633


How long will it take to learn programming?

689


What is the auto keyword good for in c++?

721


Explain explicit container.

720