array problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: array problem

  1. #1
    Join Date
    Oct 2003
    Posts
    15

    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 06:44 PM.

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    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;

  3. #3
    Join Date
    Apr 1999
    Posts
    27,418
    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

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    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...
    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds


    Article(s): Allocators (STL) Function Objects (STL)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center