Sign in to follow this  
SlashOwnsU

C++ Question...this should be easy for you

Recommended Posts

hi...I learned everything from java and I'm now learning C++ there's one thing I can't do calling a parent method from the child ex : class Parent{ virtual void meth(){...} }; class Child:public Parent{ void meth(){[INSERT CALL HERE]}; }; just like we do with super(...) in java is it possible ?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can explicitly tell a class to run a function from one of it's superclasses:

void Child::meth()
{
Parent::meth();
}


That requires you to know which parent you want to call, and will break if you rearrange your heirarchy.

I usually typedef the parent type within my child classes as "ParentClass", that way, I can call ParentClass::myfunc(), to keep a bit of standardization between classes.

Share this post


Link to post
Share on other sites
The class "Child" inherits everything from the base class, therefore Child literally has a "meth" function. So, you can just call "meth()" from anywhere inside the class. For example

class base
{
protected:
void print(int x)
{
std::cout << x << std::endl;
}
};

class base2 : public base
{
public:
base2()
{
print(2);
print(5);
}
};


int _tmain()
{
base2 *temp = new base2();
delete (temp);
}




this would print
2
5
to the console window

So that's about it


EDIT: you can also do it the way the people who beat me to it showed you

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