1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#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;
};
//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;
};
//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();
}
|