fork(1) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4.  
  5. class Animal {
  6. public:
  7. Animal()
  8. { //std::cout << "tworze obiekt klasy Animal\n";
  9. age = 0;
  10. ilosc++; }
  11.  
  12. ~Animal() {
  13. ilosc--; }
  14.  
  15. int getAge() {
  16. return age; }
  17.  
  18. virtual void sound() {
  19. std::cout << "zwierzeta wydaja rozne glosy... " << std::endl; }
  20. static int ile() { return ilosc; }
  21.  
  22. protected:
  23. int age;
  24. std::string name;
  25.  
  26. private:
  27. static int ilosc;
  28. };
  29.  
  30. int Animal::ilosc = 2;
  31.  
  32. class Lion : public Animal
  33. {
  34. public:
  35. Lion(int age) { std::cout<< "tworze obiekt klasy Lion\n";
  36. this->age = age; }
  37.  
  38. void sound() {
  39. std::cout << "Lew robi mrau!\n"; }
  40. };
  41.  
  42. class Pet : public Animal {
  43. public:
  44. Pet() {
  45. std::cout << "tworze obiekt klasy Pet" << std::endl;
  46. name = ""; }
  47.  
  48. void sound() {
  49. std::cout << "Pecik mowi kizia mizia\n"; }
  50. std::string getName() {
  51. return name; }
  52.  
  53. protected:
  54. std::string name;
  55. };
  56.  
  57.  
  58.  
  59. class Dog : public Animal {
  60.  
  61. public:
  62. Dog(int age, std::string name) { std::cout << "tworze obiekt klasy Dog\n";
  63. this->age = age;
  64. this->name = name; }
  65.  
  66. int getHumanAge() {
  67. int x;
  68. x = this->getAge() *4;
  69. return x; }
  70.  
  71. void sound() {
  72. std::cout << "Pies robi hau hau!\n"; }
  73.  
  74. protected:
  75. std::string name;
  76. };
  77.  
  78.  
  79. int main() {
  80.  
  81. std::cout << "ilosc animalsow na moment obecny to: " << Animal::ile << std::endl;
  82.  
  83. Animal *animal = new Pet();
  84.  
  85. std::cout << "a teraz zwierzakow jest: " << Animal::ile << std::endl;
  86. Animal *toska = new Dog(12, "toska");
  87.  
  88. std::cout << "a teraz zwierzakow jest: " << Animal::ile << std::endl;
  89.  
  90. Animal *marchewka = new Lion(6);
  91.  
  92. std::cout << "a teraz zwierzakow jest: " << Animal::ile << std::endl;
  93.  
  94.  
  95. return 0;
  96. }
  97.  
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
ilosc animalsow na moment obecny to: 1
tworze obiekt klasy Pet
a teraz zwierzakow jest: 1
tworze obiekt klasy Dog
a teraz zwierzakow jest: 1
tworze obiekt klasy Lion
a teraz zwierzakow jest: 1