0

Instructions:

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.

I posted my car.hpp file below. I have my Person class done, I 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. How do I update the driver without creating a new driver?

  2. the copy constructor. I literally have no idea where to start with the setDriver() method. I did my best with the copy constructor. I also am not sure if I did the default constructer correctly.

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.

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)
    }
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
KSky
  • 1
  • 1
  • 2
  • Talk to your teaching staff. There are misconceptions upon misconceptions here and too much to disentangle. You need to go back through your previous assignments and ask yourself if you understand those. Talk to your teaching staff. – JohnFilleau Mar 04 '21 at 05:18

2 Answers2

0

the setDriver() method, am I getting input from user? I don't understand what the instructions are saying. How do I update the driver without creating a new driver?

The answer is literally in the instructions:

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).

the copy constructor. I literally have no idea where to start

Try something more like this:

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

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

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

    void setDriver(std::string firstName2, std::string lastName2, std::string address2){
        driver->setFirstName(firstName2);
        driver->setLastName(lastName2);
        driver->setAddress(address2);
    }

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

    Car& operator=(const Car &other)
    {
        *driver = *(other.driver);
        make = other.make;
        color = other.color;
        year = other.year;
        return *this;
    }

    bool operator>(const Car &other) const
    {
        return *driver > *(other.driver);
    }
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thank you! The setDriver makes so much sense now, I think when I have program assignments back to back my brain sometimes just stops working lol – KSky Mar 04 '21 at 20:33
0

this is actually very similar to an assignment I had recently. What my professor wanted me to do is to use take in the arguments through the parameter of the setDriver() function, so it would make sense to add parameters in your setDriver() function. It is not clear how your teacher want the data to be entered, but to just update the data of a driver you will have to find the index of the driver in the array you want to update. You can look for a specific driver by searching doing a linear search through the array.

int Car::FindName(char* aName) const  
{
        for (int i = 0; i < currentSize; i++)   // Look at each entry.
            if (strcmp(list[i].GetName(), aName) == 0)
            return i;       // If found, return position and exit.
    
            return -1;              // Return -1 if never found.
 }

if the driver was found you can just update the spot by: (yourpointer)[index].set();

I assume you have a set function in your driver class.