copy constructor, car class

Okay so I posted my car.hpp file after the instructions. I have my Person class done and don't need help with that part. I am mostly just confused by 1) the setDriver method, am I getting input from user? I don't understand what the instructions are saying & hoe do I update the driver without creating a new driver? and 2) the copy constructor. I literally have no idea where to start with the setDriver method and did my best with the copy constructor. I also am not sure if I did the default constructer correctly. idk, any tips or tricks would be great lol. (& before anyone gets upset no you do not "need to do my homework for me", I am just simply asking for advice and your knowledge on these subjects as I have very, very little knowledge myself)



Define a Person class with the following class variables string firstName, lastName and address. The default constructor should set them all to the empty string. It should have setters and getters for each variable.

Create a Car class that has the private variables string make, string color, int year. It should also have driver that is a pointer to a Person. It should have a default constructor that creates a default person and sets the other variables to a Black, 1910, Ford.

You should setters and getters for all variables, including a setDriver method that takes a firstName, lastName, and Address and updates the driver to those values (note, this does not create a new driver, it simply updates the existing driver).

Finally, create an overloaded = operator (assignment), copy constructor, and << operator (insertion). The insertion operator should return “year color make driven by firstname space lastname of address”. The assignment and insertion should both be implemented as friends.
First test them with your own main program, after you have them using, se the provided driver to show that your classes properly work.


This is my car.hpp file, where most of the code is, this is all I have so far:

class Car
{
Person * driver;
std::string make;
std::string color;
int year;

public:
Car(std::string black, std::string Ford) : make(Ford), color(black), year(1910){driver = new Person;}
Car(const Car &other) : make(other.make), color(other.color), year(other.year){this->driver = new std::string();}
~Car();

void setMake(std::string make2){this->make = make2;}
void setColor(std::string color2){this->color = color2;}
void setYear(int year2){this->year = year2;}
void setDriver(){};

const std::string getMake(){return make;}
const std::string getColor(){return color;}
const int getYear(){return year;}

Car & operator=(Car const & other)
{
*(driver) = *(other.driver);
}

bool operator>(Car const & other)
{
return *(driver) > *(other.driver)
}

};
> 1) the setDriver method, am I getting input from user? I don't understand
> what the instructions are saying & hoe do I update the driver without
> creating a new driver?

> including a setDriver method that takes a firstName, lastName, and Address
> and updates the driver to those values

use the setters
1
2
3
4
5
//pseudocode
def setDriver(firstName, lastName, address):
	this.driver.set_firstName(firstName)
	this.driver.set_lastName(lastName)
	this.driver.set_address(address)

what do you pass to that function is up to you
some_car.setDriver("Null", "Nameless", "Under the bridge");


> 2) the copy constructor
first, ¿why a Car has a pointer to a Person?
I initially though that the idea was to have something like
1
2
3
4
5
Person teacher;
Car pinto;
pinto.setDriver(&teacher);
pinto.crash(); //the driver died, so now the teacher is dead
teacher.isDead(); //true 
but as how the Car creates a new Person it seems that's just added complexity for the sake of it. (you'll also need a proper destructor)

Now, look at your attempt.
1
2
3
4
5
6
7
Car(const Car &other) :
	make(other.make),
	color(other.color),
	year(other.year)
{
	this->driver = new std::string();
}
a driver is a pointer to a Person, ¿why are you doing new std::string() then?
just call the Person copy constructor
1
2
3
4
5
6
Car(const Car &other) :
	make(other.make),
	color(other.color),
	year(other.year),
	driver(new Person(*other.driver))
{}


> << operator (insertion)
this is << for printing, not < for comparison
outside the Car class define the function
1
2
3
4
5
6
7
8
std::ostream& operator<<(std::ostream &output, const Car &c){
	//example implementation
	if(c.getColor() == "black")
		output << "An invisible car";
	else
		output << "A " << c.getColor() << " car";
	return output;
}
Last edited on
Topic archived. No new replies allowed.