Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Device {
  5. public:
  6. void turn_on() {
  7. cout << "Device is on." << endl;
  8. }
  9. };
  10.  
  11. class Computer: public Device {};
  12.  
  13. class Monitor: public Device {};
  14.  
  15. class Laptop: public Computer, public Monitor {
  16. /*
  17. public:
  18. void turn_on() {
  19. cout << "Laptop is on." << endl;
  20. }
  21. // uncommenting this function will resolve diamond problem
  22. */
  23. };
  24.  
  25. int main() {
  26. Laptop my_laptop;
  27.  
  28. // my_laptop.turn_on();
  29. // will produce compile time error
  30. // if Laptop.turn_on function is commented out
  31.  
  32. // calling method of specific superclass
  33. my_laptop.Monitor::turn_on();
  34.  
  35. // treating Laptop instance as Monitor instance via static cast
  36. static_cast<Monitor&>( my_laptop ).turn_on();
  37.  
  38. return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement