what is the order of initialization for data?

Answers were Sorted based on User's Feedback



what is the order of initialization for data?..

Answer / avi

Data members of a class are initialized in the order they
are declared in class. in case class is derived from some
base class then the base class will be initialized first
followed by the derived class.

Is This Answer Correct ?    14 Yes 3 No

what is the order of initialization for data?..

Answer / sanjay makwana, puna

In order of class declared memeber variables.

Is This Answer Correct ?    11 Yes 4 No

what is the order of initialization for data?..

Answer / nilay

all the static memebers are created at the time of class
definition and rest all non static memebers are created
when object is created of that class. member functions are
also create in memory when class is defined. (However only
single copy of static data members and functions is created)

Is This Answer Correct ?    7 Yes 1 No

what is the order of initialization for data?..

Answer / vinay bondade

It is same as the order of declaration in the class.

Is This Answer Correct ?    14 Yes 9 No

what is the order of initialization for data?..

Answer / kasi

1) Brief details on initialization:

To perform true initialization (not assignments) for a
class data member, C++ provides extened syntax for
constructor function.

Syntax:
class A
{
int ssn;
double sal;
public:
A(int j,double k):sal(k),ssn(j) //initialization
{
//assignement
}
}

Therefore constructor function has 2 parts they are
- initialization
- Assignment
Note - initialization will be excuted first and then
assignment

2) When is the initialization list mandatory?
- If the instance variable is constant
- If the class contains reference variable

3) coming to the posted question: order of member
initialization will be done in 2 ways by the compiler.

A) Declaration order
B) The order of members in the intialization list.

For the above example ssn is initialized first then sal.

Is This Answer Correct ?    4 Yes 1 No

what is the order of initialization for data?..

Answer / sachin magdum

Kasi, I am not agreed with you, in your case sal will be initialized before ssn.

Debug this program and see,


class A
{
int i ;
public :
A (int _i)
{
i = _i;
}
};

class B
{
A a;
A b;
A c;

public :
B(int _a, int _b, int _c) : a(_a), c(_c), b(_b)
{}
};

int main(int argc, char* argv[])
{
B b(1,2,3);

return 0;
}

Here the initialization order is first 'a' then 'c' and then 'b' and not a, b, c.

However, if you have base class then the case is little different


class A
{
int i ;
public :
A (int _i)
{
i = _i;
}
};

class B : public A
{
A a;
A b;
A c;

public :
B(int _a, int _b, int _c, int _base) : a(_a), c(_c), b(_b), A(_base)
{}
};

int main(int argc, char* argv[])
{
B b(1,2,3,4);

return 0;
}

In this case first 'A' that is base class of B, then 'a' then 'c' then 'b' will be initialized.

If you have multiple base classes, then they will be initialized in the order of their declaration, and not in the order how they are initialized.

class A
{
int i ;
public :
A (int _i)
{
i = _i;
}
};

class C
{
int i ;
public :
C (int _i)
{
i = _i;
}
};


class B : public A, public C
{
A a;
A b;
A c;

public :
B(int _a, int _b, int _c, int _baseA, int _baseC) : a(_a), C (_baseC), c(_c), b(_b), A(_baseA)
{}
};

int main(int argc, char* argv[])
{
B b(1,2,3,4,5);

return 0;
}

In this case first 'A' then 'C' then 'a' then 'c' and then 'b' will be initialized.

Is This Answer Correct ?    3 Yes 1 No

what is the order of initialization for data?..

Answer / g. maheshwari

The member and base classes are initialized in the order they appear in the class definition, not the order they appear in initialization list.

Is This Answer Correct ?    0 Yes 0 No

what is the order of initialization for data?..

Answer / karthik

there is no need for ordering a initialization of data.It
depends upon the program

Is This Answer Correct ?    4 Yes 8 No

what is the order of initialization for data?..

Answer / mithali

I think initialization doesn't have any order.we r just
initializing the variables to some value.

Is This Answer Correct ?    6 Yes 15 No

what is the order of initialization for data?..

Answer / rejeesh r

Class is a connection of objects.

Is This Answer Correct ?    1 Yes 19 No

Post New Answer

More C++ General Interview Questions

Is c++ double?

0 Answers  


string somestring ; Which of the following choices will convert a standard C++ string object "somestring" to a C string? a) Copy.somestring () ; b) somestring.c_str () c) &somestring [1] d) std::cstring (somestring) e) (char *) somestring

1 Answers   Quark,


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,


Write about the retrieval of n number of objects during the process of delete[]p?

0 Answers  


What is a c++ vector?

0 Answers  






What gives the current position of the put pointer?

0 Answers  


What is the difference between structure and class?

0 Answers  


How would perform Pattern Matching in C++?

0 Answers   Genpact,


What is the best c++ compiler for windows 10?

0 Answers  


How many standards of c++ are there?

0 Answers  


What are the various situations where a copy constructor is invoked?

0 Answers  


Can char be a number c++?

0 Answers  


Categories