reate 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 have 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& operator >>(istream& inStream, Person& personObject);

friend ostream& operator <<(ostream& outStream, const Person&

personObject);

private:

string name;

};

Respuesta :

Answer:

Explanation:

class Person

{

public:

Person();

Person(string theName);

Person(const Person& theObject);

string get_name() const;

Person& operator=(const Person& rtSide);

friend istream& operator >>(istream& inStream,Person& personObject);

friend ostream& operator <<(ostream& outStream,Person& personObject);

private:

string name;

};

Person::Person()

{

 

}

Person::Person(string theName)

{

this->name=theName;

}

Person::Person(const Person& theObject)

{

name=theObject.name;

}

string Person::get_name() const

{

return name;

}

Person& Person::operator=(const Person& rtSide)

{

name=rtSide.name;

return *this;

}

istream& operator >>(istream& inStream,Person& personObject)

{

cout<<"Enter Person Name :";

getline(inStream,personObject.name);

return inStream;

}

ostream& operator <<(ostream& outStream,Person& personObject)

{

outStream<<"Name :"<<personObject.get_name()<<endl;

return outStream;

}

//================================

// Declaration of Vehicle Class

//================================

class Vehicle

{

public:

Vehicle();

Vehicle(string m, int cyl, Person p);

Vehicle(const Vehicle& theObject);

string getManufacturer() const;

int getCylinders() const;

Person getOwner() const;

void setManufacturer(string maker);

void setCylinders(int cylinders);

void setOwner (Person p);

void output();

// Outputs the data members of the class appropriately labeled

Vehicle& operator=(const Vehicle& rtSide);

private:

string manufacturer;

int numCylinders;

Person owner;

};

Vehicle::Vehicle()

{

 

}

Vehicle::Vehicle(string m, int cyl, Person p)

{

this->manufacturer=m;

this->numCylinders=cyl;

this->owner=p;

}

Vehicle::Vehicle(const Vehicle& theObject)

{

this->manufacturer=theObject.getManufacturer();

this->numCylinders=theObject.getCylinders();

this->owner=theObject.getOwner();

}

string Vehicle::getManufacturer() const

{

return manufacturer;

}

int Vehicle::getCylinders() const

{

return numCylinders;

}

Person Vehicle::getOwner() const

{

return owner;

}

void Vehicle::setManufacturer(string maker)

{

this->manufacturer=maker;

}

void Vehicle::setCylinders(int cylinders)

{

this->numCylinders=cylinders;

}

void Vehicle::setOwner (Person p)

{

this->owner=p;

}

void Vehicle::output()

{

cout<<"Person Name :"<<owner.get_name()<<endl;

cout<<"Manufacturer :"<<manufacturer<<endl;

cout<<"Number of Cylinders :"<<numCylinders<<endl;

}

// Outputs the data members of the class appropriately labeled

Vehicle& Vehicle::operator=(const Vehicle& rtSide)

{

owner=rtSide.owner;

manufacturer=rtSide.getManufacturer();

numCylinders=rtSide.getCylinders();

return *this;

}

//===============================

// Declaration of Truck Class

//===============================

class Truck : public Vehicle

{

public:

Truck();

Truck(string m, int cyl, Person p, double load, int tow);

Truck(const Truck& theObject);

double getLoadCapacity() const;

int getTowingCapacity() const;

void setLoadCapacity(double newLoad);

void setTowingCapacity(int newTowing);

void output();

// Outputs the data members appropriately labeled.

Truck& operator=(const Truck& rtSide);

private:

double loadCapacity;

int towingCapacity;

};

Truck::Truck()

{

 

}

Truck::Truck(string m, int cyl, Person p, double load, int tow):Vehicle(m,cyl,p)

{

this->loadCapacity=load;

this->towingCapacity=tow;

}

Truck::Truck(const Truck& theObject)

{

this->loadCapacity=theObject.loadCapacity;

this->towingCapacity=theObject.towingCapacity;

}

double Truck::getLoadCapacity() const

{

return loadCapacity;

}

int Truck::getTowingCapacity() const

{

return towingCapacity;

}

void Truck::setLoadCapacity(double newLoad)

{

this->loadCapacity=newLoad;

}

void Truck::setTowingCapacity(int newTowing)

{

this->towingCapacity=newTowing;

}

void Truck::output()

{

Vehicle::output();

cout<<"Load Capacity :"<<loadCapacity<<endl;

cout<<"Towing Capacity :"<<towingCapacity<<endl;

}

// Outputs the data members appropriately labeled.

Truck& Truck::operator=(const Truck& rtSide)

{

this->loadCapacity=rtSide.getLoadCapacity();

this->towingCapacity=rtSide.getTowingCapacity();

}

int main ()

{

Person p("James");

Vehicle v("Toyota",4,p);

cout<<"Displaying Vehicle Info:"<<endl;

v.output();

Person p1("Williams");

Truck t1("Maruthi",4,p1,4500,2500);

cout<<"\nDisplaying Truck Info:"<<endl;

t1.output();

return 0;