Create a base class called Vehicle that has the manufacturer\'s name (type strin
ID: 3834648 • Letter: C
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 below). 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 and a copy constructor. Write a driver program that tests all your methods. The definition of the class Person follows. Implement it.
class Person
{
public:
Person();
Person(string theName);
Person(const Person& theObject);
string getName() const;
private:
string name;
};
Please I need this solution in C++ not in Java.
thanks.
Explanation / Answer
#include <iostream>
using namespace std;
class Person
{
private:
string name;
public:
Person(){
name = " ";
}
Person(string theName){
name = theName;
}
string getName() const{
return name;
};
};
class vehicle{
private:
string manufacturerName;
int cylinders;
Person p;
public:
vehicle(string name, int c, Person p1){
p = p1;
manufacturerName = name;
cylinders = c;
}
string getManufacturerName(){
return manufacturerName;
}
void setManufacturerName(string name){
manufacturerName = name;
}
int getCylinders(){
return cylinders;
}
void setCylinders(int c){
cylinders = c;
}
void setPerson(Person p1){
p = p1;
}
};
class Truck: public vehicle{
private:
double loadCapacity;
int towingCapacity;
public :
Truck(string name, int cylinders, Person p1, int towing, double capacity ) : vehicle(name, cylinders,p1) {
setCylinders(cylinders);
setManufacturerName(name);
setLoadCapacity(capacity);
setTowingCapacity(towing);
setPerson(p1);
}
void setLoadCapacity(double d){
loadCapacity = d;
}
double getLoadCapacity(){
return loadCapacity;
}
void setTowingCapacity(int c){
towingCapacity = c;
}
int getTowingCapacity(){
return towingCapacity;
}
};
int main()
{
Person p("John");
Truck t("Toyota",10, p, 20, 30.0 );
cout<<" Person name is "<<p.getName();
cout<<" Towing capacity of truck is "<<t.getTowingCapacity();
cout<<" Manufacturer name is "<<t.getManufacturerName();
cout<<" Load Capacity is "<<t.getLoadCapacity();
cout<<" ";
cout<<" ";
return 0;
}