#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.

 Vote 0

Similar topics related to this section

object-oriented, C vs C++, C++ vs Java, encapsulation, constructor, overloaded constructor, destructor, destructor overloaded, copy constructor, copy constructor, deep copy, deep copy vs shallow copy, singleton, default access modifier, default access modifier, default access modifier, abstraction, THIS pointer, static function, THIS pointer, a static function, scope resolution operator, inline function, private vs protected, const function, polymorphism, operator overloading, function overloading, overriding, prefix vs postfix, friend class, friend function, sizeof class with virtual, vfptr and vftable, vfptr and vftable using C, early binding and late binding, inheritance, virtual function call from a constructor/destructor, virtual destructor, virtual base class, virtual base class, exception, stack unwinding, exception handling, try-catch block, namespace, mutable variable and const function, mutable keyword, explicit, access a member function, object slicing, ctor sequence of constructor, dtor sequence of destructor, virtual destructor, print the type, dynamic casting, static casting, dynamic and static casting, const casting, reinterpret casting, static and reinterpret casting, template class, template vs macro,

#Object-Oriented Programming in C++ (4th Edition)
#Programming: Principles and Practice Using C++ (2nd Edition)
#The C++ Programming Language, 4th Edition
#Professional C++ Paperback by Nicholas A.Solter, Scott J.Kleper
#Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14