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 C++ objects?

0 Answers  


What is vectorial capacity?

0 Answers  


Write a program which uses functions like strcmp(), strcpy()? etc

0 Answers  


What do you mean by a template?

0 Answers  


Is arr and &arr are same expression for an array?

0 Answers  






class X { public: int x; static void f(int z); }; void X::f(int y) {x=y;} What is the error in the sample code above? a) The class X does not have any protected members. b) The static member function f() accesses the non-static z. c) The static member function f() accesses the non-static x. d) The member function f() must return a value. e) The class X does not have any private members.

2 Answers   Quark,


What is main function in c++ with example?

0 Answers  


Write a corrected statement in c++ so that the statement will work properly. if (x = y) x = 2*z;

2 Answers  


Define copy constructor.

0 Answers  


Implement strncpy

3 Answers  


How do I run c++?

0 Answers  


what is COPY CONSTRUCTOR and what is it used for?

0 Answers  


Categories