Storing classes in array

I have a base class Building. Then come its children classes - Commercial Building and Residential Building. The third level is composed of Apartment and House classes, both inherit from Residential Building.

I need to create an array of 20 elements that will store info about all these different types of buildings(Commercial Building,Residential Building,Apartment, House). How should I proceed? Thank you in advance!!!
arrays are used to store data of the same type with the same meaning. what information do you want to use? are you trying to store objects inside the array?
A class is something that holds private data and functions. You cannot store functions into an array.

A struct is similar to a class aside from the fact that its members are public, and it cannot store functions.

example :
1
2
3
4
5
6
7
8
9
10
11
12
//declaring the variables inside your struct
struct buildings {int Commercial; int Residential; int Apartment; int House;}; 
//making an array for your object;
buildings arrayname[20];
//fill the data
for(int i = 0; i < 20; i++)
{
    arrayname[i].Commercial = //method to store data
    arrayname[i].Residential = //method to store data
    arrayname[i].Apartment = //method to store data
    arrayname[i].House = //method to store data
}


hope this helps
First off, this is C++. We don't use arrays. We use vectors. Unless you need to store something on the stack for efficiency, use a vector.

Because you are storing polymorphic types, you need to use pointers. With pointers you need to decide on the object lifecycle and ownership semantics. The easiest way to handle that is through shared pointers.

1
2
3
4
5
6
7
typedef std::shared_ptr<Building> building_ptr;  // I typically embed this in polymorphic types as Building::pointer.
typedef std::vector<building_ptr> buildings_type;  // a vector of shared pointers.

buildings_type buildings;
buildings.push_back(new CommercialBuilding);
buildings.push_back(new Apartment);
std::cout << buildings[0]->address() << std::endl;  // assumes address() member on Building. 

I don't know if your solution will work. he hasn't been clear about what type of information he needs to store in the array. I doubt there are 20 different types of buildings, but I'm not an architect. unless he's using four or five different types filling all 20 elements with them.
I don't know if your solution will work.

Yes it will.

We know that every type of building is derived ultimately from the class Building.

PanGalactic has already stated that the vector (or even evil array) must use pointers.

If the classes are implemented correctly (i.e. are well-behaved polymorphic types) then the functionality of derived building types will be accessible via a pointer to their base type (Building*).

Even if the types are not polymorphic, they can still be stored by pointer in a vector which holds pointers to their base type, though it wouldn't work right (without some mucking about.)

Actually, the problem screams "exercise in polymorphism", so this is no doubt exactly what is wanted.

But Sheroz Nazhmudinov does state that "[he needs] to create an array of 20 elements", so maybe he does need to use an (evil) C array. But this will still work correctly it it's an array of Building* pointers.

Andy

PS Would ApartmentBlock be better than Apartment, when it comes to the class hierarchy?
Last edited on
Topic archived. No new replies allowed.