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

Answers were Sorted based on User's Feedback



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

Answer / deep

There is couple special case when you need mandatory
constructor initialization list.

a. To initialise a const
b. To initialise a reference

Is This Answer Correct ?    12 Yes 1 No

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

Answer / 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

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

Answer / mahesh

The main use of constructor is to initialize object.

Is This Answer Correct ?    1 Yes 1 No

Post New Answer

More C++ General Interview Questions

Can you be bale to identify between straight- through and cross- over cable wiring? And in what case do you use straight- through and cross-over?

0 Answers  


Why c++ is not a pure oop language?

0 Answers  


Difference between shift left and shift right?

1 Answers   Symphony,


If I is an integer variable, which is faster ++i or i++?

0 Answers  


What are C++ inline functions?

1 Answers  






What is the best c++ ide?

0 Answers  


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

0 Answers   Honeywell, Zomato,


Why is that unsafe to deal locate the memory using free( ) if it has been allocated using new?

0 Answers  


Are vectors faster than arrays?

0 Answers  


How many different levels of pointers are there?

0 Answers   Genpact,


If dog is a friend of boy and boy is a friend of house, is dog a friend of house?

0 Answers  


What is c++ stringstream?

0 Answers  


Categories