473,320 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1113
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Brad Kartchner | last post by:
I'm attempting to write a program with several child classes derived from a single parent class. The parent class has a couple of variables that need to be present in each of the child classes. ...
5
by: Nathan Bullock | last post by:
Hi, I have a base class, say Base and there are two classes, say Class1 and Class2 which are derived from Base. Is there any way for me, say from a static method in Base, to get a list of all...
13
by: z. f. | last post by:
Hi, i have a class that is derived from System.Web.UI.Page, and this is the class i use in my application as PageBase. all other page classes are deriverd from my PageBase instead of the...
6
by: John Glover | last post by:
I'm having a very strange problem with XML serialization. I'm writing web services which pass instances of various classes back and forth as parameters and return values of web methods. The...
8
by: Manuel | last post by:
Hi! If I've a vector filled with abstract classes, can I push in it the derived classes too? Even if derived classes have new methods? I've done some experiments, and it seem I can push the...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
6
by: Klaus | last post by:
Hi all: I want to have something like the following (incorrect!): class Base { public: virtual void MakeSomething(); }; class BehaviourModelOne {
4
by: Josh Valino | last post by:
Hi group, Is there a way in C# (.Net 3.5 FW) for me to define an abstract class that has some properties, and then in each derived class, select which properties I'd like available and which...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.