10.8 Program: Vehicles (C++) Create a base class called Vehicle that has as prop
ID: 3812891 • Letter: 1
Question
10.8 Program: Vehicles (C++)
Create a base class called Vehicle that has as properties:
the manufacturer’s name (type string),
number of cylinders in the engine (type int), and
owner (type Person, given below).
and methods:
getters and setters for all properties (getManufacturer, getOwner, getCylinders, setManufacturer, setOwner, getCylinders)
overloaded << operator
default constructor - initialize the manufacture's name to "none", number of cylinders to 4, and the owner to a default Person
parameterized constructor - passing a string owner name, string manufacturer name, and int cylinders. Use a constructor initialization list to initialize person object with the passed name like so:
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).
and methods:
getters and setters for all properties (getLoad, getTowing, setLoad, setTowing)
overloaded << operator
default constructor initializing properties to 0
parameterized constructor receiving 5 values.
The signature for the headers for the default and parameterized constructors need to following the below pattern:
The definition of the class Person is below. The implementation of the class is part of this programming project.
In main(), based on user input for the needed values, create an instance of a Car and a Truck object and test all your member functions as shown:
Using the parameterized constructor, Create a Vehicle object
Output the object using the overloaded << operator
Change each attribute of the object using appropriate setters
Output the object again to show the changes
Repeat the same process with a Truck object.
L
Lab Submission
10.8.1: Program: Vehicles
Instructions
Deliverables
Truck.cpp
Truck.h
Vehicle.h
main.cpp
Person.h
Vehicle.cpp
Person.cpp
We will expect the above file(s) to be submitted
Compile command
g++ Truck.cpp main.cpp Vehicle.cpp Person.cpp -o a.out
We will use this command to compile your code
Instructions
Deliverables
Truck.cpp
,Truck.h
,Vehicle.h
,main.cpp
,Person.h
,Vehicle.cpp
andPerson.cpp
We will expect the above file(s) to be submitted
Compile command
g++ Truck.cpp main.cpp Vehicle.cpp Person.cpp -o a.out
We will use this command to compile your code
Explanation / Answer
//Vehicle.h
#include"Person.h"
#include<string>
using namespace std;
class Vehicle
{
string manufacture_name;
int number_cylinders;
Person p;
public:
Vehicle();
Vehicle(string manufacture_name, int number_cylinders, Person p);
string getManufacturer();
Person getOwner();
int getCylinders();
void setManufacture(string m);
void setOwner(Person p1);
void setCylinders(int c);
friend ostream &operator<<(ostream &out, Vehicle &obj);
};
-----------------------------------
//Vehicle.cpp
#include"Vehicle.h"
Vehicle::Vehicle()
{
manufacture_name = "";
number_cylinders = 0;
p.setName("");
}
Vehicle::Vehicle(string manufacture_name, int number_cylinders, Person p)
{
manufacture_name = manufacture_name;
number_cylinders = number_cylinders;
p.setName(p.getName());
}
string Vehicle::getManufacturer()
{
return manufacture_name;
}
Person Vehicle::getOwner()
{
return p;
}
int Vehicle::getCylinders()
{
return number_cylinders;
}
void Vehicle::setManufacture(string m)
{
manufacture_name = m;
}
void Vehicle::setOwner(Person p1)
{
p = p1;
}
void Vehicle::setCylinders(int c)
{
number_cylinders = c;
}
ostream &operator<<(ostream &out, Vehicle &obj)
{
out << obj.getManufacturer() << " " << obj.getCylinders() << " " << obj.getOwner() << endl;
return out;
}
--------------------------------
//Person.h
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class Person
{
string name;
public:
Person();
Person(string name);
string getName() const;
void setName(string name);
friend ostream& operator <<(ostream& out_stream, const Person& person_object);
};
-----------------------------------------
//Person.cpp
#include"Person.h"
Person::Person()
{
name = " ";
}
Person::Person(string n)
{
name = n;
}
void Person::setName(string s)
{
name = s;
}
string Person::getName() const
{
return name;
}
ostream& operator <<(ostream& out, const Person& obj)
{
out <<obj.getName() << endl;
return out;
}
-------------------------------------------
//Truck.h
#include"Vehicle.h"
class Truck : public Vehicle
{
double load_capacity;
int towing_capacity;
public:
Truck();
Truck(string, int, Person p, double, int);
void set_load_capacity(double);
void set_towing_capacity(int);
double get_load_capacity();
int get_towing_capacity();
friend ostream& operator <<(ostream& out_stream, const Truck& obj);
};
--------------------------------
//Truck.cpp
#include"Truck.h"
Truck::Truck()
{
load_capacity = 0;
towing_capacity = 0;
Vehicle("", 0, Person(""));
}
Truck::Truck(string, int, Person p, double, int)
{
}
void Truck::set_load_capacity(double l)
{
load_capacity = l;
}
void Truck::set_towing_capacity(int c)
{
towing_capacity = c;
}
double Truck::get_load_capacity()
{
return load_capacity;
}
int Truck::get_towing_capacity()
{
return towing_capacity;
}
ostream& operator <<(ostream& out, Truck& obj)
{
out << obj.getManufacturer()<<" "<<obj.getCylinders()<<" "<<obj.getOwner()<<" "<<obj.get_load_capacity() << " " << obj.get_towing_capacity() << endl;
return out;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.