Starting from:

$24.99

COP3503 Lab 2 – Classes Solution


Overview
The purpose of this assignment is give you some experience writing classes in C++, the various special functions they make use of (such as copy constructors, assignment operators, and destructors), as well as an introduction to dynamically allocating memory within those classes.
New Keywords / Language concepts
• Classes – conceptually similar to other languages
• The std::vector class – similar to Java’s ArrayList class, an expandable container
• The std::string class – similar in many ways to strings in most every language
Description
This program will represent a hypothetical car dealership, which consists of showrooms that contain the vehicles for sale. To that end, there are three classes you will be writing:
• Vehicle
• Showroom
• Dealership
For this assignment, main.cpp will be provided for you, so you don’t have to worry about the structure of the program. Instead, you can focus solely on the structure of the classes and their interactions.
Vehicle
The Vehicle class is the basic container of this assignment. You will need to store the following data as private data members of the class:
• A std::string to store the make of the vehicle (such as Mazda, Toyota, etc)
• A std::string to store the model of the vehicle (such as Mustang, Model S, F-150, etc)
• An unsigned integer to store the year
• A float to store the price
• An unsigned integer to store the number of miles the vehicle has been driven
In addition to these data members, you should have the following public functions:

Default values and constructors
Make Model Year Price Mileage
“COP3503” “Rust Bucket” 1900 0 0

These defaults are chosen arbitrarily for this assignment—for your own projects, you can of course choose anything that you like.

Showroom
The Showroom class is a bit more sophisticated. Its purpose is to store a collection of Vehicle objects. Each Showroom that you create could have a different number of Vehicles (depending on its size), so for this assignment we’ll use a vector. Your Showroom should contain variables for the following:
• The name of the Showroom
• A vector<Vehicle> to store Vehicle objects
• A maximum capacity of the showroom (we don’t want to add Vehicles beyond this limit)
In addition, you should create the following functions:

Function Reference
Constructor Same as all constructors! Initialize class member variables
GetVehicleList Return the vector<Vehicle> objects, so other code can access it
AddVehicle If the Showroom is already full (numberOfVehicles == capacity), then you should print out "Showroom is full! Cannot add 2015 Mazda Miata" (this will use the GetYearMakeModel() function from the Vehicle class)

If there is space in the showroom, add the pass-in Vehicle to the class’ vector.
Vector objects can be expanded with the push_back() function.
ShowInventory Show all vehicles in the showroom, using the Display() function of each vehicle
GetInventoryValue Sum up the prices of all vehicles in the showroom and return that value


Dealership
The Dealership class in some ways is very similar to the Showroom. Instead of Vehicles, it will store a vector of Showroom objects. It will also need a name and a capacity. In addition, you will need functions:

Constructor Same as all constructors! Initialize class member variables
AddShowroom
If the Dealership is already full (numberOfShowrooms == capacity), then you should print out "Dealership is full, can't add another showroom!"

If there is space, add the passed-in object to the vector class member.
GetAveragePrice Here you will have to loop through all showrooms, and all vehicles in those showrooms, and get a final average price of all vehicles. (Remember: Average is total / number of values)
ShowInventory Show all showrooms stored in this dealership, one at a time, finally displaying the average price of each vehicle stored in the dealership (this sounds familiar…)

Relevant Reading
zyBooks chapters:
Strings
Arrays / Vectors (specifically the section on vectors)
Objects and Classes
User-Defined Functions (specifically the section on Pass by Reference)
Canvas->Pages->strings – a more in-depth look at strings
Tips
A few tips about this assignment:
• You can print out a tab character (the escape sequence ' ' ) to help line up the output.
• Don't try to tackle everything all at once. Work on one class at a time. Can't really have a
Dealership without a Showroom, which really needs Vehicles…
• An extension of that: work on one function, one class variable at a time. Create a constructor, initialize a single variable, test that out. When that works, move to the next part, and so on.

Example Output
Default constructor output
1900 COP3503 Rust Bucket $0.00 0
Unnamed Showroom is empty!
Generic Dealership is empty!
Average car price: $0.00

Showroom output and error message when Showroom is full
Showroom is full! Cannot add Dodge Caravan 1992
Vehicles in Example Showroom
2013 Chrysler Sebring $1819.00 22987
Dealership output and error message when Dealership is full
Dealership is full, can't add another showroom!
Vehicles in Room On e
1998 Dodge Neon $500.00 932018

Vehicles in Room Two
2001 Ford Escort $2000.00 125900
2004 Ford F-150 $500.00 7392

Average car price: $1000.00
Calling just the GetAveragePrice() function of the dealership
Using just the GetAveragePrice() function

Average price of the cars in the dealership: $19272.81

More products