/*
Author: Internshala
Module: Fundamentals of Object Oriented Programming Using C++
Topic: Polymorphism
*/
#include <iostream>
#include <new>
using namespace std;
class Animal {
public:
virtual void sound() {
cout << "Animal class: making sound" << endl;
}
virtual void sleep() {
cout << "Animal class: sleeping" << endl;
}
};
class Dog : public Animal {
public:
virtual void sound() {
cout << "Dog class: bow-bow" << endl;
}
virtual void sleep() {
cout << "Dog class: sleeping" << endl;
}
};
int main() {
Dog dog; // dog --> Dog object
dog.sound();
dog.sleep();
Animal *animal = new Dog(); // animal --> Dog object
animal->sound();
animal->sleep();
return 0;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter