C++ examples for Class:Polymorphism
Overriding Functions in base class
#include <iostream> // w w w .ja v a2s .c o m enum COLOR { RED, BLUE, WHITE, BLACK, EE, YELLOW }; class Pet { public: // constructors Pet() { std::cout << "Pet constructor ...\n"; } ~Pet() { std::cout << "Pet destructor ...\n"; } // other member functions void speak() const { std::cout << "Pet sound!\n"; } void sleep() const { std::cout << "Shhh. I'm sleeping.\n"; } protected: int age; int weight; }; class Dog : public Pet { public: // constructors Dog() { std::cout << "Dog constructor ...\n"; } ~Dog() { std::cout << "Dog destructor ...\n"; } // other member functions void wagTail() { std::cout << "Tail wagging ...\n"; } void begForFood() { std::cout << "Begging for food ...\n"; } void speak() const { std::cout << "Woof!\n"; } private: COLOR color; }; int main() { Pet bigAnimal; Dog fido; bigAnimal.speak(); fido.speak(); return 0; }