Having issues with C++ pointers and classes would appreciate some help This is a
ID: 3744008 • Letter: H
Question
Having issues with C++ pointers and classes would appreciate some help
This is an example of the expected use:
Showroom The Showroom class is a bit more sophisticated. Its purpose is to store an array of Vehicle objects. Each Showroom that you create could have a different number of Vehicles, so you will have to use dynamic memory allocation in this case. Your Showroom should contain variables for the following: The name of the Showroom A pointer to the array of Vehicles, and because pointers don't have any addition info on their own... A maximum capacity of the array A count of how many Vehicles you currently have · In addition, you should create the following functions (plus the special functions-a copy constructor, assignment operator, and destructor): // Store a Vehicle in the first available spot void AddVehicle (const Vehicle *v); // Show the name of this object and all of its vehicles void ShowInventory() const; // Accessors const Vehicle *GetVehicleList() const; unsigned int GetCapacity() const; unsigned int GetCount () const; const char GetName() const;Explanation / Answer
As there is no details about the type of Vehicle, for simlicity I assumed it to be int ,you change it to your own data type from the typedef I wrote in the beginning of the program.
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
typedef int Vehicle;
class Showroom{
private:
string name;
unsigned int maximum_size;
unsigned int count_of_vehicles;
Vehicle *V;
public:
void AddVehicle(const Vehicle *v);
void ShowInventory() const;
Showroom(string name, int maximum_size);
~Showroom();
const Vehicle *GetVehicleList() const;
unsigned int GetCapacity() const;
unsigned int GetCount() const;
const char* GetName() const;
};
Showroom::Showroom(string name, int maximum_size){
this->name = name;
this->maximum_size = maximum_size;
this->V = new Vehicle[maximum_size];
this->count_of_vehicles = 0;
}
void Showroom::ShowInventory() const{
for(unsigned int i =0;i<count_of_vehicles; i++){
cout << V[i] << endl;
}
}
Showroom::~Showroom(){
delete[] V;
}
void Showroom::AddVehicle(const Vehicle *v){
if(count_of_vehicles < maximum_size){
this->V[count_of_vehicles]= *v;
count_of_vehicles++;
}
else{
cout << "No space left" << endl;
}
}
const Vehicle * Showroom::GetVehicleList() const{
return V;
}
unsigned int Showroom::GetCapacity() const{
return maximum_size;
}
const char * Showroom::GetName() const{
return name.c_str();
}
unsigned int Showroom::GetCount() const {
return count_of_vehicles;
}
int main(){
int a[5];
for(int i =0;i<5;i++){
a[i]= i;
}
Showroom showroom("Primary showroom", 3);
showroom.AddVehicle(&a[0]);
showroom.AddVehicle(&a[1]);
cout <<showroom.GetCapacity() << endl;
cout<< showroom.GetCount() << endl;
cout << showroom.GetName() << endl;
return 0;
}
Output:
Ps. If you like this answer consider giving a thumbs up.
Happy Chegging!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.