Vector

Hi there

I am doing exercise where I have class Car and class Garage.
I want to create vector of cars, that will be stored in Garage class.
In order to "move cars in and out" of the garage I want to use pointer or reference. But I am not sure if my implementation is correct.

I am finding difficult few things.
1. if my implementation of the size of vector is correct? I have tried to use .push_back; but I failed at that, so I have create this as in my code.
2. How to move object car to garage? I am lost here. suggestion how this should be implemented would be welcomed :)
3. How to allow borrowing of the card and returning of the car could be implemented?
4. is there a way I can add cars to garage with use of .push_back()?

thanks :)

if there are questions I will try to be more specific if my explanation is to vague.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <vector>

class Car{
private:
    std::string brand;
    int doors;
    double power;

public:
    Car(): doors(0), brand("a"), power(0.0)
    {}
    void add(int ds, std::string bd, double pr){

        brand = bd;
        doors = ds;
        power = pr;
    }
    void print()
    {
        std::cout << brand << std::endl;
        std::cout << doors << std::endl;
        std::cout << power << std::endl;
        std::cout << "------------------------" << std::endl;
    }

};
class Garage{

    std::vector<Car>Garage;
public:

    void add(Car* brum)
    {
        Garage.push_back(*brum);
    }
    void print()
    {
        for (size_t i = 0; i < Garage.size(); ++i) {
            Garage.at(i).print();
        }
    }
    void pop(Car* brum)
    {
        Garage.pop_back();
    }
};

int main() {

    std::cout << "How many cards do you want to add?";

    unsigned int howManyCars{0};
    std::cin >> howManyCars;
    std::vector<Car>car(howManyCars);
    std::vector<Garage>garage(howManyCars);

    std::cout << car.size() << std::endl;

    int doorsM{0};
    std::string brandM{" "};
    double powerM{0.0};
    Car* pCar = &car[0];


    for (size_t i = 0; i < howManyCars; ++i) {

        std::cout << "Make: ";
        std::cin >> brandM;
        std::cout << "How many doors: ";
        std::cin >> doorsM;
        std::cout << "How many MPH: ";
        std::cin >> powerM;
        car.at(i).add(doorsM, brandM, powerM);
        garage.at(i).add(pCar);
    }

    

    return 0;
}
Last edited on
First of all, your garage class holds a vector of Cars, not a vector of pointers to cars as you wanted. You can fix this easily by changing:
1
2
3
4
5
6
7
std::vector<Car>Garage;
//to
std::vector<Car*>Garage;
//and
Garage.push_back(*brum);
//to
Garage.push_back(brum);

Your size is fine, though push_back should work fine, why didn't it work?
Moving a car from one garage to the other shouldn't be difficult either, but you have to keep in mind that you will always move the last car of a garage or you will have to specify an index. A possible implementation could be (I specified an index):
1
2
3
4
5
6
7
8
9
10
11
void moveCar(int index, Garage destination)
{
    if (index > Garage.size()-1) //-1 because max index of a garage with 5 cars is 4
    {
        std::cout << "Error, car not found!\n"; //throw an error
        return;
    }
    Car temp = Garage[i]; //create a temporary copy of the car
    Garage.erase(i,1); //erases 1 element from the garage, starting from index i;
    destination.add(&temp); //add the car to destination garage
}

To use this just do the following in your main function:
 
garage[0].moveCar(1,garage[1]);

This moves the car at index 1 from garage 0 to garage 1.
For the borrowing, you could make moveCar return an integer, the last index of the destination garage, when you can can just call moveCar again with that return value as index to be moved.

I hope this answers all your questions.
Line 56: You're creating a vector of Garages. Each Garage contains a vector of Cars.

Line 6,13: You use std::string, but don't include the <string> header.

Line 30: Not best practice to name your member variable the same as your class. The compiler knows the difference, but it is confusing to the reader.

Line 43: Why are you passing a pointer to a Car? You don't use it.

Line 75: You're always adding car[0]. You never change pCar.

Line 75: Because garage is a vector of garages, you adding a car in the i'th garage.



Hi

thank you very much for the suggestions, very useful!!
@goldenchicken - I changed my code

std::vector<Car*>Garage;
but second line didn't work as you advised I had to add &
garage.push_back(&brum);

