#Previous question #Index of C++ Questions #Next question

Base class functionality used to get inherited to child classes. Thus if a function is there in base class and a child has been inherited from it, it calls the base function. If child wants to overwrite the base functionality, it should declare the exact prototype in child class as that of the base. Now onwards compiler will use only the child function when applicable. This mechanism is called overriding.

Example:
Suppose we have a class “base”(version:1.0) and a member function to display version number named print_version().
Now suppose we have a new functional class “child”(version: 1.1) inherited from “base”. If we do not override the function print_version(), it will continue to show version number 1.0. Thus we need to override the function to display new version number. Compiler ensures that old function will be overwritten with this new version of child class.

// version : 1.0
class base 
{
public:
  void print_version(void)
  {
    cout << " version: v.1.0";
  }
};
// version : 1.1
class child : public base
{
public:
  void print_version (void)
  {
    cout << " version: v.1.1";
  }
};
int main (int argc, char *argv[])
{
  child c1;
  c1. print_version ();
}

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

Most popular topics (must visit)

Useful links for students