Wywal wszystkie wskaźniki, nie musisz także mieć własnych konstruktorów ani destruktorów. Użyj kontenera vector oraz przeładowania operatora << do wypisywania na ekranie. Zobacz jak to może wyglądać:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Car
{
string brand {"None"};
string color {"None"};
int mileage {0};
int capacity {0}; // number of passengers
friend ostream& operator<<( ostream& out , const Car& car )
{
out << "Brand = " << car.brand << " : color = " << car.color ;
out << " : mileage = " << car.mileage << " : capacity = " << car.capacity;
return out;
}
};
class Garage
{
private:
vector<Car> cars;
public:
void add( const Car& car )
{
cars.push_back(car);
}
bool change_mileage( const string& name , int value )
{
auto iter {find_if( cars.begin(), cars.end(), [&]( const Car& car ) { return car.brand == name; } )};
if( iter!=cars.end() ) iter->mileage += value;
return iter!=cars.end();
}
friend ostream& operator<<( ostream& out , const Garage& garage )
{
for( const auto& car : garage.cars ) out << car << endl;
return out;
}
};
int main()
{
Garage garage;
garage.add( { "Mercedes","Red",120000,5 } );
garage.add( { "Volvo","Silver",250000,5 } );
garage.add( { "Audi","Green",45000,4 } );
cout << garage << endl;
cout << "Try to change mileage : " << ( garage.change_mileage("Volvo", -160000) ? "Success" : "Fail" ) << endl << endl;
cout << garage << endl;
return 0;
}