fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class Watch {
  6. public:
  7. void SetHours(int watchHours) {
  8. hours = watchHours;
  9. }
  10.  
  11. void SetMins(int watchMins) {
  12. mins = watchMins;
  13. }
  14.  
  15. virtual void PrintItem() {
  16. cout << hours << ":" << mins << endl;
  17. }
  18.  
  19. protected:
  20. int hours;
  21. int mins;
  22. };
  23.  
  24. class SmartWatch : public Watch {
  25. public:
  26. void SetPercentage(int watchPercentage) {
  27. batteryPercentage = watchPercentage;
  28. }
  29.  
  30. void PrintItem() {
  31. cout << hours << ":" << mins << " " << batteryPercentage << "%" << endl;
  32. }
  33.  
  34. private:
  35. int batteryPercentage;
  36. };
  37.  
  38. int main() {
  39. Watch* watch1;
  40. Watch* watch2;
  41. SmartWatch* watch3;
  42.  
  43. vector<Watch*> watchList;
  44. unsigned int i;
  45.  
  46. watch1 = new Watch();
  47. watch1->SetHours(6);
  48. watch1->SetMins(47);
  49.  
  50. watch2 = new Watch();
  51. watch2->SetHours(7);
  52. watch2->SetMins(28);
  53.  
  54. watch3 = new SmartWatch();
  55. watch3->SetHours(12);
  56. watch3->SetMins(43);
  57. watch3->SetPercentage(65);
  58.  
  59. watchList.push_back(watch2);
  60. watchList.push_back(watch1);
  61. watchList.push_back(watch3);
  62.  
  63. for (i = 0; i < watchList.size(); ++i) {
  64. watchList.at(i)->PrintItem();
  65. }
  66.  
  67. return 0;
  68. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
7:28
6:47
12:43 65%