Code the car class indicated below: Car //name of class - string model //private
ID: 3866764 • Letter: C
Question
Code the car class indicated below:
Car //name of class
- string model //private member variables
- int year
- double price
- string color
+ Car() //public class functions
+ Car(string newModel, int newYear, string newColor)
+ void setPrice(double newPrice)
+ void setColor(string newColor)
+ void setYear(int newYear)
+ void setModel(int newModel)
+ double getPrice() const
+ string getColor() const
+ int getYear() const
+ string getModel() const
+ bool operator>(const Car& obj) const (Cars are compared by price only)
+ bool operator<(const Car& obj) const
+ void display() const
Code the main function:
Write an program for NiftyGMC Dealer, which keeps track of their lot inventory. Your program is to allow the user to perform any of of the following until he/she wishes to exit: (program is to use either a vector or an Car array)
1 – add a Car to the Car inventory (user provides info on Car)
2 - print out all Cars
3 - accept a price from the user. Print out all Cars that are that price
4 - print out the cheapest Car on the lot
5 – ask the user to enter a color . Print out all vehcles that are that color
6 – ask the user for a year . print out all vehicles that were made that year
7 - EXIT
Car //name of class
- string model //private member variables
- int year
- double price
- string color
+ Car() //public class functions
+ Car(string newModel, int newYear, string newColor)
+ void setPrice(double newPrice)
+ void setColor(string newColor)
+ void setYear(int newYear)
+ void setModel(int newModel)
+ double getPrice() const
+ string getColor() const
+ int getYear() const
+ string getModel() const
+ bool operator>(const Car& obj) const (Cars are compared by price only)
+ bool operator<(const Car& obj) const
+ void display() const
Explanation / Answer
#include <iostream>
#include <vector>
using namespace std;
class Car
{
private:
string model;
int year;
double price;
string color;
public:
Car()
{
}
Car(string newModel, int newYear, string newColor)
{
model = newModel;
year = newYear;
color = newColor;
}
void setPrice(double newPrice)
{
price = newPrice;
}
void setColor(string newColor)
{
color = newColor;
}
void setYear(int newYear)
{
year = newYear;
}
void setModel(string newModel)
{
model = newModel;
}
double getPrice() const
{
return price;
}
string getColor() const
{
return color;
}
int getYear() const
{
return year;
}
string getModel() const
{
return model;
}
bool operator>(const Car& obj) const
{
if(this->price > obj.price)
return true;
else
return false;
}
bool operator<(const Car& obj) const
{
if(this->price < obj.price)
return true;
else
return false;
}
void display() const
{
cout<<" Car : Model : "+model<<" Year : "<<year<<" Price : "<<price <<" Color : "<<color;
}
};
int main()
{
vector<Car> list; //declare vector list of type Car
int i,n,option;
string model,color;
int year;
double price,cheapest;
Car *c; //pointer to student object
// setup an iterator loop through the vector
vector<Car>::iterator it;
do
{
cout<<" 1 – add a Car to the Car inventory ";
cout<<" 2 - print out all Cars";
cout<<" 3 - accept a price from the user. Print out all Cars that are that price";
cout<<" 4 - print out the cheapest Car on the lot";
cout<<" 5 – ask the user to enter a color . Print out all vehcles that are that color";
cout<<" 6 – ask the user for a year . print out all vehicles that were made that year";
cout<<" 7 - EXIT";
cout<<" Enter your option : ";
cin>>option;
switch(option)
{
case 1: c = new Car();
cout<<" Enter model,year and price of car : ";
cin>>model>>year>>price>>color;
c->setModel(model);
c->setYear(year);
c->setPrice(price);
c->setColor(color);
list.push_back(*c); // push each object into the list
break;
case 2: for ( it = list.begin(); it != list.end(); ++it ) {
// For each Car, print out its info
it->display();
}
break;
case 3: cout<<" Enter price : ";
cin>>price;
for ( it = list.begin(); it != list.end(); ++it ) {
if(it->getPrice() == price)
it->display();
}
break;
case 4: cheapest = 99999;
for ( it = list.begin(); it != list.end(); ++it ) {
if(it->getPrice() < cheapest)
cheapest = it->getPrice();
}
cout<<" Cheapest car is of price : "<<cheapest;
break;
case 5:cout<<" Enter color : ";
cin>>color;
for ( it = list.begin(); it != list.end(); ++it ) {
if(it->getColor() == color)
it->display();
}
break;
case 6:cout<<" Enter year : ";
cin>>year;
for ( it = list.begin(); it != list.end(); ++it ) {
if(it->getYear() == year)
it->display();
}
break;
case 7: exit(0);
break;
default: cout<<"Invalid option";
break;
}
}while(option != 7);
return 0;
}
Output:
1 – add a Car to the Car inventory
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.