Calling the Base Member Function - C++ Class

C++ examples for Class:Polymorphism

Description

Calling the Base Member Function

Demo Code

#include <iostream> 
   //  ww w .ja  va2  s. c  o 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; 
}; 
   
void Dog::move() const 
{ 
    std::cout << "Dog moves ...\n"; 
    Pet::move(3); 
} 
   
int main() 
{ 
    Pet bigAnimal; 
    Dog fido; 
    bigAnimal.move(2); 
    fido.Pet::move(6); 
    return 0; 
}

Result


Related Tutorials