Sign in to follow this  
SSJCORY

C++ equivalent of the java super

Recommended Posts

In java if you did something like this: class monster{ ~~~~~~~~~~~~~~~~ public: void ShowInfo(){ cout<<"I am a monster."; } }; class greenmonster:public monster{ public: void ShowInfo(){ super.ShowInfo(); cout<<"and i am green!"; } }mona; The output upon calling mona.ShowInfo() would be I am a monster. and i am green! Is there a way to do this in c++?

Share this post


Link to post
Share on other sites
Certainly. Just qualify the name of the method with the name of the base class you're interested in (it can't be as simple as Java because C++ allows multiple inheritance). For example:

class A
{
public:
void do_something()
{
// ...
}
};

class B: public A
{
public:
void do_something()
{
// ...
A::do_something();
// ...
}
};

Of course, in such a case, it's good that A::do_something be virtual.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this