Question
C++ Description In this assignment you are to implement the methods of the class VehicleOwners . The class stores information about vehicles and their owners.
C++
Description
In this assignment you are to implement the methods of the class VehicleOwners. The class stores information about vehicles and their owners.
VehicleOwner class
This is the class that holds a single owner and vehicle information.
class VehicleOwner { public: string first = "na"; string last = "na"; int year = 1900; string manufacturer = "na"; string model = "na"; VehicleOwner() {} VehicleOwner(const string& first, const string& last, const int year, const string& manufacturer, const string& model) : first{first}, last{last}, year{year}, manufacturer{manufacturer}, model{model} {} string getName() const { return last + "," + first; } };
VehicleOwners class
This class holds an array of VehicleOwner objects and represents the collection used in this assignment.
class VehicleOwners { public: static const int MAX_SIZE = 100; VehicleOwner vehicleOwners[MAX_SIZE]; int length = 0; void append(const VehicleOwner& vehicleOwner); void appendFromFile(const string& filename); string toString() const; }; /* VehicleOwners stream operator */ ostream& operator <<(ostream& os, const VehicleOwners& o); /* VehicleOwner relational operators */ bool operator ==(const VehicleOwner& lhs, const VehicleOwner& rhs); bool operator !=(const VehicleOwner& lhs, const VehicleOwner& rhs); bool operator <(const VehicleOwner& lhs, const VehicleOwner& rhs); bool operator <=(const VehicleOwner& lhs, const VehicleOwner& rhs); bool operator >(const VehicleOwner& lhs, const VehicleOwner& rhs); bool operator >=(const VehicleOwner& lhs, const VehicleOwner& rhs);
Instructions
In VehicleOwners.cpp define the following methods:
append() method
The append() method inserts a new VehicleOwner instance to the end of the array. You may safely assume that there won't be more than MAX_SIZE instances appended.
void VehicleOwners::append(const VehicleOwner& vehicleOwner);
vehicleOwner - the instance to add to the end of the array.
appendFromFile() method
This method attempts to read from the specified file a number of VehicleOwner records. The method first tries to open the filename and if this attempt fails, gives an error message ("Error opening file.") and returns. On success it reads each record, builds a VehicleOwner object and appends it to the end of the array. Hint: Use the function stoi() to convert a string representation of an integer to its integer equivalent, e.g. stoi("15") returns 15.
void appendFromFile(const string& filename);
filename - the filename to read from.
descriptor
The descriptor method is responsible for returning a formatted string representation of the object.
string toString() const;
returns - a formatted string of objects or [] when the array is empty.
The returned string should contain multiple objects:
[ {Owner:first last, Vehicle:year manufacturer model}, ... {Owner:first last, Vehicle:year manufacturer model} ]
stream operator
This operator outputs to its stream the VehicleOwners object by calling its toString() method.
ostream& operator <<(ostream& os, const VehicleOwners& o);
os - the output stream to write the instance.
o - the instance to output to the stream.
relational operators
These operators each returns true or false based on the result of the conditional evaluation. Each condition is tested against the owner's name as returned by the method getName() of the VehicleOwner class.
bool operator ==(const VehicleOwner& lhs, const VehicleOwner& rhs); bool operator !=(const VehicleOwner& lhs, const VehicleOwner& rhs); bool operator <(const VehicleOwner& lhs, const VehicleOwner& rhs); bool operator <=(const VehicleOwner& lhs, const VehicleOwner& rhs); bool operator >(const VehicleOwner& lhs, const VehicleOwner& rhs); bool operator >=(const VehicleOwner& lhs, const VehicleOwner& rhs);
Example:
After the following code executes:
VehicleOwners vo{}; cout << vo << endl; vo.appendFromFile("sampleCSV.txt"); cout << vo;
the output will be:
[] [ {owner:John Doe, vehicle:2000,Chrysler,300}, {owner:Mary,Doe, vehicle:2020,Honda,Accord}, {owner:Mary,Smith, vehicle:2019,Toyota,Corolla} {owner:Peter,Pan, vehicle:2019,Ford,Fiesta} ]
VEHICLEOWNERS.H
#ifndef VEHICLE_OWNERS_H #define VEHICLE_OWNERS_H
#include
/* class: VehicleOwner * Stores single vehicle owner information. */ class VehicleOwner { public: string first = "na"; string last = "na"; int year = 1900; string manufacturer = "na"; string model = "na";
VehicleOwner() {} VehicleOwner(const string& first, const string& last, const int year, const string& manufacturer, const string& model) : first{first}, last{last}, year{year}, manufacturer{manufacturer}, model{model} {}
string getName() const { return last + "," + first; } };
/* class: VehicleOwners * Stores multiple vehicle owner information. */ class VehicleOwners { public: /* class level constant for array size. */ static const int MAX_SIZE = 100;
VehicleOwner vehicleOwners[MAX_SIZE]; int length = 0;
/* methods */ void append(const VehicleOwner& vehicleOwner); void appendFromFile(const string& filename);
/* descriptor */ string toString() const; };
/* VehicleOwners stream operator */ ostream& operator <<(ostream& os, const VehicleOwners& o);
/* VehicleOwner relational operators */ bool operator ==(const VehicleOwner& lhs, const VehicleOwner& rhs); bool operator !=(const VehicleOwner& lhs, const VehicleOwner& rhs); bool operator <(const VehicleOwner& lhs, const VehicleOwner& rhs); bool operator <=(const VehicleOwner& lhs, const VehicleOwner& rhs); bool operator >(const VehicleOwner& lhs, const VehicleOwner& rhs); bool operator >=(const VehicleOwner& lhs, const VehicleOwner& rhs);
#endif
VEHICLEOWNERS.CPP
#include "VehicleOwners.h" #include
void VehicleOwners::append(const VehicleOwner& vehicleOwner) { /* Type your code here. */ }
void VehicleOwners::appendFromFile(const string& filename) { /* Type your code here. */ }
/* descriptor */ string VehicleOwners::toString() const { /* Type your code here. */ }
/* Overloaded operators */ ostream& operator <<(ostream& os, const VehicleOwners& o) { /* Type your code here */ }
bool operator ==(const VehicleOwner& lhs, const VehicleOwner& rhs) { /* Type your code here */ }
bool operator !=(const VehicleOwner& lhs, const VehicleOwner& rhs) { /* Type your code here */ }
bool operator <(const VehicleOwner& lhs, const VehicleOwner& rhs) { /* Type your code here */ }
bool operator <=(const VehicleOwner& lhs, const VehicleOwner& rhs) { /* Type your code here */ }
bool operator >(const VehicleOwner& lhs, const VehicleOwner& rhs) { /* Type your code here */ }
bool operator >=(const VehicleOwner& lhs, const VehicleOwner& rhs) { /* Type your code here */ }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started