question archive MotorVehicle Module Design and code a class named MotorVehicle that holds information about a vehicle with an engine
Subject:SociologyPrice:4.89 Bought3
MotorVehicle Module
Design and code a class named MotorVehicle that holds information about a vehicle with an engine. Place your class definition in a header file named MotorVehicle.h and your function definitions in an implementation file named MotorVehicle.cpp.
Include in your solution all of the statements necessary for your code to compile under a standard C++ compiler and within the sdds namespace.
MotorVehicle Class
Design and code a class named MotorVehicle that holds information about a vehicle with an engine.
MotorVehicle Private Members
The class should be able to store the following data:
You can add any other private members in the class, as required by your design.
MotorVehicle Public Members
|[LICENSE_PLATE]| |[CURRENT_ADDRESS] ---> [NEW_ADDRESS]|<ENDL>
| [YEAR] | [PLATE] | [ADDRESS]
Built year: [USER TYPES HERE]
License plate: [USER TYPES HERE]
Current location: [USER TYPES HERE]
Helper Functions
Truck Module
Design and code a class named Truck that holds information about a motor vehicle that can carry cargo. Place your class definition in a header file named Truck.h and your function definitions in an implementation file named Truck.cpp.
Include in your solution all of the statements necessary for your code to compile under a standard C++ compiler and within the sdds namespace.
Truck Class
Design and code a class named Truck that holds information about a motor vehicle that can carry cargo. This class should inherit from MotorVehicle class.
Truck Private Members
The class should be able to store the following data (on top of data coming from the parent class):
You can add any other private members in the class, as required by your design. Do not duplicate members from the base class!
Truck Public Members
| [YEAR] | [PLATE] | [ADDRESS] | [CURRENT_CARGO]/[CAPACITY]
Built year: [USER TYPES HERE]
License plate: [USER TYPES HERE]
Current location: [USER TYPES HERE]
Capacity: [USER TYPES HERE]
Cargo: [USER TYPES HERE]
Helper Functions
main Module (supplied)
Do not modify this module! Look at the code and make sure you understand it.
Sample Output
----------------------------------------
|> T1: Vehicle
----------------------------------------
| 2010 | VVV-111 | Factory
| VVV-111| | Factory ---> Downtown Toronto |
| VVV-111| | Downtown Toronto ---> Mississauga |
| VVV-111| | Mississauga ---> North York |
| 2010 | VVV-111 | North York
----------------------------------------
|> T2: Read/Write
----------------------------------------
Built year: 2020
License plate: abc-111
Current location: Toronto
| 2020 | abc-111 | Toronto
----------------------------------------
|> T3: Truck
----------------------------------------
| T-1111| | Factory ---> Toronto HQ |
| T-1111| | Toronto HQ ---> Toronto Deposit |
Cargo loaded!
| 2015 | T-1111 | Toronto Deposit | 2345/5432
| T-1111| | Toronto Deposit ---> Montreal |
Cargo loaded!
| 2015 | T-1111 | Montreal | 5432/5432
| T-1111| | Montreal ---> New York |
Adding cargo failed!
| 2015 | T-1111 | New York | 5432/5432
| T-1111| | New York ---> New Jersey |
Cargo unloaded!
| 2015 | T-1111 | New Jersey | 0/5432
| T-1111| | New Jersey ---> Toronto |
Unloading cargo failed!
| 2015 | T-1111 | Toronto | 0/5432
----------------------------------------
|> T4: Read/Write
----------------------------------------
Built year: 2019
License plate: def-222
Current location: Montreal
Capacity: 2345
Cargo: 1234
| 2019 | def-222 | Montreal
| 2019 | def-222 | Montreal | 1234/2345
The code that I have done so far;
// MotorVehicle.h
#ifndef SDDS_MotorVehicle_H
#define SDDS_MotorVehicle_H
namespace sdds {
class MotorVehicle
{
char m_licensePlate[8];
char m_address[64];
int m_year;
public:
MotorVehicle(const char* plate, int year, const char* address = "Factory");
void moveTo(const char* address);
void licensePlate(const char* plate);
void address(const char* address);
void year(int year);
std::ostream& write(std::ostream& os) const;
std::istream& read(std::istream& in);
};
}
#endif // !SDDS_MotorVehicle_H
// MotorVehicle.cpp
#define CRT_SECURE_NO_WARINING
#include <cstring>
#include <iomanip>
#include <iostream>
#include "MotorVehicle.h"
using namespace std;
namespace sdds {
MotorVehicle::MotorVehicle(const char* plate, int year, const char* address)
{
//this->m_licensePlate(plate);
//this->m_address(address);
//this->m_year(year);
}
void MotorVehicle::moveTo(const char* address)
{
if (m_address != address)
{
cout << "|" << right << setw(8) << m_licensePlate;
cout << "| |" << right << setw(20) << m_address;
cout << "--->" << left << setw(20) << address << "|" << endl;
strcpy(m_address, address);
}
}
void MotorVehicle::licensePlate(const char* plate) {
strncpy(m_licensePlate, plate, 8);
m_licensePlate[8] = '';
}
void MotorVehicle::address(const char* address)
{
strncpy(m_address, address, 63);
m_address[63] = '';
}
void MotorVehicle::year(int year)
{
m_year = year;
}
std::ostream& MotorVehicle::write(std::ostream& os) const
{
// TODO: insert return statement here
}
}
// Truck.h
#ifndef SDDS_TRUCK_H
#define SDDS_TRUCK_H
#include <iostream>
#include "MotorVehicle.h"
namespace sdds {
class Truck : public MotorVehicle
{
float m_capacity;
float m_cargo;
public:
Truck(const char* plate, const int year, float capacity, const char* address);
bool addCargo(double cargo);
bool unload();
std::ostream& write(std::ostream& os) const;
};
}
#endif // !SDDS_TRUCK_H
// Truck.cpp
#include <iostream>
#include <cstring>
#include <iomanip>
#include "Truck.h"
using namespace std;
namespace sdds {
Truck::Truck(const char* plate, const int year, float capacity, const char* address)
{
m_capacity = capacity;
m_cargo = 0;
moveTo(address);
}
bool Truck::addCargo(double cargo)
{
bool add = false;
double newCargo = m_cargo + cargo;
if (m_cargo != m_capacity)
{
if (newCargo > m_capacity)
{
m_cargo = m_capacity;
add = true;
}
else
{
m_cargo = newCargo;
add = true;
}
}
return false;
}
bool Truck::unload()
{
bool unload = !(m_cargo == 0);
m_cargo = 0;
return unload;
}
std::ostream& Truck::write(std::ostream& os)const {
MotorVehicle::write(os);
os << " | " << m_cargo << "/" << m_capacity;
return os;
}
std::istream& Truck::read(std::istream& in) {
MotorVehicle::read(in);
std::cout << "Capacity: ";
in >> m_capacity;
std::cout << "Cargo: ";
in >> m_cargo;
return in;
}
std::ostream& operator<<(std::ostream& ostr, const Truck& righOper) {
righOper.write(ostr);
return ostr;
}
std::istream& operator>>(std::istream& istr, Truck& rightOper) {
rightOper.read(istr);
return istr;
}
}
//Main.cpp
// Workshop 7: Inheritance
// Version: 1.0
// Date: 2021-03-15
// Author: Cornel
// Revised: Fardad
/////////////////////////////////////////////
#include<iostream>
#include "Truck.h"
#include "Truck.h" // intentional
#include "MotorVehicle.h"
#include "MotorVehicle.h" // intentional
using namespace std;
using namespace sdds;
void printHeader(const char* title)
{
char oldFill = cout.fill('-');
cout.width(40);
cout << "" << endl;
cout << "|> " << title << endl;
cout.fill('-');
cout.width(40);
cout << "" << endl;
cout.fill(oldFill);
}
void moveAndLoad(Truck& aTruck, const char* destination, double cargo)
{
//cout << aTruck << endl;
aTruck.moveTo(destination);
if (aTruck.addCargo(cargo))
cout << "Cargo loaded!n";
else
cout << "Adding cargo failed!n";
cout << aTruck << endl << endl;
}
void moveAndUnload(Truck& aTruck, const char* destination)
{
//cout << aTruck << endl;
aTruck.moveTo(destination);
if (aTruck.unloadCargo())
cout << "Cargo unloaded!n";
else
cout << "Unloading cargo failed!n";
cout << aTruck << endl << endl;
}
int main()
{
{
printHeader("T1: Vehicle");
MotorVehicle aVehicle("VVV-111", 2010);
cout << aVehicle << endl << endl;
aVehicle.moveTo("Downtown Toronto");
aVehicle.moveTo("Mississauga");
aVehicle.moveTo("North York");
cout << endl << aVehicle << endl << endl;
printHeader("T2: Read/Write");
cin >> aVehicle;
cout << endl << aVehicle << endl << endl;
}
{
printHeader("T3: Truck");
Truck aTruck("T-1111", 2015, 5432, "Toronto HQ");
cout << endl;
moveAndLoad(aTruck, "Toronto Deposit", 2345);
moveAndLoad(aTruck, "Montreal", 3456);
moveAndLoad(aTruck, "New York", 4567);
moveAndUnload(aTruck, "New Jersey");
moveAndUnload(aTruck, "Toronto");
printHeader("T4: Read/Write");
cin >> aTruck;
cout << endl << (MotorVehicle)aTruck;
cout << endl << aTruck << endl << endl;
}
}
Looking forward to learning from your approach to this code and have ideas in upcoming lectures. Thank you.
Purchased 3 times