erehh

oop finaluri savarjisho 1

Jun 19th, 2022
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class SportsCar;
  7. class Car
  8. {
  9. private:
  10.     double speed;
  11.     double weight;
  12. public:
  13.     Car() {
  14.         cout << "Enter car speed - "; cin >> speed;
  15.         cout << "Enter car weight - "; cin >> weight;
  16.     }
  17.     double getWeight() { return weight; }
  18.  
  19.     int compare(const SportsCar& obj);
  20. };
  21.  
  22. class SportsCar
  23. {
  24. private:
  25.     double speed;
  26.     double weight;
  27.     string color;
  28. public:
  29.     SportsCar() {
  30.         cout << "Enter sports car speed - "; cin >> speed;
  31.         cout << "Enter sports car weight - "; cin >> weight;
  32.         cout << "Enter sports car color - "; cin >> color;
  33.     }
  34.  
  35.     double getWeight() { return weight; }
  36.     string getColor() { return color; }
  37.    
  38.     friend int Car :: compare(const SportsCar& obj);
  39. };
  40.  
  41. int Car::compare(const SportsCar& obj) {
  42.     if (this->speed > obj.speed)return 1;
  43.     else if (this->speed < obj.speed)return 2;
  44.     else return 0;
  45. }
  46.  
  47. int main()
  48. {
  49.     Car A;
  50.     SportsCar B;
  51.     if (A.compare(B) == 1)
  52.         cout << "Car weight - " << A.getWeight() << endl;
  53.     else if (A.compare(B) == 2)
  54.         cout << "Sports car weight - " << B.getWeight() << endl;
  55.     else
  56.         cout << "Sports car color - " << B.getColor() << endl;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment