Advertisement
akela43

Animal

Apr 3rd, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. //clang 3.8.0
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <utility> //swap
  6. #include <vector>
  7. class Animal {
  8. public:
  9.     Animal() = default;
  10.     Animal(const Animal &animal) = default;
  11.     Animal(const std::string &name,
  12.            const double weight,
  13.            const double age,
  14.            const std::string &ration):
  15.     name(name), weight(weight), age(age), ration(ration) {};
  16.    
  17.     std::string getName() const {return this->name; };
  18.     void setName(const std::string &name_) {this->name = name_;};
  19.    
  20.  
  21.     Animal& operator=(const Animal &other) {
  22.         Animal temp(other);
  23.         swap(temp);
  24.         return *this;
  25.     }
  26.     const bool operator<(const Animal &other) const {
  27.         return this->name < other.name;
  28.     }
  29.    friend std::ostream& operator<<(std::ostream& os, const Animal& animal);
  30.    friend std::istream& operator>>(std::istream& is, Animal& animal);
  31.  
  32. private:
  33.     void swap(Animal &other) {
  34.         std::swap(this->name, other.name);
  35.         std::swap(this->weight, other.weight);
  36.         std::swap(this->age, other.age);
  37.         std::swap(this->ration, other.ration);
  38.     }
  39.     std::string name;
  40.     double weight;
  41.     double age;
  42.     std::string ration;
  43.    
  44. };
  45.  
  46. std::ostream& operator<<(std::ostream& os, const Animal& a) {
  47.     os << a.name << " " << a.age << " "  << a.weight << " " << a.ration;
  48.     return os;
  49. }
  50. std::istream& operator>>(std::istream& is, Animal& a) {
  51.     is >> a.name >> a.age >> a.weight >> a.ration;
  52.     return is;
  53. }
  54. int main()
  55. {
  56.     std::vector <Animal> v;
  57.     v.push_back({"tor", 12, 12, "d1"});
  58.     v.push_back({"loki", 12, 12, "d2"});
  59.     v.push_back({"set", 12, 12, "d2"});
  60.    
  61.     Animal animal;
  62.     std::cin >> animal;
  63.     v.push_back(animal);
  64.  
  65.     std::sort(v.begin(), v.end());
  66.     for (const auto &a: v) std::cout << a << "\n";
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement