Answer to Question #273581 in C++ for jye

Question #273581

Define a Car class that represents a car with the following requirements:

Speed ​​int, brand in string, price in float.Default constructor definition, function for displaying vehicle information, function for assigning vehicle speed, for assigning vehicle brand, for assigning a vehicle price, for writing a chapter main program using the above classIn the main program, declare an array of N vehicles (N is entered from the keyboard). Enter information for each vehicle from the keyboard, then display the information of the most expensive car on the screen.


1
Expert's answer
2021-11-30T07:15:04-0500
#include <iostream>
#include <string>
using namespace std;

class Car {
public:
    Car() {}
    void Display() const;
    void SetSpeed(int speed) {
        this->speed = speed;
    }
    void SetBrand(string brand) {
        this->brand = brand;
    }
    void SetPrice(float price) {
        this->price = price;
    }

private:
    int speed;
    string brand;
    float price;    
};


void Car::Display() const {
    cout << "Brand: " << brand << endl;
    cout << "Speed: " << speed << endl;
    cout << "Price: " << price << endl;
}


int main() {
    const int MAX_N=100;
    Car cars[MAX_N];
    int n;
    int speed;
    string brand;
    float price, max_price=-1;
    int i_max;


    cout << "Enter N: ";
    cin >> n;
    if (n > MAX_N) {
        cout << "The value is too big, sorry" << endl;
        return 0;
    }


    for (int i=0; i<n; i++) {
        cout << "Enter information for " << i+1 << "th vehicle" << endl;
        cout << "Brand: ";
        cin >> brand;
        cout << "Speed: ";
        cin >> speed;
        cout << "Price: ";
        cin >> price;
        if (price > max_price) {
            max_price = price;
            i_max = i;
        }
        cars[i].SetBrand(brand);
        cars[i].SetSpeed(speed);
        cars[i].SetPrice(price);
    }

    cout << endl << "The more pricise car is" << endl;
    cars[i_max].Display();

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS