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

What is the precedence when there is a global variable and a local variable in the program with the same name?

645


Explain terminate() function?

598


Should I learn c++ c?

625


What is the difference between function overloading and operator overloading?

579


What is the main purpose of c++?

555






What are the unique features of C++.

581


What is the difference between reference and pointer?

618


What is the prototype of printf function?

665


Can I learn c++ without c?

609


What are mutator methods in c++?

655


Which is best ide for c++?

574


What is the full form of dos?

573


What are the important differences between c++ and java?

611


What are the various operations performed on stack?

635


What are c++ templates used for?

625