I have to get to move car out ( Iwill try to wokr on this tonight :)

@AbstractionAnon - vey usfull advise thank you :)
line 56 I am wondering if I shuold then just have object (not vector) garage? and inside store vector of cars? does this make sense?

line 6,13 - I was wondering about this... but for whatever the reason CLion (my IDE) did't mind this. I have added include string.

line 30 - I have renamed Garage to garage1

Line 75 - I was just wondering how I could iterate trough pointer can I just add next line pCar++; ?

updated code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <vector>
#include <string>

class Car{
private:
    std::string brand;
    int doors;
    double power;

public:
    Car(): doors(0), brand("a"), power(0.0)
    {}
    void add(int ds, std::string bd, double pr){

        brand = bd;
        doors = ds;
        power = pr;
    }
    void print()
    {
        std::cout << brand << std::endl;
        std::cout << doors << std::endl;
        std::cout << power << std::endl;
        std::cout << "------------------------" << std::endl;
    }

};
class Garage{

    std::vector<Car*>garage1;
public:

    void add(Car brum)
    {
        garage1.push_back(&brum);
    }
    void print()
    {
        for (size_t i = 0; i < garage1.size(); ++i) {
            garage1.at(i)->print();
        }
    }
    void pop(Car brum)
    {
        garage1.pop_back();
    }
};

int main() {

    std::cout << "How many cards do you want to add?";

    unsigned int howManyCars{0};
    std::cin >> howManyCars;
    std::vector<Car>car(howManyCars);
    std::vector<Garage>garage(howManyCars);

    std::cout << car.size() << std::endl;

    int doorsM{0};
    std::string brandM{" "};
    double powerM{0.0};
    Car* pCar = &car[0];


    for (size_t i = 0; i < howManyCars; ++i) {

        std::cout << "Make: ";
        std::cin >> brandM;
        std::cout << "How many doors: ";
        std::cin >> doorsM;
        std::cout << "How many MPH: ";
        std::cin >> powerM;
        car.at(i).add(doorsM, brandM, powerM);
        garage.at(i).add(*pCar);
        pCar++;
    }



    return 0;
}

Last edited on
This is C++ so pointers really should be a last resort. Why not just use a vector of Car instead of the pointer?

You really only need one Garage, not a vector of Garages.

Something more like the following perhaps?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <string>
#include <iostream>
#include <vector>

class Car
{
    private:
        std::string brand;
        int doors;
        double power;

    public:
        Car(): brand("a"), doors(0), power(0.0)
        {}
        Car(const std::string& model, int number_of_doors, double mpg) :
            brand(model), doors(number_of_doors),  power(mpg)
        {
            // Blank body.
        }
        void add(int ds, std::string bd, double pr)
        {

            brand = bd;
            doors = ds;
            power = pr;
        }
        void print()
        {
            std::cout << brand << std::endl;
            std::cout << doors << std::endl;
            std::cout << power << std::endl;
            std::cout << "------------------------" << std::endl;
        }

};

class Garage
{

        std::vector<Car> cars;
    public:

        void add(const Car& brum)
        {
            cars.push_back(brum);
        }
        void print()
        {
            for(size_t i = 0; i < cars.size(); ++i)
            {
                cars[i].print();
            }
        }
        void pop()
        {
            cars.pop_back();
        }
};

int main()
{
    std::cout << "How many cards do you want to add? ";

    size_t howManyCars{0};
    std::cin >> howManyCars;

    // You only need one Garage.
    Garage garage;

    for(size_t i = 0; i < howManyCars; ++i)
    {
        int doorsM{0};
        std::string brandM;
        double powerM{0.0};

        std::cout << "Make: ";
        std::cin >> brandM;

        std::cout << "How many doors: ";
        std::cin >> doorsM;

        std::cout << "How many MPH: ";
        std::cin >> powerM;

        garage.add({brandM, doorsM, powerM});
    }


    garage.print();

    return 0;
}

jlb has great advice.

What is the purpose of Car::add()? It basically changes the car to a different car.

What is the purpose of Garage::pop()? Is it supposed to remove a specific car, or just the last one that was inserted? Right now it removes the last one inserted.
Topic archived. No new replies allowed.