Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please I really need help with the following C++ exercise. I don't know how to start. I would really appreciate the help. Thank you. Prompt:

Please I really need help with the following C++ exercise. I don't know how to start. I would really appreciate the help. Thank you.

Prompt:

You are now rich enough to buy all the bikes you want, but now you can have other vehicles too! But wait you need to store them in a garage. To do so, you will use the class Garage, that you will modify and implement. You will proceed to create different children of the Vehicle class through inheritance (these are Truck, Car, Bus, and Motorcycle), which will collectively represent every type of item with which you can populate a Garage object. For this project you will use separate compilation with g++ to link multiple classes into one executable, and, in order to successfully complete this project, you must understand the concept of dynamic memory allocation and inheritance.

Car Class:

Uses setGarageSpaces function to set garage space to 2. Uses setNumWheels function to set wheels to 4.

Bus Class:

Uses setGarageSpaces function to set garage space to 4. Uses setNumWheels function to set wheels to 8.

Motorcycle Class:

Uses setGarageSpaces function to set garage space to 1. Uses setNumWheels function to set wheels to 2.

Truck Class:

Uses setGarageSpaces function to set garage space to 8. Uses setNumWheels function to set wheels to 18.

Required files:

1- Vehicle.hpp

#ifndef VEHICLE_

#define VEHICLE_

#include

#include

class Vehicle

{

public:

//default constructor

//set name and manufacturer to ""

//set all numerical values to 0

Vehicle();

/**

Parameterized Constructor

@param name : name/model of said vehicle

@param manufacturer : the name of the manufacturer

@param topSpeed : a double letting us know the fastest speed of the vehicle in Miles Per Hour

@param weight : weight of the car pounds

@param milesPerGallon : how many miles your car gets on a single gallon

@param currentAmountGas : your vehicle's current gas capacity

*/

Vehicle(std::string name, std::string manufacturer, double top_speed, double weight, double mpg, double curr_gas_amt);

/**

given your vehicles top speed and @param float duration,

calculate the distance traveled during the specified time;

increment distance_traveled_ by this amount

@param float duration: time traveled

@return: updated distance_traveled_

*/

double travelMaxSpeed(float duration);

/**

Filling up a tank sets it to 1

@post : returns true if your tank has been filled and was not already full

*/

bool fillUpTank(); // x

double getTopSpeed() const; // returns top_speed_

size_t getWheels() const; // returns wheels_

double getWeight() const; // returns weight_

double getMilesPerGallon() const; // returns milesPerGallon_

size_t getGaragePositions() const; // returns garagePositions_

double getDistanceTraveled() const; // returns distance_traveled_

double getCurrentAmountGas() const; // return how much gas we currently have

std::string getName() const; // returns name_

std::string getManufacturer() const; // returns manufacturer_

void turn(float degrees); // adds to current direction

std::string getDirection(); // gets current direction of vehicle based on degrees

const size_t getSpaces() const; // returns garage_spaces_

const size_t getNumWheels() const; // returns num_wheels_

void setGarageSpaces(size_t spaces); // sets the amount of available garage spaces

void setNumWheels(size_t wheels); // sets the number of wheels the vehicle has

bool operator!=(const Vehicle &rhs) const;

bool operator==(const Vehicle &rhs) const; // Comparison operator overload

void operator=(const Vehicle &rhs); // Assignment operator overload

protected:

std::string manufacturer_;

std::string name_;

size_t wheels_;

double top_speed_;

double weight_;

double mpg_;

double curr_gas_amt_; // <- describe units that gas is measured in [0, 1] 0 <- empty, 1 <- full

double curr_direction_; // describes which direction (in degrees) vehicle is facing on a compass

double distance_traveled_; // how far you have traveled with your vehicle

size_t garage_spaces_; // number of garage slots this vehicle takes up

size_t num_wheels_;

}; // end Vehicle

#endif

2- Vehicle.cpp

