Need help with this assignment. create a base class called Vehicle that has the
ID: 3778298 • Letter: N
Question
Need help with this assignment.
create a base class called Vehicle that has the manufacturer's name (type string), number of cylinders in the engine (type int), and owner (type Person given in the code that follows). Then create a class called Truck that is derived from Vehicle and has additional properties, the load capacity in tons (type double since it may contain a fractional part) and towing capacity in pounds (type int). Be sure your classes habe a reasonable complement of constructors and accessor methods, an overloaded assignment operator, and a copy constructor. Write a driver program that tests all your methods.
The definition of the class Person follows. The implementation of the class is part of this programming project
class person
{
public:
Person();
Person(string theName);
Person(const Person&theObject);
string getName() const;
Person& operator=(const Person& rtSide);
friend istream& operatgor >>(istream& inStream,
Person& personObject);
friend ostream& operator <<(ostream& outStream,
const Person& personObject);
private:
string name;
};
Explanation / Answer
this is the code as per your requirement
#include <iostream>
#include <string>
using namespace std;
//Provided Person class.
class Person{
public:
Person();
Person(string theName);
Person(const Person& theObject);
Person(const char* theName); //allows const char[number] inputs
string getName() const;
Person& Person::operator =(const Person& rtSide);
friend istream& operator >>(istream& inStream,
Person& personObject);
friend ostream& operator <<(ostream& outStream,
const Person& personObject);
private:
string name;
};
//the Required Vehicle class.
class Vehicle{
public:
Vehicle();
Vehicle(string manuName, int numCyl, const Person& owner);
Vehicle(const Vehicle& theVehicle);
Vehicle& operator =(const Vehicle& theVehicle);
string getManuName();
int getNumCyl();
Person getOwner();
private:
string manuName;
int numCyl;
Person owner;
};
//the Required derived class Truck from base class Vehicle.
class Truck : public Vehicle{
public:
Truck();
Truck(double capTons, int capPounds, string manuName, int numCyl, const Person& owner);
Truck(const Truck& theTruck);
Truck& Truck::operator =(const Truck& theTruck);
double getCapTons();
int getCapPounds();
private:
double capTons;
int capPounds;
};
//Person Implementation
//Constructors
Person::Person() : name(""){}
Person::Person(string theName) : name(theName){}
Person::Person(const char* theName) : name(theName){}
Person::Person(const Person& theName): name(theName.name){} //Copy Constructor
//Overloaded assignment operator
Person& Person::operator =(const Person& thePerson){
this->name = thePerson.name;
return *this;
}
istream& operator >>(istream& inStream, Person& personObject){
inStream >> personObject.name;
return inStream;
}
ostream& operator <<(ostream& outStream, const Person& personObject){
outStream << personObject.name;
return outStream;
}
//Accessor methods
string Person::getName() const{
return name;
}
//Vehicle Implementation
//Constructors
Vehicle::Vehicle() : manuName("No Manufacturer"), numCyl(0), owner("No Owner"){}
Vehicle::Vehicle(string theManuName, int theNumCyl, const Person& theOwner)
: manuName(theManuName),
numCyl(theNumCyl),
owner(theOwner){}
Vehicle::Vehicle(const Vehicle& theVehicle)//Copy Constructor.
: manuName(theVehicle.manuName),
numCyl(theVehicle.numCyl),
owner(theVehicle.owner){}
//Overloaded Assignment Operator
Vehicle& Vehicle::operator =(const Vehicle& theVehicle){
this->manuName = theVehicle.manuName;
this->numCyl = theVehicle.numCyl;
this->owner = theVehicle.owner;
return *this;
}
//Accessor methods
string Vehicle::getManuName(){
return manuName;
}
int Vehicle::getNumCyl(){
return numCyl;
}
Person Vehicle::getOwner(){
return owner;
}
//Truck Implementation
//Constructors
Truck::Truck() : capTons(0.0), capPounds(0){}
Truck::Truck(double theCapTons, int theCapPounds, string theManuName, int theNumCyl, const Person& theOwner)
: Vehicle(theManuName, theNumCyl, theOwner),
capTons(theCapTons),
capPounds(theCapPounds){}
Truck::Truck(const Truck& theTruck) //Copy Constructor.
: Vehicle(theTruck),
capTons(theTruck.capTons),
capPounds(theTruck.capPounds){}
//Overloaded Assigment Operator
Truck& Truck::operator =(const Truck& theTruck){
Vehicle::operator =(theTruck);
capTons = theTruck.capTons;
capPounds = theTruck.capPounds;
return *this;
}
//Accessor methods
double Truck::getCapTons(){
return capTons;
}
int Truck::getCapPounds(){
return capPounds;
}
//Driver Program
int main(){
Vehicle v1("Honda", 4, "Brian Valle");
cout << "Vehicle v1 Data: " << endl;
cout << "Manufacturer's Name: " + v1.getManuName() << endl;
cout << "Number of Cylinders: " + v1.getNumCyl() << endl;
cout << "Owner: " + v1.getOwner() << endl;
Truck t1(72.0, 18000, "Ford", 8, "Brian Valle");
cout << "Truck t1 Data: " << endl;
cout << "Manufacturer's Name: " + t1.getManuName();
cout << "Number of Cylinders: " + t1.getNumCyl();
cout << "Owner: " + t1.getOwner() << endl;
cout << "Load Capacity (Tons): " + t1.getCapTons();
cout << "Towing Capacity (Pounds): " + t1.getCapPounds();
}