473,322 Members | 1,347 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,322 software developers and data experts.

Question on C++ parent and child functions


Consider the following:

class ParentClass
{
public:
void FunctionOne( void );
protected:
void FunctionTwo( void );
private:
void FunctionThree( void );
};
ParentClass::FunctionOne( void )
{
FunctionTwo();
}

ParentClass::FunctionTwo( void )
{
FunctionThree();
}

ParentClass::FunctionThree( void )
{
printf( "Executing parent class's FunctionThree\n" );
}
class ChildClass : public ParentClass
{
public:
void FunctionTwo( void );
void FunctionThree( void );
};

ChildClass::FunctionTwo( void )
{
FunctionThree();
}

ChildClass::FunctionThree( void )
{
printf( "Executing child class's FunctionThree\n" );
}

int main( int argc, char** argv )
{
ChildClass* ccPtr = new ChildClass;

ccPtr->FunctionOne();
}
There is no ChildClass::FunctionOne(), so it's actually
ParentClass::FunctionOne() that gets executed.

My question is, is the call to FunctionTwo() within FunctionOne() a
call to the parent class's FunctionTwo(), or the child class's
FunctionTwo()?

I suspect it's the former (parent class), so my question then becomes,
how do I force it to execute the child class's FunctionTwo()?

Maybe something like pass FunctionOne() the "this" pointer and then
have FunctionOne call FunctionTwo and FunctionThree using the this
pointer (i.e. "CallersThis->FunctionOne()" and
"CallersThis->FunctionTwo()")?
Mar 25 '06 #1
1 5516
Eric wrote:
My question is, is the call to FunctionTwo() within FunctionOne() a
call to the parent class's FunctionTwo(), or the child class's
FunctionTwo()?

I suspect it's the former (parent class), so my question then becomes,
how do I force it to execute the child class's FunctionTwo()?


You are correct - it executes the parent's FunctionTwo because
FunctionTwo is not virtual. Declare FuntionTwo and FunctionThree as
virtual (in the parent class, and optionally in the derived class) and
you'll see the behaviour you want.

Mar 25 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Peter Hamilton | last post by:
I am trying to implement inheritance but I am having a difficult time with some concepts. What I am trying to do is have a Child object inherit from the Parent, and when you set a property value...
16
by: Suzanne Vogel | last post by:
Hi, I've been trying to write a function to test whether one class is derived from another class. I am given only id's of the two classes. Therefore, direct use of template methods is not an...
8
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By...
6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
3
by: Maheshkumar.R | last post by:
Hi groups, How i can command over the MDI CHIlD forms created dynamically at runtime from PARENT. Let say, i have generated 5 mdichild forms, but i want to work with child form1 from MDI...
2
by: Jim Shank | last post by:
I am really trying to find the best OOP way of doing this. I have a parent MDI form with multiple children and I am trying to communicate variables between them. I have been able to successfully...
7
by: msxkim | last post by:
How to execute functions in the parent class first and then functions in the child class? For example, I have a parent class with functions 'ONE' and 'TWO' and child class has a function 'THREE'. ...
3
by: Eddie | last post by:
If FormMain = MDI parent, FormSub = Child parent, I execute FormSub from the menu like this way. FormSub^ sub = gcnew FormSub; sub->MdiParent = this; sub->Show(); This can generate child...
2
by: ciccio | last post by:
Hi, Ones more I have a stupid virtual function question. Assume the following simple lines of code class Parent { public: virtual Parent foo(Parent &); virtual void bar(Parent &); }
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...
1
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: 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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.