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

What is the disadvantage of using a macro?

0 Answers  


Which uses less memory? a) struct astruct { int x; float y; int v; }; b) union aunion { int x; float v; }; c) char array[10];

4 Answers   Quark,


Does c++ cost money?

0 Answers  


Is the declaration of a class its interface or its implementation?

0 Answers  


What does extern mean in a function declaration in c++?

0 Answers  






class X { private: int a; protected: X(){cout<<"X constructor was called"<<endl;} ~X(){cout<<"X destructor was called"<<endl} }; Referring to the code above, which one of the following statements regarding "X" is TRUE? a) X is an abstract class. b) Only subclasses of X may create X objects. c) Instances of X cannot be created. d) X objects can only be created using the default copy constructor. e) Only friends can create instances of X objects.

2 Answers   Quark,


How do I open binary files?

1 Answers  


What is the difference between strcpy() and strncpy()?

0 Answers  


What and all can a compiler provides by default?

3 Answers   Accenture, HP,


What are the 2 main types of data structures?

0 Answers  


Define vptr.

0 Answers  


Why do we need constructors in c++?

0 Answers  


Categories