Skip to content

Instantly share code, notes, and snippets.

@kateolenya
Last active March 28, 2019 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kateolenya/94e354c4d3d1928bb4b649a6ea04b13e to your computer and use it in GitHub Desktop.
Save kateolenya/94e354c4d3d1928bb4b649a6ea04b13e to your computer and use it in GitHub Desktop.
Example usage of keyword 'virtual' in C++
#include <iostream>
using namespace std;
class Device {
public:
Device() {
cout << "Device constructor called" << endl;
}
void turn_on() {
cout << "Device is on." << endl;
}
};
class Computer: virtual public Device {
public:
Computer() {
cout << "Computer constructor called" << endl;
}
};
class Monitor: virtual public Device {
public:
Monitor() {
cout << "Monitor constructor called" << endl;
}
};
class Laptop: public Computer, public Monitor {};
int main() {
Laptop Laptop_instance;
Laptop_instance.turn_on();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment