Link to home
Start Free TrialLog in
Avatar of dinorama
dinorama

asked on

<<Inheritance- NEW!!>>

Hello there,

I just need help on this question:

Here is the question: I just scanned the question and posted it online. Please view it. too much typing, my hands are hurting.. :)

Here is the link to the question:

http://img.photobucket.com/albums/v218/nexus-6/question.jpg


ok. now questions:

1. In the main function, C obj1(1,2,3,4) was instantiated. ON this instantiation, according to the constructor rulez, Class A constructor be called first right?? If it is called, what value of x, would it substitute or initialize a to?? How does this question work anyways??

2. Cuz, i was able to get the correct output, if i CALLED, THE CLASS B constructor first, and then i went upwards step by step. But, is this correct?? Is not Class A constructor called first??

HELP PLZ....
thanks
dino.
Avatar of jhshukla
jhshukla
Flag of United States of America image

1. In the main function, C obj1(1,2,3,4) was instantiated. ON this instantiation, according to the constructor rulez, Class A constructor be called first right?? If it is called, what value of x, would it substitute or initialize a to?? How does this question work anyways??
ctor for C calls ctor for B with args (2,3,4)
C(x,y,z,q) ==> C(1,2,3,4)
B(y,z,q)    ==> B(2,3,4)

ctor for B calls ctor for A with arg (4)
B(x,y,z)    ==> B(2,3,4)
A(z)         ==> A(4)

so at the end you have: a=4, b=2, c=3, d=1.

2. Cuz, i was able to get the correct output, if i CALLED, THE CLASS B constructor first, and then i went upwards step by step. But, is this correct?? Is not Class A constructor called first??
I am not exactly sure of this or wording in my comment above but I believe that the compiler inserts some code before the 'body' of the function and that's where the calls to ctors for parent class are located. the reasult is that you don't call the constructors for parent class, the compiler does. better make sure with someone more knowledgeable.
Avatar of dinorama
dinorama

ASKER

?? i didnt understand what u said for # 2. ? jhshukla.
yo avizit! where r u guy?? or can somebody hear my cry! :) lol
SOLUTION
Avatar of jhshukla
jhshukla
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
now, that sorta helps.. jhshukla. thanks. don't know if its that damn important... or not.. :)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for confirming that abhijit.

dinorama: i don't that is that danm important for passing the 'other' parts of the exam but it will definitely help if you have function-tracing questions.
Abhijit dude, But, there is a question in my textbook says:

State the order of the constructor and destructor calls:

class B: A {
public:
C cobj;
D dobj;
B();
~B();
};

class E: public B {
F fobj;
public:
E();
~E();
};

And the answer to that is: (for the constructors)

A
C
D
B
F
E


Should it not be CONSTRUCTOR EXECUTION? RATHER THAN CALLS?

So, should the question be specifically "Constructor Execution" ??
SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>Should it not be CONSTRUCTOR EXECUTION? RATHER THAN CALLS?

Either way, the answer would still be the same.
Constructor execution and constructor calls come in the same order.
>> The code that exist between the brace {} is the constructor.
>> What comes before, is called the initialize list, and it is not part of the constructor.
different terminology. would I be misleading if I prefer to call the combined code for initializer list & the code between {} as the constructor; and the code between {} as the 'body' of the function?

>> So the order of construction is: A B C
so how does the compiled code look like? (if not like the one I posted @ 12/12/2004 08:48PM EST).
>>different terminology. would I be misleading if I prefer to call the combined code for initializer list & the code between {} as the constructor;

Yes.

>>so how does the compiled code look like? (if not like the one I posted @ 12/12/2004 08:48PM EST).

The compiler code will first construct A, then Consruct B, and then construct C.
So the compiler code will be in the same order.
yes, it will finish constructing A first but start constructing C first. am i right or wrong?
>>yes, it will finish constructing A first but start constructing C first. am i right or wrong?

IAW C++ standards, that would be wrong.
It can not start to construct C, until the base class and data members are constructed.

See section 12.6.2, paragraph 5, of the C++ standards {ISO/IEC 14882:1998(e)}

One can say you can't build a brick house without first creating the bricks.
What you're saying, is that the manufactor who's building the bricks, are indirectly building the brick house.

While that *may* be true for a brick house, that is *not* the way the C++ standard looks at it for the order of constructors.
>> What you're saying, is that the manufactor who's building the bricks, are indirectly building the brick house
what I am saying is that manufacturer who's building the house, orders the bricks to be made. brick manufacturer does not know who the order is from, and if the order is from house manufacturer, he is doing a part in manufacturing of the house.

I'll look at Sec. 12.6.2 though.
>>what I am saying is that manufacturer who's building the house, orders the bricks to be made. brick manufacturer does not know who the order is from,
>>and if the order is from house manufacturer, he is doing a part in manufacturing of the house.

That's why I stated
"While that *may* be true for a brick house, that is *not* the way the C++ standard looks at it for the order of constructors."
I looked at it and figured that [again] the only difference between our comments is of terminology.

what does construction mean anyways? allocating some space and then filling it up with some data. I dont think the program would first allocate space for base class and then for the derived class. from my point of view, construction of the derived class has begun as soon as space is allocated.
>>what does construction mean anyways? allocating some space and then filling it up with some data. I dont think the program would first allocate space for
>>base class and then for the derived class. from my point of view, construction of the derived class has begun as soon as space is allocated.

I'm sorry, but that is not the point of view of the standard, nor is it the point of view of many C++ Books.
See "The C++ Programming Language Special Edition" by Bjarne Stroustrup (The creator of C++)
Section 10.4.6
The members' constructors are called before the body of the containing class. -----
The member destructors are called in the reverse order of construction.

If you want to play around with the meaning of constructor and what the compiler is doing, then you can view this any way you want.

However, in C++ terms, constructors are constructed in the order I've posted.
That's what is stated in the standard, and that is what is stated by the experts.
>> The members' constructors are called before the body of the containing class. -----
>> The member destructors are called in the reverse order of construction.
that is for members, not for base/parent classes

>> constructors are constructed in the order I've posted
agreed.