Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //clang 3.8.0
- #include <iostream>
- #include <string>
- #include <utility> //swap
- #include <vector>
- class Animal {
- public:
- Animal() = default;
- Animal(const Animal &animal) = default;
- Animal(const std::string &name,
- const double weight,
- const double age,
- const std::string &ration):
- name(name), weight(weight), age(age), ration(ration) {};
- std::string getName() const {return this->name; };
- void setName(const std::string &name_) {this->name = name_;};
- Animal& operator=(const Animal &other) {
- Animal temp(other);
- swap(temp);
- return *this;
- }
- const bool operator<(const Animal &other) const {
- return this->name < other.name;
- }
- friend std::ostream& operator<<(std::ostream& os, const Animal& animal);
- friend std::istream& operator>>(std::istream& is, Animal& animal);
- private:
- void swap(Animal &other) {
- std::swap(this->name, other.name);
- std::swap(this->weight, other.weight);
- std::swap(this->age, other.age);
- std::swap(this->ration, other.ration);
- }
- std::string name;
- double weight;
- double age;
- std::string ration;
- };
- std::ostream& operator<<(std::ostream& os, const Animal& a) {
- os << a.name << " " << a.age << " " << a.weight << " " << a.ration;
- return os;
- }
- std::istream& operator>>(std::istream& is, Animal& a) {
- is >> a.name >> a.age >> a.weight >> a.ration;
- return is;
- }
- int main()
- {
- std::vector <Animal> v;
- v.push_back({"tor", 12, 12, "d1"});
- v.push_back({"loki", 12, 12, "d2"});
- v.push_back({"set", 12, 12, "d2"});
- Animal animal;
- std::cin >> animal;
- v.push_back(animal);
- std::sort(v.begin(), v.end());
- for (const auto &a: v) std::cout << a << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement