Overloading Versus Overriding, Hiding the Base Class Member Function - C++ Class

C++ examples for Class:Polymorphism

Description

Overloading Versus Overriding, Hiding the Base Class Member Function

Demo Code

#include <iostream> 
   /*from  w  ww  . j av a 2 s .co m*/
class Pet 
{ 
public: 
    void move() const { std::cout << "Pet moves one step\n"; } 
    void move(int distance) const  { std::cout << "Pet moves " << distance <<" steps\n"; } 
protected: 
    int age; 
    int weight; 
}; 
   
class Dog : public Pet 
{ 
public: 
    void move() const { std::cout << "Dog moves 5 steps\n"; } 
}; // you may receive a warning that you are hiding a function! 
   
int main() 
{ 
    Pet bigAnimal; 
    Dog fido; 
    bigAnimal.move(); 
    bigAnimal.move(2); 
    fido.move(); 
    // fido.move(10); 
    return 0; 
}

Result


Related Tutorials