Description

Access parent method

Demo

#include <iostream> 
class Mammal //  w  w  w  . ja va 2  s.co  m
{ 
public: 
    // constructors 
    Mammal(): age(2), weight(5) {} 
    ~Mammal(){} 
   
    // accessors 
    int getAge() const { return age; } 
    void setAge(int newAge) { age = newAge; } 
    int getWeight() const { return weight; } 
    void setWeight(int newWeight) { weight = newWeight; } 
   
    // other member functions 
    void speak() const { std::cout << "Mammal sound!\n"; } 
    void sleep() const { std::cout << "I'm sleeping.\n"; } 
   
protected: 
    int age; 
    int weight; 
}; 
   
class Dog : public Mammal 
{ 
public: 
    // constructors 
    Dog(): breed(2) {} 
    ~Dog() {} 
   
    // accessors 
    int getBreed() const { return breed; } 
    void setBreed(int newBreed) { breed = newBreed; } 
   
    // other member functions 
    void wagTail() { std::cout << "Tail wagging ...\n"; } 
    void begForFood() { std::cout << "Begging for food ...\n"; } 
   
private: 
    int breed; 
}; 
   
int main() 
{ 
    Dog my_dog2; 
    my_dog2.speak(); 
    my_dog2.wagTail(); 
    std::cout << "Fido is " << my_dog2.getAge() << " years old\n"; 
    return 0; 
}

Result

Related Topic