vendredi 11 août 2017

storing object names in array?

I am new to c++ and I was wondering if you could store object names in an array to access them later using a for loop. For example:

#include <iostream>
    array[] = { "Obj1", "Obj2", "Obj3" };
    int i;

    class Class {
        public:
            string name; // the name of the object
            int age; // the age of the object
    }

    int main() {
      Class Obj1;
      Class Obj2;
      Class Obj3;
        Obj1.name = "Judy";
        Obj1.age = 29;
        Obj2.name = "Mike";
        Obj2.age = 38;
        Obj3.name = "Dorothy";
        Obj3.age = 19;

        for (i = 0; i < (sizeof(array) / sizeof(array[0])); i++) {

            cout << array[i].name << endl;
            cout << array[i].age << endl;

        }
    }

Aucun commentaire:

Enregistrer un commentaire