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
Define the process of handling in case of destructor failure?
What is friend class in c++ with example?
Carry out conversion of one object of user-defined type to another?
What is the basic structure of a c++ program?
What is vector processing?
What is conditions when using boolean operators?
Can I create my own functions in c++?
How do I get good at c++ programming?
When should I use unitbuf flag?
What is wrapper class in c++?
What is a singleton class c++?
What is operator overloading in c++ example?
What is #include iostream h in c++?
How the delete operator differs from the delete[]operator?
What is the real purpose of class – to export data?