Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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 ifstream c++?

1028


Do you know what are the new features that iso/ansi c++ has added to original c++ specifications?

1116


What is difference between array and vector in c++?

1025


What can I safely assume about the initial values of variables which are not explicitly initialized?

1053


What is a stack? How it can be implemented?

1167


What is difference between rand () and srand ()?

1079


Will a recursive function without an end condition every quit, in practice a) Compiler-Specific (Some can convert to an infinite loop) b) No c) Yes

1071


What does the following do: for(;;) ; a) Illegal b) Loops forever c) Ignored by compiler...not illegal

1165


What is dynamic and static typing?

1180


How many namespaces are there in c++?

1071


What are the effects after calling the delete this operator ?

1050


What is the advantage of c++ over c?

1016


Discuss the effects occur, after an exception thrown by a member function is unspecified by an exception specification?

1070


What are the benefits of oop in c++?

1220


Can a Structure contain a Pointer to itself?

1131