Run-time polymorphism

2 views
Skip to first unread message

Nilu

unread,
Oct 2, 2010, 3:34:35 AM10/2/10
to nextgen_engg
Consider the following code snippet :

#include <iostream>

class Base
{ public :
Base()
{ someMethod(); }

virtual ~Base(){}

virtual void someMethod()
{ std::cout << "\nBase"; }
};

class Derived : public Base
{ public :
Derived()
{ someMethod(); }

~Derived(){}

void someMethod()
{ std::cout << "\nDerived"; }
};

int main(void)
{ Base* b = new Derived;
delete b;

return 0;
}

1. What is the output?
2. Explain(this is more important).

Mickey

unread,
Oct 2, 2010, 8:10:37 AM10/2/10
to nextgen_engg

Base
Derived

Am I missing something anywhere?
First the base class is constructed and then the derived class... @
new Derived.

Regards,
Jyoti

skarthi...@gmail.com

unread,
Oct 2, 2010, 10:49:06 PM10/2/10
to nextgen_engg
Hi all,

>
> 1. What is the output?
<karthikselvan> I got the output like this
Base
Derived
> 2. Explain(this is more important).

Now you need lot of patience to understand what I am going to say. I
used Intel system programmers pdf file, assembly language, reverse
engineering and C++ concepts.

1) I used cygwin in windows OS. First I used compile only option to
generate assemly language. Here is the command.

g++ -S inheri.cpp (You can use g++ --help to find all the options. You
can play with all the options.

I got the output file inheri.S
This file contains all symbols and text section. You guys studied
assembly language. You should be able to use Intel system programmers
pdf files from Intel website to map the program to assembly language.
(Here is the link for intel programmers manual.
http://www.intel.com/design/pentiumii/manuals/243192.htm You can find
using google also.

2) Anything present in double quotion "" in cout method is treated
ascii string so I searched for Base in inheri.s

3) It is right under the label like this in my .s file.

LC1:
.ascii "\nBase"
.section .text$_ZN4Base10SomeMethodEV,"x"
.linkonce discard
.align 2

(Note you don't need to be a compiler writer but if you know g++
compiler you can understaand more about this section. I can interpret
only first two line i.e. "\nBase" ascii string is present in
__ZN4Base10SomeMethodEV text section. Programmers calls this as method
or functions.
Now start the trace because you know which text (a.k.a functions,
methods, etc) which contains the ASCII string.

__ZN4Base10SomeMethodEV:
pushl %ebp
movl %esp, %ebp
( current function is getting pushed in to the stack pointer and
program counter (PC) is updated to call __ZN4Base10SomeMethodEV.)
...
leave
ret
(Read Intel system programmers guide to understand these two
instructions. In layman term, function is returning to the caller.)
4) (__ZN4Base10SomeMethodEv is called from __ZN4BaseC2Ev like this.)
__ZN4BaseC2Ev:
pushl %ebp
movl %esp, %ebp
...
call __ZN4Base10SomeMethodEv
leave
ret
(As you see, Base class someMethod() called from Base class context.

5) (__ZN4BaseC2Ev is called from derived call context.)

__ZN7DerivedEv:
pushl %ebp
movl %esp,%ebp
...
call __ZN4BaseC2Ev
call __ZN&Derived10SomeMethodEv
Jmp L18

(Note Base class constructor is called first which in turn calls
SomeMethod function present in the base class. After that only derived
class someMethod is getting called. Still we have to go one more in
the stack. ;-).

6) _main:
pushl %ebp
movl %esp, %bp
...
call __alloca
...
call __ZN7DerivedC1Ev
...
jmp L12


7) Now youself can work it out how the functions are getting called in
order. Memory is allocated using a library function called ___alloca.
After that derived class is called. With in dervied class context Base
class is called first. Base class SomeMethod() is getting executed.
After that Derived class someMethod() is getting excecuted. This works
as per the C++ design.

8) You guys should try learn objdump command also.

Thanks
Karthikselvan

skarthi...@gmail.com

unread,
Oct 2, 2010, 10:53:41 PM10/2/10
to nextgen_engg
I am correct my grammer errors. No big change.

On Oct 2, 10:49 pm, "skarthiksel...@gmail.com"
<skarthiksel...@gmail.com> wrote:
> Hi all,
>
>
>
> > 1. What is the output?
>
> <karthikselvan> I got the output like this
>  Base
>  Derived
>
> > 2. Explain(this is more important).
>
> Now you need lot of patience to understand what I am going to say. I
> used Intel system programmers pdf file, assembly language, reverse
> engineering and C++ concepts.
>
> 1) I used cygwin in windows OS. First I used compile only option to
> generate assemly language. Here is the command.
>
> g++ -S inheri.cpp (You can use g++ --help to find all the options. You
> can play with all the options.
>
> I got the output file inheri.S
> This file contains all symbols and text section. You guys studied
> assembly language. You should be able to use Intel system programmers
> pdf files from Intel website to map the program to assembly language.
> (Here is the link for intel programmers manual.http://www.intel.com/design/pentiumii/manuals/243192.htm. You can find
> using google also.
>
> 2) Anything present in double quotion "" in cout method is treated as an
> ascii string so I searched for "\nBase" in inheri.s
>
> 3) It is right under the label like this in my .s file.
>
> LC1:
>    .ascii "\nBase"
>    .section .text$_ZN4Base10SomeMethodEV,"x"
>    .linkonce discard
>    .align 2
>
> (Note you don't need to be a compiler writer but if you know g++
> compiler you can understaand more about this section. I can interpret
> only first two lines i.e. "\nBase" ascii string is present in
> __ZN4Base10SomeMethodEV text section. Programmers call this as method
> or functions.
> Now start the trace because you know which text (a.k.a functions,
> methods, etc) contains the ASCII string.
> 8) You guys should try to learn objdump command also.
>
> Thanks
> Karthikselvan

Nilu

unread,
Oct 7, 2010, 11:43:10 AM10/7/10
to nextgen_engg
The purpose of asking this question is to confuse, and then understand
how vtbl comes into picture in all of this. does vtbl contain
references to only virtual functions? if yes, then how a call is made
to Derived's non-virtual function using base class reference?

On 3 Oct, 07:53, "skarthiksel...@gmail.com" <skarthiksel...@gmail.com>
wrote:

Nilu

unread,
Oct 7, 2010, 11:50:20 AM10/7/10
to nextgen_engg
Also, in the code, when derived object is constructed, does vtbl get
created or or as soon as the class was declared, vtbl got created at
compile time? i.e., the concept of vtbl is class based or object
based?
Reply all
Reply to author
Forward
0 new messages