//Vehicle.h #pragma once #ifndef VEHICLE_H #define VEHICLE_H #include<iostream>
ID: 3698383 • Letter: #
Question
//Vehicle.h
#pragma once
#ifndef VEHICLE_H
#define VEHICLE_H
#include<iostream>
#include "Person.h"
#include<string>
using namespace std;
class Vehicle
{
public:
//call the "Vehicle"
Vehicle(); // Default constructor
Vehicle(string the_name, string man_name, int num_cyl);
// Accessors
//call the "getManufacturer()" function
string getManufacturer() const;
//call the "getOwner()" function
string getOwner() const;
//call the "getCylinders()" function
int getCylinders() const;
//call the "setManufacturer()" function
// Setters/mutators
void setManufacturer(string man_name);
//call the "setOwner" function
void setOwner(Person owner);
//call the "setCylinders" function
void setCylinders(int num_cyl);
//call the friend function: Overload the << operator
friend ostream& operator <<(ostream& out_stream, const Vehicle& vehicle_object);
private:
//declaration
string man_name;
int num_cyl;
Person owner;
};
#endif // VEHICLE_H
//Vehicle.cpp
// vehicle.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include "Vehicle.h"
#include<string>
using namespace std;
// Default constructor: initialize the manufacture's name to "none",
// number of cylinders to 4, and the owner to a default Person
Vehicle::Vehicle()
{
man_name = "none";
num_cyl = 4;
Person p; // Calls default constructor of Person class
owner = p;
}
Vehicle::Vehicle(string the_name, string man_name, int num_cyl) :owner(the_name)
{
this->man_name = man_name;
this->num_cyl = num_cyl;
}
string Vehicle::getManufacturer() const
{
return man_name;
}
string Vehicle::getOwner() const
{
return owner.getName();
}
int Vehicle::getCylinders() const
{
return num_cyl;
}
void Vehicle::setManufacturer(string man_name)
{
this->man_name = man_name;
}
void Vehicle::setOwner(Person owner)
{
this->owner = owner;
}
void Vehicle::setCylinders(int num_cyl)
{
this->num_cyl = num_cyl;
}
ostream& operator <<(ostream& out_stream, const Vehicle& vehicle_object)
{
out_stream << "Owner Name : " << vehicle_object.owner
<< "Name of the Man: " << vehicle_object.man_name << endl
<< "Value of the cylinders: " << vehicle_object.num_cyl << endl
<< "=========================================================" << endl;
return out_stream;
}
//Person.h
#pragma once
#ifndef PERSON_H
#define PERSON_H
#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
Person();
Person(string the_name);
string getName() const;
void setName(string name);
friend ostream& operator <<(ostream& out_stream, const Person& person_object);
private:
string name;
};
#endif // PERSON_H
//Person.cpp
#include "stdafx.h"
#include<iostream>
#include<string>
#include "Person.h"
using namespace std;
Person::Person()
{
name = "none";
}
Person::Person(string the_name)
{
name = the_name;
}
string Person::getName() const
{
return name;
}
void Person::setName(string name)
{
this->name = name;
}
ostream& operator <<(ostream& out_stream, const Person& person_object)
{
out_stream << person_object.name << endl;
return out_stream;
}
//Truck.h
#pragma once
//Truck.h
#ifndef TRUCK_H
#define TRUCK_H
#include<iostream>
#include<string>
#include "Vehicle.h"
using namespace std;
class Truck :public Vehicle
{
public:
Truck();
Truck(string the_name, string man_name, int num_cyl, double load, int towing);
double getLoad() const;
int getTowing() const;
void setLoad(double load);
void setTowing(int towing);
friend ostream& operator <<(ostream& out_stream, const Truck& person_object);
private:
double load;
int towing;
Vehicle v;
};
#endif // TRUCK_H
//Truck.cpp
#include "stdafx.h"
#include<iostream>
#include<string>
#include "Person.h"
#include "Vehicle.h"
#include "Truck.h"
using namespace std;
Truck::Truck() :v("none", "none", 4)
{
}
Truck::Truck(string the_name, string man_name, int num_cyl, double load, int towing)
: v(the_name, man_name, num_cyl)
{
this->load = load;
this->towing = towing;
}
double Truck::getLoad() const
{
return load;
}
int Truck::getTowing() const
{
return towing;
}
void Truck::setLoad(double load)
{
this->load = load;
}
void Truck::setTowing(int towing)
{
this->towing = towing;
}
ostream& operator <<(ostream& out_stream, const Truck& truck_object)
{
out_stream << truck_object.v
<< "Load of the truck : " << truck_object.load << endl
<< "Towing of the truck: " << truck_object.towing << endl;
return out_stream;
}
//main.cpp
#include "stdafx.h"
#include<iostream>
#include "Person.h"
#include "Truck.h"
#include "Vehicle.h"
using namespace std;
int main()
{
// Test case 1: Create a Person objectwith name Carl
Person a("Carl");
cout << a;
cout << "========================================"
"=============" << endl;
// Create a Vehicle object with owner name = "Berk"
Vehicle b("Berk", "Honda", 3);
cout << b;
b.setOwner(a); // Set the owner name using person object
b.setCylinders(5);
b.setManufacturer("Toyota");
// Gets the name of the person using getName
//function in background
cout << "New Vehicale details " << endl;
cout << b;
Truck c("Berk", "Honda", 3, 3.3, 5);
cout << c;
// Update truck configuration
c.setTowing(8);
c.setLoad(7.5);
cout << "========================================"
"=============" << endl;
cout << "New Truck details" << endl;
cout << c;
system("pause");
return 0;
}
Explanation / Answer
Solution
string Vehicle::getOwner() const
{
return owner.getName();
}
replace this method such that it returns person object instead of string
Person Vehicle::getOwner() const
{
return owner;
}
Reason:
--The existing getOWner() method of vehicle class
string Vehicle::getOwner() const
{
return owner.getName();
}
returns the name of the owner(which is string object).
so the statement car.getOwner().getName() will be evaluated as
(car.getOwner()).getName() which will be equal to
string object.getName() [because car.getOwner() method returns the name of the owner of the car(string object) which is Carl in your case]
so here you are trying to call the getName() method on string object. since string class do not have any pre defined method getName(), you are getting the error.
--Now the new method getOwner() will be
Person Vehicle::getOwner() const
{
return owner;
}
it returns a Person object
so the statement car.getOwner().getName() will be
(car.getOwner()).getName() which is equal to
(Person object).getName()
since person class has getName() method it returns the name of the owner which is Carl.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.