Sorry, if this has been asked before, just learning C++, had tried to search for it but not sure what the keyword is.
would it possible to do this?
class Car {
public:
void addColor(string c) {
color = c;
}
private:
string color;
}
class Honda:public Car {}
class Toyota:public Car {}
int main() {
vector<Car> v;
Honda *car1 = new Honda();
car1.addColor("green");
Toyota *car2 = new Toyota();
car2.addColor("blue");
v.push_back(car1);
v.push_back(car2);
for (int i = 0; i < v.size(); i++) {
cout << v[i].color << endl; // should output green, blue
}
}
if it's possible, what is the most right/effective way to do it?
EDIT 1
wow, thank you everybody!