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
Where the memory to the static variables is allocated?
How many characters are recognized by ANSI C++?
what is a reference variable in C++?
Can we use this pointer in a class specific, operator-overloading function for new operator?
Can union be self referenced?
What is static in c++?
What is the use of cmath in c++?
Why iomanip is used in c++?
Why cstdlib is used in c++?
Write a code/algo to find the frequency of each element in an array?
What is the disadvantage of using a macro?
Explain explicit container.
What are files in c++?
Write about an iterator class?
I want explanation for this assignment: how to connect mysql database using c/c++,please explain this detailly?