#include "Vehicle.hpp" /* Constructors */ Vehicle::Vehicle() : name_{""}, manufacturer_{""}, top_speed_{0}, weight_{0}, mpg_{0}, curr_gas_amt_{1}, wheels_{0}, garage_spaces_{0}, num_wheels_{0} { } Vehicle::Vehicle(std::string name, std::string manufacturer, double top_speed, double weight, double mpg, double curr_gas_amt) : name_{name}, manufacturer_{manufacturer}, top_speed_{top_speed}, weight_{weight}, mpg_{mpg}, curr_gas_amt_{curr_gas_amt}, wheels_{0}, garage_spaces_{0}, num_wheels_{0} { } bool Vehicle::fillUpTank() { if (curr_gas_amt_ < 1) { curr_gas_amt_ = 1; return true; } return false; } void Vehicle::turn(float degrees) { curr_direction_ += degrees; while (curr_direction_ > 360) { curr_direction_ -= 360; } while (curr_direction_ < 0) { curr_direction_ += 360; } } std::string Vehicle::getDirection() { if (curr_direction_ == 0) { return "East"; } else if (curr_direction_ < 90) { return "Northeast"; } else if (curr_direction_ == 90) { return "North"; } else if (curr_direction_ < 180) { return "Northwest"; } else if (curr_direction_ == 180) { return "West"; } else if (curr_direction_ < 270) { return "Southwest"; } else if (curr_direction_ == 270) { return "South"; } else if (curr_direction_ < 360) { return "Southeast"; } else { return "Out of bounds"; } } double Vehicle::travelMaxSpeed(float duration) { if (duration <= 0) { return 0; } float curr_distance_traveled = top_speed_ * duration; distance_traveled_ += curr_distance_traveled; curr_gas_amt_ = (14 - curr_distance_traveled * mpg_) / 14; if (curr_gas_amt_ < 0) { curr_gas_amt_ = 0; } return curr_distance_traveled; } double Vehicle::getTopSpeed() const { return top_speed_; } double Vehicle::getCurrentAmountGas() const { return curr_gas_amt_; } double Vehicle::getDistanceTraveled() const { return distance_traveled_; } double Vehicle::getWeight() const { return weight_; } double Vehicle::getMilesPerGallon() const { return mpg_; } size_t Vehicle::getWheels() const { return wheels_; } std::string Vehicle::getName() const { return name_; } std::string Vehicle::getManufacturer() const { return manufacturer_; } /* comparison operator overload */ bool Vehicle::operator==(const Vehicle &rhs) const { return rhs.getName() == name_ && rhs.getManufacturer() == manufacturer_; } /* comparison operator overload */ bool Vehicle::operator!=(const Vehicle &rhs) const { return rhs.getName() != name_ || rhs.getManufacturer() != manufacturer_; } size_t Vehicle::getGaragePositions() const { return garage_spaces_; } /* assignment operator overload */ void Vehicle::operator=(const Vehicle &rhs) { name_ = rhs.name_; manufacturer_ = rhs.manufacturer_; top_speed_ = rhs.top_speed_; weight_ = rhs.weight_; mpg_ = rhs.mpg_; curr_gas_amt_ = rhs.curr_gas_amt_; } void Vehicle::setGarageSpaces(size_t spaces) { garage_spaces_ = spaces; } const size_t Vehicle::getNumWheels() const { return num_wheels_; } void Vehicle::setNumWheels(size_t wheels) { num_wheels_ = wheels; } const size_t Vehicle::getSpaces() const { return garage_spaces_; }

3- Garage.cpp

#include "Garage.hpp" #include "cmath" namespace null { Vehicle v; }

Task 1:

Define and implement the Car, Motorcycle, Bus, and Truck classes as inherited children of the Vehicle class. Lets get the easy ones out of the way! For each child class you want to call the parameterized constructor Vehicle(...) and use 0 for the current gas parameter.

Class Car must contain the following methods:

 /** Calls the parameterized constructor Vehicle(...) Uses setGarageSpaces function to set garage space to 2. Uses setNumWheels function to set wheels to 4. */ Car(std::string name, std::string manufacturer, double top_speed, double weight, double mpg); 

No private members to worry about here ;)

