fork(7) download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. const int DAYS = 0x08;
  6.  
  7. class Date
  8. {
  9. private:
  10. uint8_t day, month, year;
  11.  
  12. public:
  13. Date():day(1), month(1), year(00) {} // Initialisierungsliste mit konstante Parameterwerten
  14. // Initialisierungsliste ueber parametrisierten Konstruktor
  15. Date(uint8_t _day, uint8_t _month, uint8_t _year):day(_day), month(_month), year(_year) {}
  16.  
  17. uint8_t GetDay(); // Methode
  18. virtual string GetDay(string where);
  19. };
  20.  
  21. string Date::GetDay(string where)
  22. {
  23. //day = "Datestr " + toString(value);
  24. string day = where + " In Date Day: " + to_string(GetDay());
  25. return day;
  26. }
  27.  
  28. uint8_t Date::GetDay()
  29. {
  30. return day;
  31. }
  32.  
  33. class DateString : public Date
  34. {
  35. public:
  36. DateString():Date() {}
  37. DateString(uint8_t _day, uint8_t _month, uint8_t _year): Date(_day, _month, _year) {}
  38.  
  39. int GetDay() { return 10; }
  40. int GetDay(int xy) { return 99; }
  41. virtual string GetDay(string where);
  42. };
  43.  
  44. string DateString::GetDay(string where)
  45. {
  46. //day = "Datestr " + toString(value);
  47. string day = where + " In DateString Day: " + to_string(GetDay());
  48. return day;
  49. }
  50.  
  51. int main()
  52. {
  53. Date *pBoth;
  54. DateString ds;
  55. Date d;
  56. pBoth = &d; // Mit new wird Speicher wird auf dem Heap reserviert
  57.  
  58. // löschen Sie virtual vor der Deklaration. Wann und was ändert sich am Ergebnis?
  59.  
  60. pBoth = &d;
  61. cout << pBoth->GetDay("Where am I?") << endl; // Virtuelle Methode GetDay return 1
  62. pBoth = &ds;
  63. cout << pBoth->GetDay("Where am I?") << endl; // Virtuelle Methode GetDay return 10;
  64. }
  65.  
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
Where am I? In Date Day: 1
Where am I? In DateString Day: 10