what is the order of initialization for data?

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What do you mean by early binding?

599


What is the use of cmath in c++?

589


Define anonymous class.

623


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

580


What do you mean by translation unit in c++?

669






Does c++ have finally?

568


What is token c++?

576


What is an overflow error?

617


Is recursion allowed in inline functions?

607


Why do we use vector in c++?

600


What is endl?

631


When is the destructor called?

604


What is const in c++?

596


What is #include ctype h in c++?

665


What are guid? Why does com need guids?

570