Advertisement
Vla_DOS

18

Feb 22nd, 2023
1,165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Phones {
  5. private:
  6.     string brand;
  7.     double price;
  8.     double screen_size;
  9. public:
  10.     Phones(string brand, double price, double screen_size) {
  11.         this->brand = brand;
  12.         this->price = price;
  13.         this->screen_size = screen_size;
  14.     }
  15.  
  16.     ~Phones() {}
  17.  
  18.     double getPrice() {
  19.         return price;
  20.     }
  21.  
  22.     void print() {
  23.         cout << "Brand: " << brand << endl;
  24.         cout << "Price: " << price << endl;
  25.         cout << "Screen size: " << screen_size << endl;
  26.     }
  27. };
  28.  
  29. void findMostExpensive(Phones arr[], int size) {
  30.     double maxPrice = 0;
  31.     int index = -1;
  32.     for (int i = 0; i < size; i++) {
  33.         if (arr[i].getPrice() > maxPrice) {
  34.             maxPrice = arr[i].getPrice();
  35.             index = i;
  36.         }
  37.     }
  38.     if (index != -1) {
  39.         cout << "The most expensive phone: " << endl;
  40.         arr[index].print();
  41.     }
  42. }
  43. void PrintAllPhones(Phones arr[], int size) {
  44.  
  45.     for (int i = 0; i < size; i++) {
  46.         arr[i].print();
  47.         cout << endl;
  48.     }
  49. }
  50. int main() {
  51.     const int size = 3;
  52.     Phones phones[size] = { Phones("Samsung", 500, 6.2),
  53.                             Phones("Apple", 800, 6.1),
  54.                             Phones("Huawei", 400, 6.5) };
  55.  
  56.     PrintAllPhones(phones, size);
  57.     cout << endl;
  58.     findMostExpensive(phones, size);
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement