Derived classes

A

Alfred

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");
}

___________________________
 
Z

Zara

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
 
A

anon

Alfred said:
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-lite/strange-inheritance.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??
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top