• Advertisement
Sign in to follow this  

object problem

This topic is 3623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I make it so when I call say the "step1()" function of object "bigball" it also runs the "step1()" function of ball?
class ball
{
	public:
	ball();
	~ball();
	virtual void step1();
	virtual void step2();
	virtual void step3();
	virtual void draw();
};
class bigball : public ball
{
	public:
	bigball ();
	~bigball ();
	virtual void step1();
	virtual void step2();
	virtual void step3();
	virtual void draw();
};

Share this post


Link to post
Share on other sites
Advertisement
Do you want bigball::step1() to do something that ball::step1() doesn't? Or do you just want to execute ball::step1() from bigball::step1(), without any added functionality in bigball::step1()?

Share this post


Link to post
Share on other sites
It goes a little something like this:

void bigball::step1()
{
// Make a call to the base member function
ball::step1();

// Do other stuff
}


Note that if 'do other stuff' actually equates to 'do nothing' then there's no point in overrriding the step1 function at all.

Share this post


Link to post
Share on other sites

with inheritance in c++ style languages you have to do it by explicitly calling the
base behavour method from the inherited one. ie

bigball :: step1()
{
// do the bigball stuff
// ...

// do the default ball stuff
// note that the method is type qualified to get the method we want
ball::step1();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement