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 how to initialize a const member data.
How we can differentiate between a pre and post increment operators during overloading?
Explain object slicing in c++?
Is java made in c++?
Mention the ways in which parameterized can be invoked.
How would you obtain segment and offset addresses from a far address of a memory location?
How many types of modularization are there in c++?
what is a class? Explain with an example.
Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the maximum number of concurrent threads that the InnoDB plug-in can create
How would perform Pattern Matching in C++?
What is == in programming?
What's the best free c++ profiler for windows?
What are c++ stream classes?
Difference between pass by value and pass by reference?
What is code reusability in c++?