fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Person
  5. {
  6. protected:
  7. int age, weight, height;
  8. public:
  9. Person(int, int, int);
  10. ~Person();
  11. virtual void getAge()
  12. {
  13. cout << age << endl;
  14. }
  15. void getWeight()
  16. {
  17. cout << weight << endl;
  18. }
  19. void getHeight()
  20. {
  21. cout << height << endl;
  22. }
  23. };
  24.  
  25. Person::Person(int age, int weight, int height)
  26. {
  27. this -> age = age;
  28. this -> weight = weight;
  29. this -> height = height;
  30. }
  31.  
  32. Person::~Person()
  33. {
  34. }
  35.  
  36. class Dog : public Person
  37. {
  38. public:
  39. Dog(int, int, int);
  40. void getAge()
  41. {
  42. cout << age << " -- Dog Age" << endl;
  43. }
  44. void getWeight()
  45. {
  46. cout << weight << " -- Dog Weight" << endl;
  47. }
  48. void getHeight()
  49. {
  50. cout << height << "Dog Height" << endl;
  51. }
  52. };
  53.  
  54. Dog::Dog(int age, int weight, int height) : Person::Person(age, weight, height)
  55. {
  56. }
  57.  
  58. class Cat : public Person
  59. {
  60. public:
  61. Cat(int, int, int);
  62. void getAge()
  63. {
  64. cout << age << "Cat Age" << endl;
  65. }
  66. void getWeight()
  67. {
  68. cout << weight << "Cat Height" << endl;
  69. }
  70. void getHeight()
  71. {
  72. cout << height << "Cat Weight" << endl;
  73. }
  74. };
  75.  
  76. Cat::Cat(int age, int weight, int height) : Person::Person(age, weight, height)
  77. {
  78. }
  79.  
  80. int main()
  81. {
  82. Person* person;
  83. Dog cake(15, 55, 175);
  84. Cat terry(15, 55, 532);
  85. person = &cake;
  86. person->getAge();
  87. person = &terry;
  88. person->getAge();
  89.  
  90. Dog Cake(15, 55, 175);
  91. Cat Terry(5, 76445, 1756656);
  92. Cake.getAge();
  93. Terry.getAge();
  94. Terry.getWeight();
  95. Terry.getHeight();
  96.  
  97. return 0;
  98. }
  99.  
  100.  
Success #stdin #stdout 0s 3100KB
stdin
Standard input is empty
stdout
15 -- Dog Age
15Cat Age
15 -- Dog Age
5Cat Age
76445Cat Height
1756656Cat Weight