CPP Override and Hiding Methods

Overriding base class methods in a derived class

  • When a method in a derived class has the same name, arguments in numbers and types and the same return type the method says to override that base class method.
  • If in this case the base class has an overloaded method with the same name as the overridden method, that method says to be hided and cannot be accessed by the derived class without the special class (baseclass::) notation.
    Class Override and Hiding method example:
    #include <iostream>
    #include <cstring>
    
    class Person {
    public:
      Person (int age, int weight){
        this->age = age;
        this->weight = weight;
      }
      // this method is overrided in the drived class
      void showInfo() {
        std::cout << "I am " << age << " years old " ;
        std::cout << "and weighs " << weight << " kilo.\n\n" ;
      }
      // this method is hided
      void showInfo(std::string pname) {
        std::cout << "I am " << pname.c_str()  << " " << age << " years old " ;
        std::cout << "and weighs " << weight << " kilo.\n\n" ;
      }
      int getAge() { return age; }
      int getWeight() { return weight; }
    protected:
      int age;
    private:
      int weight;
    };
    
    class Employee : public Person {
    public:
      Employee (int age, int weight, int salary): Person(age,weight){
        this->salary = salary;
      }
      // This method override the class method : void showInfo();
      void showInfo() {
        // Drived class can only access protected members in base class
        // as long as Inheritance access specifier is pucblic or protected.
        std::cout << "I am " << age << " years old " ;
        std::cout << "and weighs " << getWeight() << " kilo " ;
        std::cout << "and earns " << salary << " dollar.\n\n" ;
        // access hided method inside the drived class
        Person::showInfo("an Employee");
      }
      int getSalary() { return salary; }
    private:
      int salary;
    };
    int main()
    {
      Person * pRicard = new Person(40, 70);
      pRicard->showInfo();
      // access hided method outside the class
      pRicard->Person::showInfo("a Person");
      delete pRicard;
      Employee * pObama = new Employee(45, 65, 50000);
      pObama->showInfo();
      delete pObama;
      return 0;
    }
    When we run this application, the result will be:
    I am 40 years old and weighs 70 kilo.
    
    I am a Person 40 years old and weighs 70 kilo.
    
    I am 45 years old and weighs 65 kilo and earns 50000 dollar.
    
    I am an Employee 45 years old and weighs 65 kilo.

    You can download this example here (needed tools can be found in the right menu on this page).

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.