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
Explain the concept of friend function in c++?
What are the advantages of using typedef in a program?
How do you define a class in c++?
Can you sort a set c++?
How does list r; differs from list r();?
What is the best c++ compiler for windows 10?
Define private, protected and public access control.
What is a dangling pointer in c++?
What is switch case in c++ syntax?
What are c++ templates used for?
What is null and void pointer?
How do you invoke a base member function from a derived class in which you have not overridden that function?
What is stream and its types in c++?
What are maps in c++?
What is the meaning of c++?