Pleae include both cpp and h files and I would be thankful if you could show the
ID: 3745361 • Letter: P
Question
Pleae include both cpp and h files and I would be thankful if you could show the output you got, please :)
And main. cpp:
#include "Vehicle.h"
#include "Showroom.h"
#include "Dealership.h"
#include <iostream>
#include <limits>
#include <memory>
#include <iomanip>
using namespace std;
void TestOne(Vehicle *v);
void TestTwo(Vehicle *v);
int main()
{
// Initialize some data. It's hard-coded here, but this data could come from a file, database, etc
Vehicle vehicles[] =
{
Vehicle("Ford", "Mustang", 1973, 9500, 113000),
Vehicle("Mazda", "CX-5", 2017, 24150, 5900),
Vehicle("Dodge", "Charger", 2016, 18955, 9018),
Vehicle("Telsa", "Model S", 2018, 74500, 31),
Vehicle("Toyota", "Prius", 2015, 17819, 22987),
Vehicle("Nissan", "Leaf", 2016, 12999, 16889),
Vehicle("Chevrolet", "Volt", 2015, 16994, 12558),
};
int testNum;
cin >> testNum;
if (testNum == 1)
TestOne(vehicles);
else if (testNum == 2)
TestTwo(vehicles);
return 0;
}
void TestOne(Vehicle *vehicles)
{
// Showrooms to store the vehicles
Showroom showroom("Primary Showroom", 3);
showroom.AddVehicle(&vehicles[0]);
showroom.AddVehicle(&vehicles[1]);
//showroom.AddVehicle(&vehicles[2]);
Showroom secondary("Fuel-Efficient Showroom", 4);
secondary.AddVehicle(&vehicles[3]);
secondary.AddVehicle(&vehicles[4]);
secondary.AddVehicle(&vehicles[5]);
secondary.AddVehicle(&vehicles[6]);
// A "parent" object to store the Showrooms
Dealership dealership("COP3503 Vehicle Emporium", 2);
dealership.AddShowroom(&showroom);
dealership.AddShowroom(&secondary);
dealership.ShowInventory();
}
void TestTwo(Vehicle *vehicles)
{
// Showrooms to store the vehicles
Showroom showroom("Primary Showroom", 3);
showroom.AddVehicle(&vehicles[0]);
showroom.AddVehicle(&vehicles[1]);
Showroom secondary("Fuel-Efficient Showroom", 4);
secondary.AddVehicle(&vehicles[4]);
secondary.AddVehicle(&vehicles[5]);
Showroom third("Fuel-Efficient Showroom", 4);
third.AddVehicle(&vehicles[3]);
// A "parent" object to store the Showrooms
Dealership dealership("COP3503 Vehicle Emporium", 3);
dealership.AddShowroom(&showroom);
dealership.AddShowroom(&secondary);
dealership.AddShowroom(&third);
cout << "Using just the GetAveragePrice() function ";
cout << "Average price of the cars in the dealership: $" << std::fixed << std::setprecision(2);
cout << dealership.GetAveragePrice();
}
Explanation / Answer
//main.cpp
#include "Vehicle.h"
#include "Showroom.h"
#include "Dealership.h"
#include <iostream>
#include <limits>
#include <memory>
using namespace std;
int main()
{ Vehicle veh;
// Initialize some data. It's hard-coded here, but this data could come from a file, database, etc
Vehicle vehicles[] =
{
Vehicle("Ford", "Mustang", 1973, 9500, 113000),
Vehicle("Mazda", "CX-5", 2017, 24150, 5900),
Vehicle("Dodge", "Charger", 2016, 18955, 9018),
Vehicle("Telsa", "Model S", 2018, 74500, 31),
Vehicle("Toyota", "Prius", 2015, 17819, 22987),
Vehicle("Nissan", "Leaf", 2016, 12999, 16889),
Vehicle("Chevrolet", "Volt", 2015, 16994, 12558),
};
// Showrooms to store the vehicles
Showroom showroom("Primary Showroom", 3);
showroom.AddVehicle(&vehicles[0]);
showroom.AddVehicle(&vehicles[1]);
showroom.getAvAlt();
Showroom secondary("Fuel-Efficient Showroom", 4);
secondary.AddVehicle(&vehicles[3]);
secondary.AddVehicle(&vehicles[4]);
secondary.AddVehicle(&vehicles[5]);
secondary.AddVehicle(&vehicles[6]);
// A "parent" object to store the Showrooms
Dealership dealerShip("COP3503 Vehicle Emporium", 2);
dealerShip.AddShowroom(&showroom);
dealerShip.AddShowroom(&secondary);
dealerShip.ShowInventory();
cout << endl << "Calling destructors..." << endl;
return 0;
}
----------------------------------------------------------------------------------------------------
//Dealership.cpp
#include "Dealership.h"
#include <iostream>
#include <iomanip>
Dealership::Dealership(std::string _dealer_name, const int _num_showroom):
m_dealer_name(std::move(_dealer_name)),
m_num_showroom(_num_showroom),
m_showroom_index(0),
m_total_car_price(0.0f),
m_total_car_count(0)
{
m_showrooms = new Showroom[m_num_showroom];
}
Dealership::Dealership(const Dealership& _other)
{
m_dealer_name = _other.m_dealer_name;
m_num_showroom = _other.m_num_showroom;
m_total_car_price = 0.0f;
m_total_car_count = 0;
m_showroom_index = _other.m_showroom_index;
m_showrooms = new Showroom[m_num_showroom];
for (unsigned int i = 0; i < m_showroom_index; i++)
m_showrooms[i] = _other.m_showrooms[i];
}
Dealership::Dealership(Dealership&& _other) noexcept
{
m_dealer_name = std::move(_other.m_dealer_name);
m_num_showroom = _other.m_num_showroom;
m_total_car_price = 0.0f;
m_total_car_count = 0;
m_showroom_index = _other.m_showroom_index;
m_showrooms = new Showroom[m_num_showroom];
for (unsigned int i = 0; i < m_showroom_index; i++)
m_showrooms[i] = _other.m_showrooms[i];
}
Dealership& Dealership::operator=(const Dealership& _other)
{
if (this == &_other)
return *this;
return *this;
}
Dealership& Dealership::operator=(Dealership&& _other) noexcept
{
if (this == &_other)
return *this;
return *this;
}
void Dealership::AddShowroom(const Showroom* _showroom)
{
if (_showroom != nullptr)
{
if (m_showroom_index < m_num_showroom)
{
Showroom* new_showroom = new Showroom(*_showroom);
m_showrooms[m_showroom_index++] = *new_showroom;
const Vehicle* vehicles = new_showroom->GetVehicleList();
if (vehicles != nullptr)
{
for (unsigned int i = 0; i < new_showroom->GetCount() && i < new_showroom->GetCapacity(); i++)
{
m_total_car_price += vehicles[i].GetPrice();
m_total_car_count++;
}
}
}
}
}
void Dealership::ShowInventory() const
{
std::cout << "Inventory of " << m_dealer_name << std::endl;
for (unsigned int x = 0; x < m_showroom_index; x++)
{
std::cout << "Vehicles in " << m_showrooms[x].GetName() << std::endl;
const Vehicle* vehicles = m_showrooms[x].GetVehicleList();
if (vehicles != nullptr)
{
for (unsigned int i = 0; i < m_showrooms[x].GetCount() && i < m_showrooms[x].GetCapacity(); i++)
{
vehicles[i].Display();
}
std::cout << std::endl;
}
}
std::cout << "Average car price: $" << std::setprecision(7) << GetAveragePrice() << std::endl;
}
float Dealership::GetAveragePrice() const
{
return (m_total_car_count == 0) ? 0.0f : (m_total_car_price / m_total_car_count);
}
Dealership::~Dealership()
{
delete[] m_showrooms;
std::cout << m_dealer_name << " destructor called." << std::endl;
}
---------------------------------------------------------------------------------------------------------------
//Dealership.h
#pragma once
#include <string>
#include "Showroom.h"
class Dealership
{
public:
Dealership(std::string _dealer_name, const int _num_showroom);
Dealership(const Dealership& _other);
Dealership(Dealership&& _other) noexcept;
Dealership& operator=(const Dealership& _other);
Dealership& operator=(Dealership&& _other) noexcept;
void AddShowroom(const Showroom* _showroom);
void ShowInventory() const;
float GetAveragePrice() const;
virtual ~Dealership();
private:
std::string m_dealer_name;
unsigned int m_num_showroom;
unsigned int m_showroom_index;
Showroom* m_showrooms;
mutable float m_total_car_price;
mutable int m_total_car_count;
};
--------------------------------------------------------------------------------------------------
//Showroom.cpp
#include "Showroom.h"
#include <iostream>
Showroom::Showroom() :
m_showroom_name(""),
m_vehicles(nullptr),
m_max_capacity(0),
m_cur_num_vehicle(0)
{
}
Showroom::Showroom(std::string _showroom_name, const int _max_capacity) :
m_showroom_name(std::move(_showroom_name)),
m_max_capacity(_max_capacity),
m_cur_num_vehicle(0)
{
m_vehicles = new Vehicle[m_max_capacity];
}
Showroom::Showroom(const Showroom& _other_showroom) :
m_showroom_name(_other_showroom.m_showroom_name),
m_max_capacity(_other_showroom.m_max_capacity),
m_cur_num_vehicle(_other_showroom.m_cur_num_vehicle)
{
m_vehicles = new Vehicle[m_max_capacity];
for (int i = 0; i < m_cur_num_vehicle; i++)
m_vehicles[i] = _other_showroom.m_vehicles[i];
}
Showroom::Showroom(Showroom&& _other_showroom) noexcept
{
m_showroom_name = std::move(_other_showroom.m_showroom_name);
m_max_capacity = _other_showroom.m_max_capacity;
m_cur_num_vehicle = _other_showroom.m_cur_num_vehicle;
m_vehicles = new Vehicle[m_max_capacity];
for (int i = 0; i < m_cur_num_vehicle; i++)
m_vehicles[i] = _other_showroom.m_vehicles[i];
}
Showroom& Showroom::operator=(const Showroom& _other_showroom)
{
m_showroom_name = _other_showroom.m_showroom_name;
m_max_capacity = _other_showroom.m_max_capacity;
m_cur_num_vehicle = _other_showroom.m_cur_num_vehicle;
m_vehicles = new Vehicle[m_max_capacity];
for (int i = 0; i < m_cur_num_vehicle; i++)
m_vehicles[i] = _other_showroom.m_vehicles[i];
return *this;
}
Showroom& Showroom::operator=(Showroom&& _other_showroom) noexcept
{
m_showroom_name = std::move(_other_showroom.m_showroom_name);
m_vehicles = _other_showroom.m_vehicles;
m_max_capacity = _other_showroom.m_max_capacity;
m_cur_num_vehicle = _other_showroom.m_cur_num_vehicle;
return *this;
}
void Showroom::AddVehicle(const Vehicle* _vehicle)
{
if (_vehicle != nullptr)
{
if (m_cur_num_vehicle < m_max_capacity)
{
Vehicle* new_vehicle = new Vehicle(*_vehicle);
m_vehicles[m_cur_num_vehicle++] = *new_vehicle;
}
}
}
void Showroom::ShowInventory() const
{
std::cout << "Show inventory " << m_showroom_name << std::endl;
}
const Vehicle* Showroom::GetVehicleList() const
{
return m_vehicles;
}
unsigned int Showroom::GetCapacity() const
{
return m_max_capacity;
}
unsigned int Showroom::GetCount() const
{
return m_cur_num_vehicle;
}
const char* Showroom::GetName() const
{
return m_showroom_name.c_str();
}
Showroom::~Showroom()
{
delete[] m_vehicles;
std::cout << GetName() << " destructor called." << std::endl;
}
------------------------------------------------------------------------------------------------
//Showroom.h
#pragma once
#include <string>
#include <vector>
#include "Vehicle.h"
class Showroom
{
public:
Showroom();
Showroom(std::string _showroom_name, int _max_capacity);
Showroom(const Showroom& _other_showroom);
Showroom(Showroom&& _other_showroom) noexcept;
Showroom& operator=(const Showroom& _other_showroom);
Showroom& operator=(Showroom&& _other_showroom) noexcept;
void AddVehicle(const Vehicle* _vehicle);
void ShowInventory() const;
const Vehicle* GetVehicleList() const;
unsigned int GetCapacity() const;
unsigned int GetCount() const;
const char* GetName() const;
virtual ~Showroom();
private:
std::string m_showroom_name;
Vehicle* m_vehicles;
int m_max_capacity;
int m_cur_num_vehicle;
};
-----------------------------------------------------------------------
//Vehicle.cpp
#include "Vehicle.h"
#include <algorithm>
#include <iostream>
#include <iomanip>
// Helper consts
static const std::string TAB = " ";
Vehicle::Vehicle() :
m_make(""),
m_model(""),
m_year(0),
m_price(0),
m_miles(0)
{
}
Vehicle::Vehicle(std::string _make,
std::string _model,
const int _year,
const int _price,
const int _miles) :
m_make(std::move(_make)),
m_model(std::move(_model)),
m_year(_year),
m_price(_price),
m_miles(_miles)
{
}
Vehicle::Vehicle(const Vehicle& _other_vehicle)
{
m_make = _other_vehicle.m_make;
m_model = _other_vehicle.m_model;
m_price = _other_vehicle.m_price;
m_year = _other_vehicle.m_year;
m_miles = _other_vehicle.m_miles;
}
Vehicle::Vehicle(Vehicle&& _other_vehicle) noexcept
{
m_make = std::move(_other_vehicle.m_make);
m_model = std::move(_other_vehicle.m_model);
m_price = _other_vehicle.m_price;
m_year = _other_vehicle.m_year;
m_miles = _other_vehicle.m_miles;
}
Vehicle& Vehicle::operator=(const Vehicle& _other_vehicle)
{
m_make = _other_vehicle.m_make;
m_model = _other_vehicle.m_model;
m_price = _other_vehicle.m_price;
m_year = _other_vehicle.m_year;
m_miles = _other_vehicle.m_miles;
return *this;
}
Vehicle& Vehicle::operator=(Vehicle&& _other_vehicle) noexcept
{
m_make = std::move(_other_vehicle.m_make);
m_model = std::move(_other_vehicle.m_model);
m_price = _other_vehicle.m_price;
m_year = _other_vehicle.m_year;
m_miles = _other_vehicle.m_miles;
return *this;
}
void Vehicle::Display() const
{
std::cout << GetYearMakeModel() << " " + TAB + "$" << static_cast<int>(GetPrice()) << TAB + std::to_string(m_miles) << std::endl;
}
std::string Vehicle::GetYearMakeModel() const
{
return std::to_string(m_year) + " " + TAB + m_make + " " + m_model;
}
float Vehicle::GetPrice() const
{
return m_price;
}
Vehicle::~Vehicle()
{
}
-------------------------------------------------------------------------------------------
//Vehicle.h
#pragma once
#include <string>
class Vehicle
{
public:
Vehicle();
Vehicle(std::string _make,
std::string _model,
const int _year,
const int _price,
const int _miles);
Vehicle(const Vehicle& _other_vehicle);
Vehicle(Vehicle&& _other_vehicle) noexcept;
Vehicle& operator=(const Vehicle& _other_vehicle);
Vehicle& operator=(Vehicle&& _other_vehicle) noexcept;
void Display() const;
std::string GetYearMakeModel() const;
float GetPrice() const;
virtual ~Vehicle();
private:
std::string m_make;
std::string m_model;
int m_year;
int m_price;
int m_miles;
};