Advertisement
35657

Untitled

Aug 31st, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1.  
  2. #include <string>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. // домашнее животное
  8. class Pet {
  9. public:
  10.     Pet(string name, int age, string color) : name_(name), age_(age), color_(color) {}
  11.  
  12.     string GetName() const {
  13.         return name_;
  14.     }
  15.  
  16.     void SetAge(int age) {
  17.         age_ = age;
  18.     }
  19.  
  20.     int GetAge() const {
  21.         return age_;
  22.     }
  23.  
  24.     string GetColor() const {
  25.         return color_;
  26.     }
  27.  
  28.  
  29. private:
  30.     string name_;
  31.     int age_;
  32.     string color_;
  33. };
  34.  
  35.  
  36. class Dog : public Pet {
  37. public:
  38.     Dog(string name, int age, string color, string breed) : Pet(name, age, color), breed_(breed) {}
  39.  
  40.     string GetBreed() const {
  41.         return breed_;
  42.     }
  43.  
  44. private:
  45.     string breed_;
  46.  
  47. };
  48.  
  49. class Cat : public Pet {
  50. public:
  51.     Cat(string name, int age, string color, bool longhair) : Pet(name, age, color), longhair_(longhair) {}
  52.  
  53.     string Longhair() const {
  54.         return longhair_ ? "longhair" : "shorthair";
  55.     }
  56.  
  57. private:
  58.     bool longhair_;
  59.  
  60. };
  61.  
  62.  
  63. class Parrot : public Pet {
  64. public:
  65.     Parrot(string name, int age, string color, bool talking) : Pet(name, age, color), talking_(talking) {}
  66.  
  67.     string Talking() const {
  68.         return talking_ ? "talking" : "not talking";
  69.     }
  70.  
  71. private:
  72.     bool talking_;
  73.  
  74. };
  75.  
  76. int main() {
  77.     setlocale(LC_ALL, "ru");
  78.  
  79.     Dog dog("Rex", 2, "black", "shepherd");
  80.  
  81.     Cat cat("Lucky", 1, "white", true);
  82.  
  83.     Parrot parrot("Gosha", 3, "yellow", true);
  84.  
  85.     cout << dog.GetName() << " " << dog.GetAge() << " " << dog.GetColor() << " " << dog.GetBreed() << endl;
  86.  
  87.     cout << cat.GetName() << " " << cat.GetAge() << " " << cat.GetColor() << " " << cat.Longhair() << endl;
  88.  
  89.     cout << parrot.GetName() << " " << parrot.GetAge() << " " << parrot.GetColor() << " " << parrot.Talking() << endl;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement