what is the order of initialization for data?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What does it mean to declare a member variable as static?

582


How do you establish an is-a relationship?

600


Why do we need function?

576


Give an example of run-time polymorphism/virtual functions.

539


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

610






A prime number is a number which is divisible only by itself and 1. Examples of the first few primes are 2, 3, 5, 7, 11. Consider writing a program which can generate prime numbers for you. Your program should read in and set a maximum prime to generate and a minimum number to start with when looking for primes. This program should be able to perform the following tasks: 1. Read the maximum number from user (keyboard input) to look for primes. The program should not return any primes greater than this number. 2. Read the minimum number from user (keyboard input) to look for primes. The program should not return any primes less than this number. 3. Generate and print out every prime number between the maximum prime and minimum number specified by the user.

1717


How should runtime errors be handled in c++?

582


What is std :: endl?

566


What are move semantics?

647


What parameter does the constructor to an ofstream object take?

584


How can you link a c++ program to c functions?

596


How would you use the functions sin(), pow(), sqrt()?

716


Is string data type in c++?

559


Can manipulators fall in love?

546


What are the three forms of cin.get() and what are their differences?

603