What is a constructor initializer list and when we use
constructor initializer list?
Answers were Sorted based on User's Feedback
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 |
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 |
Answer / mahesh
The main use of constructor is to initialize object.
| Is This Answer Correct ? | 1 Yes | 1 No |
write a programming using for loop in c++ to generate diamond trangel?
How do I write a c++ program?
What is the exit function in c++?
What does ios :: app do in c++?
what is data Abstraction? and give example
147 Answers Aaditya Info Solutions, American Express, CMS, College School Exams Tests, Data Entry Operator, First Advantage, Google, HCL, IBM, Infosys, Microsoft, Mind Links, NIIT, Oracle, Pact, QBit Systems, TCS, WAYA, Wipro,
Does c++ support exception handling?
What does new do in c++?
Why pure virtual functions are used if they don't have implementation / When does a pure virtual function become useful?
Why is the function main() special?
Does there exist any other function which can be used to convert an integer or a float to a string?
Name the implicit member functions of a class.
Explain 'this' pointer and what would happen if a pointer is deleted twice?