1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 5;
class car
{
private:
string name;
int mpg;
double price;
int horsepower;
public:
car(string name, int mpg, double price, int horsepower);
void viewCars();
void bestMileage();
void lowestPrice();
void greatestHorspower();
};
int main()
{
car carArray[SIZE] = {
car("2012 Toyota Scion tC", 26, 10575, 180),
car("2019 Toyota TUNDRA", 18, 33320, 310),
car("2017 Chrysler 200", 25, 19000, 295),
car("2017 Chevy Silverado", 15, 26946, 285),
car("2007 BMC Envoy", 17, 4500, 291)
};
for (size_t i = 0; i < SIZE; i++) {
carArray[i].viewCars();
cout << endl;
}
carArray[5].bestMileage();
cout << endl;
carArray[5].lowestPrice();
cout << endl;
carArray[5].greatestHorspower();
system("pause");
return 0;
}
car::car(string name, int mpg, double price, int horsepower) {
this->name = name;
this->mpg = mpg;
this->price = price;
this->horsepower = horsepower;
}
void car::viewCars()
{
cout << "Car name: " << name << endl;
cout << "Car MPG: " << mpg << endl;
cout << "Car price: " << price << endl;
cout << "Car horsepower: " << horsepower << endl;
}
void car::bestMileage()
{
car carArray[SIZE] = {
car("2012 Toyota Scion tC", 26, 10575, 180),
car("2019 Toyota TUNDRA", 18, 33320, 310),
car("2017 Chrysler 200", 25, 19000, 295),
car("2017 Chevy Silverado", 15, 26946, 285),
car("2007 BMC Envoy", 17, 4500, 291)
};
size_t idx = 0;
for (size_t i = 1; i < SIZE; ++i) {
if (carArray[i].mpg > carArray[idx].mpg) {
idx = i;
}
}
cout << "\nThe Vehicle with highest mileage:\n";
carArray[idx].viewCars();
cout << '\n';
}
void car::lowestPrice()
{
car carArray[SIZE] = {
car("2012 Toyota Scion tC", 26, 10575, 180),
car("2019 Toyota TUNDRA", 18, 33320, 310),
car("2017 Chrysler 200", 25, 19000, 295),
car("2017 Chevy Silverado", 15, 26946, 285),
car("2007 BMC Envoy", 17, 4500, 291)
};
size_t idx = 0;
for (size_t i = 1; i < SIZE; ++i) {
if (carArray[i].price < carArray[idx].price) {
idx = i;
}
}
cout << "\nThe Vehicle with the lowest price is: \n";
carArray[idx].viewCars();
cout << '\n';
}
void car::greatestHorspower()
{
car carArray[SIZE] = {
car("2012 Toyota Scion tC", 26, 10575, 180),
car("2019 Toyota TUNDRA", 18, 33320, 310),
car("2017 Chrysler 200", 25, 19000, 295),
car("2017 Chevy Silverado", 15, 26946, 285),
car("2007 BMC Envoy", 17, 4500, 291)
};
size_t idx = 0;
for (size_t i = 1; i < SIZE; ++i) {
if (carArray[i].horsepower > carArray[idx].horsepower) {
idx = i;
}
}
cout << "\nThe Vehicle with the highest Horse Power is : \n";
carArray[idx].viewCars();
cout << '\n';
}
|