Alex Rivera | Logout

How to call a parent class function from derived class function?

Asked 2008-12-10T19:35:45.430
809

How do I call the parent function from a derived class using C++? For example, I have a class called parent, and a class called child which is derived from parent. Within each class there is a print function. In the definition of the child's print function I would like to make a call to the parents print function. How would I go about doing this?

Edit
Report

1 Answer

42

If your base class is called Base, and your function is called FooBar() you can call it directly using Base::FooBar()

void Base::FooBar()
{
   printf("in Base\n");
}

void ChildOfBase::FooBar()
{
  Base::FooBar();
}
answered 2008-12-10T19:40:45.110

Your Answer