Class Motorcycle must contain the following methods:

 /** Calls the parameterized constructor Vehicle(...) Uses setGarageSpaces function to set garage space to 1. Uses setNumWheels function to set wheels to 2. */ Motorcycle(std::string name, std::string manufacturer, double top_speed, double weight, double mpg); 

Still no private members, pretty easy right? =P

Class Bus must contain the following methods:

 /** Calls the parameterized constructor Vehicle(...) Remember to set the number of seats. Uses setGarageSpaces function to set garage space to 4. Uses setNumWheels function to set wheels to 8. */ Bus(std::string name, std::string manufacturer, double top_speed, double weight, double mpg, size_t number_seats); /** returns the number of seats */ size_t getNumSeats() const; 

We have a private member variable now!

 double number_seats; 

Class Truck must contain the following methods:

 /** Calls the parameterized constructor Vehicle(...) Remember to set the cargo capacity. Uses setGarageSpaces function to set garage space to 8. Uses setNumWheels function to set wheels to 18. */ Truck(std::string name, std::string manufacturer, double top_speed, double weight, double mpg, double cargo_capacity); /** Add the weight of the cargo the current weight_of_held_cargo_ ONLY if it does not exceed the capacity. Return true if you manage to add the cargo successfully, otherwise return false. */ bool add_cargo(double weight_of_cargo); /** If weight_of_held_cargo_ isn't 0 then set it to 0, return true if you manage to clear it or else return false! */ bool clear(); /** return the cargo_capacity_ variable */ double getCargoCapacity() const; /** returns the weight_of_held_cargo_ variable */ double getHeldCargoWeight() const; 

That escalated quickly Here are your private variables:

 double cargo_capacity_; double weight_of_held_cargo_;

Task 2:

Define the Garage class in a file entitled Garage.hpp. As a preliminary, our concept of a garage is composed of an array that is filled from front to back. We consider each index in this array as a garage space, where different vehicle subclasses occupy a different number of garage spaces respectively.

Motorcycles occupy 1 space Cars occupy 2 spaces Busses occupy 4 spaces Trucks occupy 8 spaces

Implement the class in a file entitled Garage.cpp. You must include but are not limited to the following methods and members:

private: /** Changes the contents of arr_ to have all non-null vehicles strictly at the end of the array and all inserted vehicles at the beginning of the array. HINT: create a new array and repoint arr_; don't do this "in-place" */ void arrange(); /* a pointer to an array of vehicles - HINT: dynamically allocate the actual array */ Vehicle *arr_; /* the number of vehicle slots in the caller */ size_t capacity_; /* the number of occupied vehicle slots in the caller */ size_t num_vehicles_; public: /* Parameterized constructor with a default argument for capacity of 12 */ Garage(size_t capacity = 12); /** Inserts @param to_add into the number of spaces that correspond to its type into arr_[]; must insert from front to back As an example, if we had a garage that already contains an Audio R8 (a car) the default garage would have an Audio R8 object in both its first and second indeces. MUST INCREMENT num_vehicles_ MUST RETURN FALSE IF isFull() MUST RETURN FALSE IF num_vehicles_ + to_add.getSpaces() > capacity_ */ bool addVehicle(Vehicle to_add); /** Replaces any object in arr_ that == @param to_remove with null::v MUST RETURN FALSE IF num_vehicles_ == 0 MUST DECREMENT num_vehicles_ MUST CALL arrange() immediately before return statement */ bool removeVehicle(Vehicle to_remove); /** HINT: use what you already have! */ bool swapVehicles(Vehicle swap_in, Vehicle swap_out); bool isFull() const; /** Outputs the contents of the caller such that the manufacturer and vehicle name are only printed once per vehicle in arr_; for example if a garage object contains the aforementioned Audi R8 a call to display() will print Audi R8 even though arr_ contains the vehicle in two positions - also print each vehicle on its own line */ void display() const;

Submission:

You will submit the following files: Bus.cpp Bus.hpp Car.hpp Car.hpp Garage.cpp Garage.hpp Motorcycle.cpp Motorcycle.hpp Truck.cpp Truck.hpp

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Define procedural justice. How does that relate to unions?

Answered: 1 week ago