Create a base class called Vehicle that has the manufacturer\'s name (string), n
ID: 3756679 • Letter: C
Question
Create a base class called Vehicle that has the manufacturer's name (string), number of cylinders in the engine (int) and owner (type Person class). Then create a class called Truck that is derived from Vehicle and has additional properties: the load capacity in tons (double) and towing capacity in pounds (int). Be sure your classes have a reasonable complement of constructors, accessor and mutator member functions. Person class has a private member name (string) and the following public methods: no-arg constructor, constructor passing name as a string, getName returning string. Define Car class that is derived from the Vehicle class. Define a class called SportCar that is derived from Car class. Choose the appropriate member variables and methods. Write a driver program that test all your member functions.
Explanation / Answer
CODE TO COPY:
#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;
}