SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Inheritance
Exercise:
Programming Projects
Question:4 | ISBN:9780132846813 | Edition: 5

Question

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 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;

};


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Complete Program:

// File: person.h

#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <string>
using std::string;
using std::istream;
using std::ostream;
using std::cout;
using std::endl;

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;
};
#endif
// File: person.cpp

#include "person.h"

Person::Person() : name("") 
{
	// deliberately empty
}

Person::Person(string theName) : name(theName)
{
	// deliberately empty
}

Person::Person(const Person& theObject) : name(theObject.name)
{
	// deliberately empty
}
	
string Person::getName() const
{
	return name;
}

Person& Person::operator=(const Person& rtSide)
{
	this->name = rtSide.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;
}
// File: vehicle.h

#ifndef VEHICLE_H
#define VEHICLE_H
#include "person.h"

class Vehicle 
{
public:
	Vehicle();
	Vehicle(const string& newManufacturerName, int newNumberOfCylinders, 
                const Person& newOwner);
 	Vehicle(const Vehicle& otherObject);
	Vehicle& operator=(const Vehicle& otherObject);
	string getManufacturerName();
	int getNumberOfCylinders();
	Person getOwner();
	void setManufacturerName(const string& newManufacturerName);
	void setNumberOfCylinders(int newNumberOfCylinders);
	void setOwner(const Person& newOwner);
	void print();

private:
	string manufacturerName;
	int numberOfCylinders;
	Person owner;
};
#endif
// File: vehicle.cpp

#include "vehicle.h"

Vehicle::Vehicle() : manufacturerName(""), numberOfCylinders(0), owner(Person())
{
	// deliberately empty
}

Vehicle::Vehicle(const string& newManufacturerName, int newNumberOfCylinders, 
                  const Person& newOwner)
	 : manufacturerName(newManufacturerName), numberOfCylinders(newNumberOfCylinders),     
                  owner(newOwner)
{
	// deliberately empty
}

Vehicle::Vehicle(const Vehicle& otherObject)
	: manufacturerName(otherObject.manufacturerName), 
           numberOfCylinders(otherObject.numberOfCylinders), owner(otherObject.owner)
{
	// deliberately empty
}

Vehicle& Vehicle::operator=(const Vehicle& otherObject)
{
	manufacturerName = otherObject.manufacturerName;
	numberOfCylinders = otherObject.numberOfCylinders;
	owner = otherObject.owner;

	return *this;
}

string Vehicle::getManufacturerName()
{
	return manufacturerName;
}

int Vehicle::getNumberOfCylinders()
{
	return numberOfCylinders;
}

Person Vehicle::getOwner()
{
	return owner;
}

void Vehicle::setManufacturerName(const string& newManufacturerName)
{
	manufacturerName = newManufacturerName;
}

void Vehicle::setNumberOfCylinders(int newNumberOfCylinders)
{
	numberOfCylinders = newNumberOfCylinders;
}

void Vehicle::setOwner(const Person& newOwner)
{
	owner = newOwner;
}

void Vehicle::print()
{
	cout << "Owner: " << owner << endl;
	cout << "Manufacturer's name: " << manufacturerName << endl;
	cout << "Number of cylinders in the engine: " << numberOfCylinders << endl;	
}
// File: truck.h

#ifndef TRUCK_H
#define TRUCK_H
#include "vehicle.h"

class Truck : public Vehicle 
{
public:
	Truck();
	Truck(string newManufacturerName, int newNumberOfCylinders, 
		const Person& newOwner, double newLoadCapacity, int newTowingCapacity);
	Truck(const Truck& otherObject);
	Truck& operator=(const Truck& otherObject);
	double getLoadCapacity();
	int getTowingCapacity();
	void setLoadCapacity(double newLoadCapacity);
	void setTowingCapacity(int newTowingCapacity);
	void print();

private:
	double loadCapacity;
	int towingCapacity;
};
#endif
// File: truck.cpp

#include "truck.h"

Truck::Truck() : Vehicle()
{
	// deliberately empty
}

Truck::Truck(string newManufacturerName, int newNumberOfCylinders, 
	const Person& newOwner, double newLoadCapacity, int newTowingCapacity)
	: Vehicle(newManufacturerName, newNumberOfCylinders, newOwner), 
	loadCapacity(newLoadCapacity), towingCapacity(newTowingCapacity)
{
	// deliberately empty
}

Truck::Truck(const Truck& otherObject) : Vehicle(otherObject), 
		loadCapacity(otherObject.loadCapacity), 
		towingCapacity(otherObject.towingCapacity)
{
	// deliberately empty
}

Truck& Truck::operator =(const Truck& otherObject)
{
	Vehicle:: operator =(otherObject);
	loadCapacity = otherObject.loadCapacity;
	towingCapacity = otherObject.towingCapacity;

	return *this;
}

double Truck::getLoadCapacity()
{
	return loadCapacity;
}

int Truck::getTowingCapacity()
{
	return towingCapacity;
}

void Truck::setLoadCapacity(double newLoadCapacity)
{
	loadCapacity = newLoadCapacity;
}

void Truck::setTowingCapacity(int newTowingCapacity)
{
	towingCapacity = newTowingCapacity;
}

void Truck::print()
{
	Vehicle::print();
	cout << "Load capacity in tons: " << loadCapacity << endl;
	cout << "Towing capacity in pounds: " << towingCapacity << endl;
}
// File: main.cpp

#include <iostream>
#include "truck.h"
using std::cout;
using std::endl;

int main()
{
	Person p1("James Smith");
	Truck t1("Blue Diamond Truck Company LLC", 6, p1, 25, 4000);

	Truck t2(t1);

	Truck t3 = t2;

	cout << "Details of Truck #1..." << endl;
	t1.print();

	cout << "\nDetails of Truck #2..." << endl;
	t2.print();

	cout << "\nDetails of Truck #3..." << endl;
	t3.print();
	
	return 0;
}

Sample Output:

0 0

Discussions

Post the discussion to improve the above solution.