fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Device
  5. {
  6. protected:
  7. string name;
  8.  
  9. public:
  10. virtual void showStatus() = 0; // Pure Virtual Function
  11. };
  12.  
  13. class Laptop : public Device
  14. {
  15. private:
  16. string brand;
  17.  
  18. public:
  19. Laptop(string b)
  20. {
  21. brand = b;
  22. }
  23.  
  24. void showStatus()
  25. {
  26. cout << "Laptop " << brand << " is ON" << endl;
  27. }
  28. };
  29.  
  30. class Mobile : public Device
  31. {
  32. private:
  33. string model;
  34.  
  35. public:
  36. Mobile(string m)
  37. {
  38. model = m;
  39. }
  40.  
  41. void showStatus()
  42. {
  43. cout << "Mobile " << model << "is Running" << endl;
  44. }
  45. };
  46.  
  47. class Tablet : public Device
  48. {
  49. private:
  50. string osType;
  51.  
  52. public:
  53. Tablet(string os)
  54. {
  55. osType = os;
  56. }
  57.  
  58. void showStatus()
  59. {
  60. cout << "Tablet " << osType << "is Active" << endl;
  61. }
  62. };
  63.  
  64. int main()
  65. {
  66. Device *d;
  67.  
  68. Laptop l("Dell");
  69. Mobile m("Samsung");
  70. Tablet t("Android");
  71.  
  72. d = &l;
  73. d->showStatus();
  74.  
  75. d = &m;
  76. d->showStatus();
  77.  
  78. d = &t;
  79. d->showStatus();
  80.  
  81. return 0;
  82. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Laptop Dell is ON
Mobile Samsungis Running
Tablet Androidis Active