array of objects

I need an array of class objects but am unsure of how one might accomplish this. I have so far...

1
2
3
4
5
6
7
8
9
10
11
12
13
//element class driver code
Element Arsenic(lowCeiling, highCeiling);
Element Cadmium(lowCeiling, highCeiling);
Element Chromium(lowCeiling, highCeiling);
Element Copper(lowCeiling, highCeiling);
Element Lead(lowCeiling, highCeiling);
Element Nickel(lowCeiling, highCeiling);
Element Molybdenum(lowCeiling, highCeiling);
Element Mercury(lowCeiling, highCeiling);
Element Selenium(lowCeiling, highCeiling);
Element Zinc(lowCeiling, highCeiling);

Element metal[10] = {Arsenic, Cadmium, Chromium, Copper, Lead, Nickel, Molybdenum, Mercury, Selenium, Zinc};


Could the array be created in this manner or is that illegal code?


You may be looking to to create a child class off of the base "Element" class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Element
{
    public:
        // Code
    protected:
        unsigned int numProtons;
        unsigned int numElectrons;
        unsigned int numNutrons;
}

// Inherits all values from base element class
class Oxygen : public Element
{
    // Code
};
Last edited on
This would still leave me with the same problem of having to operate on each object's data individually when i could instead perform operations on all objects in the same sequence via for-loop
Topic archived. No new replies allowed.