I was wondering how you can define an array of the same class. Pretend we have 10 different cars and they all have the same properties. (This isn't really what I'm doing, but just a made up example).
Now say I want to create multiple instances of Car class in an array. Would I do something like this?:Code:class Car { public: unsigned int year; unsigned char color[256]; unsigned short weight; }
If this is correct, could I do something like this to loop through and get information from a user? Just an example:Code:Car cars[10];
Code:for( unsigned int i = 0; i < 10; i++ ) { cout << "Enter a cars year: \n"; cin >> cars[i].year; cout << "Enter a cars color: \n"; cin >> cars[i].color; cout << "Enter a cars weight: \n"; cin >> cars[i].weight; } for( unsigned int i = 0; i < 10; i++ ) { cout << "Year: " << cars[i].year; cout << "\tColor: " << cars[i].color; cout << "\tWeight: \n" << cars[i].weight; }



LinkBack URL
About LinkBacks



