dinamic arrays of classes

I'm sorry, guys if i cann't post my code it's written in my language, but i have serious problem... what i want to do looks like this code

i need to do one thing, CPark derives from CData, CTax, CFloor and CPlaces...
i want to make a fixed array of floors but i need to have a dynamic alocation for the places, instead of having CPlaces Array_Places[6], need to change that number six anytime i want, when reading a configuration file to change the number of places to park a car in several floors...

i read the file in the main.cpp, ask the user the number of places, write it on file and then place the number configured to the number of floors on the [] of the array...
i want to d this x="some number" and Array_Places[x];
the constructor
initilizes it with Array_Floor[i]->Array_Places[j]->license_plate=NULL;
this means that the default license plate is NULL.... in each place if each floor....
class CData:
{

protected:
int year;
int month;
public:
//methods to use these members
};

class CTax:
{
protected:
int cents;
int euros;
public:
CData(int year,int month);
//methods to use these members
};
class CPlaces: public CData, CTax
{
public:
CPlaces *Array_Places[6]; // i need to change this 6
CData *date;
};

class CFloor: public CPlaces, CData, CTax
{
public:
CFloor *Array_Floors[4];
};

/*
I can save the data using the pointers like
in a method of CPark*/
//constructor of CPark


CPark::CPark()
{
for(int i=0;i<4;i++){
for(int j=0;i<some_number;j++){
Array_Floors[i]->Array_Places[j]->license_plate=NULL
}
}

void CPark::Insert_Car()
{
int Year;;
int Month;
cin >> Year;
cin >> Month;

Array_Floors[0]->Array->Places[0]->date=new CData(Year,Month;
}


void CPark::showonscreen()
{

cout << Array_Floors[0]->Array->Places[0]->date->Year;

cout << Array_Floors[0]->Array->Places[0]->date.Year;
}

You need a dynamically allocated array.

You're getting into an area that a large number of problems occur because people don't handle their dynamically allocated memory well.

The thing with allocating memory, is you must deallocate it. You must be responsible for your own memory or you will get what is known as a memory leak.

But, enough banter, lets get to the issue.

first, please don't post code again unless you use code tags (the < > icon).. Nobody seems to ever have read a code forum before.

This is your current code that you want to change:

1
2
3
4
5
class CPlaces: public CData, CTax
{
public:
CPlaces *Array_Places[6]; // i need to change this 6
};


and you want:
i want to d this x="some number" and Array_Places[x];


do you want to do this in the constructor? in a public method? in a private method?

For now I'll set you up using a public method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class CPlaces : public ...
{
private:
    unsigned int xSize , ySize;
    CPlaces ** Array_Places;
public:
    CPlaces()
    {
        Array_Places = NULL;
        xSize = ySize = 0;
    }
    // a destructor, called when your object goes out of scope or is deleted
    ~CPlaces()
    {
        freeArray_Places();
    }

    //initializes Array_Places to a 2-dimensional array with the passed sizes
    // deletes all current information in Array_Places.. if you want to keep it you need to copy it before this call
    void initArray_Places(unsigned int xSize, unsigned int ySize)
    {
        if(Array_Places)
            freeArray_Places();

        this->xSize = xSize;
        this->ySize = ySize;

        Array_Places = new CPlaces[xSize][ySize];
    }
    void freeArray_Places()
    {
        for( int x = 0; x < xSize; ++x )
        {
            delete [ ] Array_Places[x];
        }
        
        delete [ ] Array_Places;
        Array_Places = NULL;
        xSize = ySize = 0;
    }
}



if you have any more questions..check out this, it goes over the same question you're asking:
http://stackoverflow.com/questions/1824363/dynamic-allocation-deallocation-of-2d-3d-arrays
This doesn't sound like you want to use derived classes. Do you have to? The derived class is an "is a" relationship.

The amount of pointers you are all using is a little out of hand.
Lowestone, yes i use derived classes, that's the biggest, problem, beyond that, SexyBachelor, sorry i'm new in the forum, i still don't know how to use the tags, i just wroted the message, dealing with the priblem you have a good idea, and i've thought in the 2d-array, but as i wanted to use separate classes, and half of the code is already done and it's a team work, instead of using a 2d-array, i have two 1-d arrays one for floors and other for places, when the floor[0]->to places[from 0 to 5], and floor[1] the same thing, the problem for me it's to read a configuration file and pass all the changes that i need to the classes that i use, i have an idea, each time a need to change a value i'll read the file and use only the value needed, in each class, 'cuz it will have change of taxes change of places, etc... it's kind a kreepy this work and it only counts for three values maximum 3.5, from 0 to 20.... bu t without it i can't pass on the discipline... tomorrow i'll try to write a better code for you to understand exactly what i'm i want to do... i'm tired by now it's 0h30... see ya
i use derived classes, that's the biggest, problem

Indeed, let's start with that.

Your code claims that a parking lot is a floor, and also that a parking lot is tax, whatever that means.

What it should says is that a parking lot has floors, and that a floor has parking places.

So, it's

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Place
{
    Date date;
    Tax tax;
};

class Floor
{
    vector<Place> places;
};

class Park 
{
    vector<Floor> floors;
};


start with that, and build out.
Topic archived. No new replies allowed.