need comments. #include \"vehicle.h\" #include \"functions.h\" #include <iostrea
ID: 3574021 • Letter: N
Question
need comments.
#include "vehicle.h"
#include "functions.h"
#include <iostream>
using namespace std;
int main(){
vector<Vehicle> cars;
while(1){
int ch;
cout << "Please enter your choices "
"1.Display Inventory "
"2.Add a vehicle "
"3.Update a vehicle "
"4.Delete a vehicle "
"5.Sort inventory by VIN "
"6.Search inventory by Model "
"7.Read inventory from file "
"8.Write inventory to file and exit ";
cin >> ch;
switch(ch)
{
case 1:
if(cars.size() == 0)
{
cout << "Invetory is empty" << endl;
}else
{
displayInventory(cars);
}
break;
case 2:
addVehicle(cars);
break;
case 3:
if(cars.size() == 0)
{
cout << "Invetory is empty" << endl;
}else
{
cout << "Enter the index to update" << endl;
updateVehicle(cars);
break;
}
case 4:
if(cars.size() == 0)
{
cout << "Invetory is empty" << endl;
}else
{
cout << "Enter index to delete a vehicle" << endl;
deleteVehicle(cars);
break;
}
case 5:
if(cars.size() == 0)
{
cout << "Invetory is empty" << endl;
}else
{
sortByVIN(cars);
break;
}
case 6:
searchByModel(cars);
break;
case 7:
readInventory(cars);
break;
case 8:
if(cars.size() == 0){
cout << "Inventory is empty to write ";
break;
}
writeInventoryToFile(cars);
return 0;
default:
cout << "please eneter 1-8 to use this program ";
}
}
return 0;
}
#include "vehicle.h"
void Dealer::setName(string name){
this->dealerName = name;
}
void Dealer::setAddress(string address){
this->dealerAddress = address;
}
string Dealer::getName(){
return this->dealerName;
}
string Dealer::getAddress(){
return this->dealerAddress;
}
string Vehicle::getVIN(){
return VIN;
}
string Vehicle::getMake(){
return make;
}
string Vehicle::getModel(){
return model;
}
int Vehicle::getYear(){
return year;
}
double Vehicle::getPrice(){
return price;
}
void Vehicle::setVIN(string VIN){
this->VIN = VIN;
}
void Vehicle::setMake(string make){
this->make = make;
}
void Vehicle::setModel(string model){
this->model = model;
}
void Vehicle::setYear(int year){
this->year = year;
}
void Vehicle::setPrice(double price){
this->price = price;
}
ostream & operator << (ostream& out, Vehicle car1)
{
out << "VIN: "<< car1.getVIN()<< " " << "Make: " << car1.getMake() << " " << "Model: " << car1.getModel() << " " << "Year: "<< car1.getYear() << " " << "Price: $" << car1.getPrice() << " "<< "Dealer Name: "<< car1.DealerPtr->getName() <<" " << "Dealer Address: " << car1.DealerPtr->getAddress() << endl;
return out;
}
#ifndef vehicle_h
#define vehicle_h
#include <string>
#include <fstream>
#include <ostream>
using namespace std;
class Dealer{
private:
string dealerName, dealerAddress;
public:
Dealer(){dealerName = "";dealerAddress = "";}
Dealer(string iName){dealerName = iName;dealerAddress = "";}
void setName(string);
void setAddress(string);
string getName();
string getAddress();
};
class Vehicle{
private:
string VIN, make, model;
int year;
double price;
public:
Dealer * DealerPtr;
Vehicle(){
VIN = "";
make = "";
model = "";
year = 0;
price = 0.0;
}
Vehicle(string iVIN, string iMake, string iModel, int iYear, double iPrice)
{
VIN = iVIN;
make = iMake;
model = iModel;
year = iYear;
price = iPrice;
}
string getVIN();
string getMake();
string getModel();
int getYear();
double getPrice();
void setVIN(string);
void setMake(string);
void setModel(string);
void setYear(int);
void setPrice(double);
friend ostream & operator << (ostream &, Vehicle );
};
#endif
Explanation / Answer
Dear student,
Here is the above program with comments...
program..
#include "vehicle.h" // including vehicle.h header file which is user defined header file
#include "functions.h" // including functions.h header file which is also a user defined header file
#include <iostream> // including standard header file
using namespace std; // namespace declaration
int main( // main function calling)
{
vector<Vehicle> cars; // A vector name cars declartion which stores the information of cars
while(1){
int ch;
cout << "Please enter your choices "
"1.Display Inventory "
"2.Add a vehicle " // A Menu for choice selection
"3.Update a vehicle "
"4.Delete a vehicle "
"5.Sort inventory by VIN "
"6.Search inventory by Model "
"7.Read inventory from file "
"8.Write inventory to file and exit ";
cin >> ch;
switch(ch) // switch to case according to the given user input.
{
case 1:
if(cars.size() == 0)
{
cout << "Invetory is empty" << endl;
}else
{
displayInventory(cars); // Display Inventory function calling
}
break;
case 2:
addVehicle(cars); // addVehicle function calling
break;
case 3:
if(cars.size() == 0)
{
cout << "Invetory is empty" << endl;
}else
{
cout << "Enter the index to update" << endl;
updateVehicle(cars); // Update Vehicel function calling
break;
}
case 4:
if(cars.size() == 0)
{
cout << "Invetory is empty" << endl;
}else
{
cout << "Enter index to delete a vehicle" << endl;
deleteVehicle(cars); // Delete Vehicel function calling
break;
}
case 5:
if(cars.size() == 0)
{
cout << "Invetory is empty" << endl;
}else
{
sortByVIN(cars); // SortByVIN function calling
break;
}
case 6:
searchByModel(cars); // SearchByModel function calling
break;
case 7:
readInventory(cars); //readInventory function calling
break;
case 8:
if(cars.size() == 0){
cout << "Inventory is empty to write ";
break;
}
writeInventoryToFile(cars); // writeInventory function calling
return 0;
default:
cout << "please eneter 1-8 to use this program ";
}
}
return 0;
}
#include "vehicle.h"
// SetName function defination outside of class Dealer
void Dealer::setName(string name){
this->dealerName = name; //this’ pointer is a constant pointer that holds the memory address of the current object.
//If you declare a local variable in a method with the same name as an existing member,
//you will have to use this->var to access the class member instead of the local variable.
}
// setAddress function defination outside of class Dealer
void Dealer::setAddress(string address){
this->dealerAddress = address; // this pointer
}
// getName function Defination outside of class Dealer
string Dealer::getName(){
return this->dealerName; // return this pointer
}
// getAddress function Defination outside of class Dealer
string Dealer::getAddress(){
return this->dealerAddress; // return this pointer
}
// getVIN function Defination outside of class Vehicle
string Vehicle::getVIN(){
return VIN; // return VIN.
}
// getMake function Defination outside of class Vehicle
string Vehicle::getMake(){
return make; // return make
}
// getModel function Defination outside of class Vehicle
string Vehicle::getModel(){
return model; // return model
}
// getYear function Defination outside of class Vehicle
int Vehicle::getYear(){
return year; // return year
}
// getPrice function Defination outside of class Vehicle
double Vehicle::getPrice(){
return price; // return price
}
// setVIN function Defination outside of class Vehicle
void Vehicle::setVIN(string VIN){
this->VIN = VIN; // this pointer
}
// setMake function Defination outside of class Vehicle
void Vehicle::setMake(string make){
this->make = make; // this pointer
}
// setModel function Defination outside of class Vehicle
void Vehicle::setModel(string model){
this->model = model; // this pointer
}
// setYear function Defination outside of class Vehicle
void Vehicle::setYear(int year){
this->year = year; // this pointer
}
// setPrice function Defination outside of class Vehicle
void Vehicle::setPrice(double price){
this->price = price; // this pointer
}
/*The operator << applied to an output stream is known as insertion operator,
and performs formatted output: here Vehicle class memebers are inserted into out stream.*/
ostream & operator << (ostream& out, Vehicle car1)
{
out << "VIN: "<< car1.getVIN()<< " " << "Make: " << car1.getMake() << " " << "Model: " << car1.getModel() << " " << "Year: "<< car1.getYear() << " " << "Price: $" << car1.getPrice() << " "<< "Dealer Name: "<< car1.DealerPtr->getName() <<" " << "Dealer Address: " << car1.DealerPtr->getAddress() << endl;
return out;
}
// Here you are creating a header file name as vehicle.h which you will use in your program...
#ifndef vehicle_h // if not define the vehicle_h
#define vehicle_h // define vehicle_h
#include <string>
// Including of standard heder file
#include <fstream>
#include <ostream>
// std name space declaration
using namespace std;
// Dealer class declaration
class Dealer{
private:
string dealerName, dealerAddress; // Private class members declaration
public:
/* Dealer class Default Constructor (A constructor without any arguments or with
default value for every argument, is said to be default constructor.*/
Dealer()
{
dealerName = "";
dealerAddress = "";
}
/* Dealer Constructor(A constructor is a member function of a class which initializes
objects of a class.)*/
Dealer(string iName)
{
dealerName = iName;
dealerAddress = "";
}
void setName(string);
void setAddress(string); // Class Member function declaration
string getName();
string getAddress();
};
class Vehicle{ // Vehical class declaration
private:
string VIN, make, model; // Vehicle class private members declaration
int year;
double price;
public:
Dealer * DealerPtr; // Vehicle class publicle memebrs declaration
/* Vehicle class Default Constructor (A constructor without any arguments or with
default value for every argument, is said to be default constructor.*/
Vehicle()
{
VIN = "";
make = "";
model = "";
year = 0;
price = 0.0;
}
/* Vehicle Constructor(A constructor is a member function of a class which initializes
objects of a class.)*/
Vehicle(string iVIN, string iMake, string iModel, int iYear, double iPrice)
{
VIN = iVIN;
make = iMake;
model = iModel;
year = iYear;
price = iPrice;
}
// Vehicle class Member functions declaration
string getVIN();
string getMake();
string getModel();
int getYear();
double getPrice();
void setVIN(string);
void setMake(string);
void setModel(string);
void setYear(int);
void setPrice(double);
friend ostream & operator << (ostream &, Vehicle ); // friend function calling to access the private members of the vehicle class
};
#endif // end of defination
Kindly Check and Verify.... Thanks...!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.