472,354 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 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 5417
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 &); }
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.