Overriding a function means changing the implementation of a base class function in a derived class.
When you make an object of the derived class, the corresponding function is called.
To override a function, a derived class creates a member function with the same return type and signature as a member function in the base class.
Dog class overrides the speak() member function of the Mammal class, causing Dog objects to say "Woof!" when the function is called.
#include <iostream> class Mammal /*from ww w . j a v a 2 s.c o m*/ { public: // constructors Mammal() { std::cout << "Mammal constructor ...\n"; } ~Mammal() { std::cout << "Mammal destructor ...\n"; } // other member functions void speak() const { std::cout << "Mammal sound!\n"; } void sleep() const { std::cout << "Shhh. I'm sleeping.\n"; } protected: int age; int weight; }; class Dog : public Mammal { public: // constructors Dog() { std::cout << "Dog constructor ...\n"; } ~Dog() { std::cout << "Dog destructor ...\n"; } // other member functions void wagTail() { std::cout << "Tail wagging ...\n"; } void begForFood() { std::cout << "Begging for food ...\n"; } void speak() const { std::cout << "Woof!\n"; } private: int breed; }; int main() { Mammal bigAnimal; Dog fido; bigAnimal.speak(); fido.speak(); return 0; }