468,291 Members | 1,422 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,291 developers. It's quick & easy.

Derived classes

Hi ti all!
I'd like o ask some clarification about this code.
I'd like to undestand why in main function pD->f() return D even if pE
and pD have the same value.
Do you know where I can find more information about?

Thank you very much!!!
Regards
Alfred

___________________________

#include <cstdlib>
#include <conio.h>
#include <iostream>
#include <windows.h>

using namespace std;
class B {
public:
void f() { cout << "B" << endl; };
};

class D : public B{
public: void f() { cout << "D" << endl; };
};

class E : public D {
public: void f() { cout << "E" << endl; };
};

int main()
{

E *pE = new E;
pE->f();
cout <<"pE= " << pE << endl;

D *pD = pE;
cout <<"pD= " << pD << endl;
pD->f();

system ("pause");
}

___________________________
Sep 19 '08 #1
2 969
On Fri, 19 Sep 2008 00:10:13 -0700 (PDT), Alfred <ga******@yahoo.com>
wrote:
>Hi ti all!
I'd like o ask some clarification about this code.
I'd like to undestand why in main function pD->f() return D even if pE
and pD have the same value.
Do you know where I can find more information about?

You must look for information about *virtual functions* and how to use
them

Best regards,

zara
Sep 19 '08 #2
Alfred wrote:
Hi ti all!
I'd like o ask some clarification about this code.
I'd like to undestand why in main function pD->f() return D even if pE
and pD have the same value.
Because you did this:
http://www.parashift.com/c++-faq-lit....html#faq-23.8

Maybe you wanted to make the method f() virtual?
Do you know where I can find more information about?
books, online, etc

Thank you very much!!!
Regards
Alfred

___________________________

#include <conio.h>
#include <windows.h>
btw what are these two headers for??
Sep 19 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Brad Kartchner | last post: by
6 posts views Thread by John Glover | last post: by
6 posts views Thread by Klaus | last post: by
4 posts views Thread by Josh Valino | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.