Language: C++ In my main file (main.cpp), the code includes int main(){ Vehicle
ID: 3743704 • Letter: L
Question
Language: C++
In my main file (main.cpp), the code includes
int main(){
Vehicle vehicles[] = { Vehicle("Toyota", "Camry", 2010, 20000, 31000), ......};
Showroom showroom ("Primary" , 3);
}
I already wrote the code for Vehicle.cpp and vehicle.h, now i need help writing the Showroom.cpp and showroom.h. The Showroom class has to store an array of Vehicle objects using dynamic memory allocation and needs to contain variables for the name of the showroom, a pointer to the array of vehicles, a maximum capacity of array, and count of how many vehicles it currently has. It also needs functions "void AddVehicle(const Vehicle *v);" to store vehicle in first available spot and "void ShowInventory() const;" to show the name and all of the vehicles in it. The accessors it should have are "const Vehicle *GetVehicleList() const;" , "unsigned int GetCapacity() const;" , "unsigned int GetCount() const;" , and "const char * GetName() const;"
Explanation / Answer
Hi, you can find the code for Showroom.h and Showroom.cpp below. Make sure to update the ShowInventory() to print whatever details you want to print regarding a particular Vehicle.In my case, I have only printed the name of the vehicle.
Showroom.h
//The reason for this macro is to prevent redeclaration of header files
#ifndef SHOWROOM_H
#define SHOWROOM_H
#include "Vehicle.h"
#include<string>
#include<iostream>
using namespace std;
class Showroom{
Vehicle *vehicles;
string showroom_name;
size_t max_capacity; // specifies the maximum possible capacity
size_t current_count; // refers to the current index at which vehicle can be added
public:
Showroom(string _showroom_name, size_t _max_capacity);
~Showroom();
void AddVehicle(const Vehicle *v);
void ShowInventory();
const Vehicle *GetVehicleList() const;
unsigned int GetCapacity() const; // returns the max capacity of a particular showroom
unsigned int GetCount() const; // returns the current number of vehicles in the showroom
const char * GetName() const; // return the name of the showroom
};
#endif
Showroom.cpp:
#include "Showroom.h"
Showroom::Showroom(string _showroom_name, size_t _max_capacity){
max_capacity = _max_capacity;
showroom_name = _showroom_name;
vehicles = new Vehicle[max_capacity]; // dynamic memory allocation
current_count = 0;
}
Showroom::~Showroom(){
delete []vehicles;
}
void Showroom::AddVehicle(const Vehicle *v){
if(current_count == max_capacity)
cout<<"capacity is full. cannot add more vehicles ";
else
vehicles[current_count++] = *v;
}
const Vehicle* Showroom::GetVehicleList() const{
return vehicles;
}
unsigned int Showroom::GetCapacity() const{
return max_capacity;
}
unsigned int Showroom::GetCount() const{
return current_count;
}
const char * Showroom::GetName() const{
return showroom_name.c_str();
}
void Showroom::ShowInventory(){
cout<<"Showroom Name : "<<showroom_name<<endl;
cout<<"Vehicles Information : "<<endl;
for(int i = 0; i < current_count; i++){
// At this point print whatever information you want to print with respect to a particular vehicle. At my end, I had create a Vehicle class with only name as a member variable
cout<<"Vehicle "<<std::to_string(i)<<" : "<<vehicles[i].getName()<<endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.