#include <iostream> #include <string> using namespace std; class Person { privat
ID: 3683235 • Letter: #
Question
#include <iostream>
#include <string>
using namespace std;
class Person
{
private:
string name; //Name of a person
public:
Person();
Person(string theName);
Person(const Person& theObject);
void setName(string theName);
string getName() const;
Person& operator=(const Person& rtSide);
friend istream& operator >>(istream& inStream, Person& personObject);
friend ostream& operator <<(ostream& outStream, Person& personObject);
};
//Constructors for Person class
Person::Person()
{
name = "";
}
Person::Person(string theName)
{
name = theName;
}
Person::Person(const Person& theName)
{
name = theName.getName();
}
//Person class functions
string Person::getName() const
{
return name;
}
//Person class operator overloading
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.getName();
return outStream;
}
class Vehicle
{
private:
string manuName; //Manufacturers Name
int numCyl; //Number of Cylinders in the Engine
Person owner; //The owner of the vehicle
public:
Vehicle();
Vehicle(string manufacturer, int cyl, const Person own);
Vehicle(string manufacturer, int cyl, string ownName);
Vehicle(const Vehicle& theVehicle);
Vehicle& operator =(const Vehicle& theVehicle);
string getManuName() const {return manuName;}; //Vehicle class functions
int getNumCyl() const {return numCyl;};
Person getOwner() const {return owner;};
};
//Vehicle class constructors
Vehicle::Vehicle()
{
manuName = "";
numCyl = 0;
owner = Person("No Owner");
}
Vehicle::Vehicle(string manufacturer, int cyl, const Person own)
{
manuName = manufacturer;
numCyl = cyl;
owner = own;
}
Vehicle::Vehicle(string manufacturer, int cyl, string ownName)
{
manuName = manufacturer;
numCyl = cyl;
owner = Person(ownName);
}
Vehicle::Vehicle(const Vehicle& theVehicle)
{
manuName = theVehicle.manuName;
numCyl = theVehicle.numCyl;
owner = theVehicle.owner;
}
class Truck : public Vehicle
{
private:
double loadCapacity;
int towingCapacity;
public:
Truck();
Truck(string name, int cylinders, Person truckOwner, double loadCap, int towingCap);
Truck(string name, int cylinders, string truckOwner, double loadCap, int towingCap);
Truck(const Truck& theTruck);
double getLoadCapacity() const { return loadCapacity; }; //Truck class functions
int getTowingCapacity() const { return towingCapacity; };
};
//Truck class constructors
Truck::Truck()
{
loadCapacity = 0.0;
towingCapacity = 0;
}
Truck::Truck(string name, int cylinders, Person truckOwner, double loadCap, int towingCap)
{
Vehicle(name, cylinders, truckOwner);
loadCapacity = loadCap;
towingCapacity = towingCap;
}
Truck::Truck(string name, int cylinders, string truckOwner, double loadCap, int towingCap)
{
Vehicle(name, cylinders, Person(truckOwner));
loadCapacity = loadCap;
towingCapacity = towingCap;
}
Truck::Truck(const Truck& theTruck)
{
Vehicle(theTruck.getManuName(), theTruck.getNumCyl(), theTruck.getOwner());
loadCapacity = theTruck.loadCapacity;
towingCapacity = theTruck.towingCapacity;
}
//Driving Test
int main()
{
Vehicle v1("Honda", 6, "Tom Sawyer");
cout << "Vehicle v1 Data: " << endl;
cout << "Manufacturer's Name: " << v1.getManuName() << endl;
cout << "Number of Cylinders: " << v1.getNumCyl() << endl;
cout << "Owner: " << v1.getOwner() << endl;
Person p1("Huckleberry Finn");
Vehicle v2("Ford", 6, p1);
cout << "Vehicle v2 Data: " << endl;
cout << "Manufacturer's Name: " << v2.getManuName() << endl;
cout << "Number of Cylinders: " << v2.getNumCyl() << endl;
cout << "Owner: " << v2.getOwner() << endl;
Truck t1("Chevrolet", 8, p1, 75.5, 20000);
cout << "Truck t1 Data: " << endl;
cout << "Manufacturer's Name: " << t1.getManuName() << endl;
cout << "Number of Cylinders: " << t1.getNumCyl() << endl;
cout << "Owner: " << t1.getOwner() << endl;
cout << "Load Capacity (Tons): " << t1.getLoadCapacity() << endl;
cout << "Towing Capacity (Pounds): " << t1.getTowingCapacity();
return (0);
}
When I compile and run my code it seems as though when creating a new Truck object it initializes it with the values from the no arg constructor instead of the constructor with the proper parameters. The output for manufacturer's name, number of cylinders, and name of the owner are set to "", 0, and "No Owner" respectively, where it should be "Chevrolet", 8, and "Huckleberry Finn." What am I doing wrongly?
If it helps, this is from Absolute C++ 5th Edition, Chapter 14, Programming Project 4. Chapter 14 is about inheritence.
Explanation / Answer
we need to first inherit the class person as well publicly by
class vehicle:public person
This will inherit the name of the person in the class vehicle as well as the class truck as both are publicly inherited.
This statement was missing .This will help to call the vehicle and person constructors(superclasses) within the constructor of truck class.As a result the arguements passed will be initialized accordingly related to the paratemetrized constuctors.The initialization stage will be as follows here when a particular object is created and arguement is passed to it.
Truck::Truck(string name, int cylinders, Person truckOwner, double loadCap, int towingCap)
{
Vehicle(name, cylinders, truckOwner); //calls vehicle constructor initalizing the three. and person constructor is called to know the struck owner
loadCapacity = loadCap;
towingCapacity = towingCap;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.