472,958 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1101
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.