DacCum

ООП лаб 7(2)

Nov 6th, 2021 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class tractor {
  7. private:
  8.     string brand,
  9.            fuel_type,
  10.            engine_power;
  11.     double price;
  12. public:
  13.     tractor() {
  14.         brand = "ERROR";
  15.         fuel_type = "ERROR";
  16.         engine_power = -1;
  17.         price = -1;
  18.     }
  19.     tractor(string _brand, string _fuel_type, double _engine_power, double _price) {
  20.         brand = _brand;
  21.         fuel_type = _fuel_type;
  22.         engine_power = _engine_power;
  23.         price = _price;
  24.     }
  25.     tractor(const tractor& t) {
  26.         brand = t.brand;
  27.         fuel_type = t.fuel_type;
  28.         engine_power = t.engine_power;
  29.         price = t.price;
  30.     }
  31.     void input() {
  32.         cout << endl;
  33.         cout << "Enter brand: ";
  34.         getline(cin, brand);
  35.         cout << "Enter type of fuel: ";
  36.         cin >> fuel_type;
  37.         cin.ignore(37578, '\n');
  38.         cout << "Enter engine power(kW): ";
  39.         cin >> engine_power;
  40.         engine_power += "kW";
  41.         cin.ignore(37578, '\n');
  42.         cout << "Enter price($): ";
  43.         cin >> price;
  44.         cin.ignore(37578, '\n');
  45.     }
  46.     void print() {
  47.         cout << endl;
  48.         cout << "Brand: " << brand << endl;
  49.         cout << "Type of fuel: " << fuel_type << endl;
  50.         cout << "Engine power: " << engine_power << endl;
  51.         cout << "Price: " << price << '$' << endl;
  52.     }
  53.     bool operator== (const tractor& t) {
  54.         if (price == t.price)
  55.             return 1;
  56.         return 0;
  57.     }
  58.     bool operator!= (const tractor& t) {
  59.         if (price != t.price)
  60.             return 1;
  61.         return 0;
  62.     }
  63.     bool operator> (const tractor& t) {
  64.         if (price > t.price)
  65.             return 1;
  66.         return 0;
  67.     }
  68.     bool operator< (const tractor& t) {
  69.         if (price < t.price)
  70.             return 1;
  71.         return 0;
  72.     }
  73.     tractor operator+ (const tractor& t) {
  74.         tractor res;
  75.         res.brand = brand + " & " + t.brand;
  76.         res.fuel_type = fuel_type + " & " + t.fuel_type;
  77.         res.engine_power = engine_power + " & " + t.engine_power;
  78.         res.price = price + t.price;
  79.         return res;
  80.     }
  81. };
  82.  
  83. int main() {
  84.     tractor belarus;
  85.     belarus.input();
  86.     tractor MT;
  87.     MT.input();
  88.  
  89.     cout << endl;
  90.     cout << "operator== for belarus and MT returned " << (belarus == MT) << endl;
  91.     cout << "operator!= for belarus and MT returned " << (belarus != MT) << endl;
  92.     cout << "operator< for belarus and MT returned " << (belarus < MT) << endl;
  93.     cout << "operator> for belarus and MT returned " << (belarus > MT) << endl;
  94.  
  95.     tractor MTandBelarus = MT + belarus;
  96.     MTandBelarus.print();
  97.     return 0;
  98. }
Add Comment
Please, Sign In to add comment