-
January 14th, 2004, 07:40 PM
#1
array problem
Hello people I need help, i want use an array to store an object
but I don't know how I do this.
eg:
---------------
car.h
----------------
class car{
public:
car();
public:
string marc;
string model;
string color;
int category;
};
-------------------
car.cpp
------------------------
constructor
car::car(){
marc="bmw";
model="v2" ;
string="black";
category=2;
}
I want to know how I can declare and create an array to store the object "car" in this class.
Thanks in advance for your help.
Last edited by dmf9000; January 14th, 2004 at 07:44 PM.
-
January 14th, 2004, 07:52 PM
#2
Since you have already provided a default constructor for the car class, you can create an array of car just like an array of POD, like int.
Code:
// Method 1
car myCar[10];
// Method 2
car *pCar = new car[10];
delete []pCar;
-
January 14th, 2004, 09:58 PM
#3
Another method:
Code:
#include <vector>
std::vector<car> MyCar(10); // 10 cars
//...
MyCar[0].model = "Mercedes";
//etc...
This method does not require new[] or delete[].
Regards,
Paul McKenzie
-
January 15th, 2004, 02:58 AM
#4
Re: array problem
Originally posted by dmf9000
I want to know how I can declare and create an array to store the object "car" in this class.
Use 'vector' as mentioned by Paul...take a look at the fowolling introduction for further questions...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|