#include #include #include struct person { string fname, lname; // object's variables are MEMBERS. int ssn, bd; // object's functions are called METHODS void print() { cout << fname << " " << lname << ", SSN=" << ssn << ", born " << bd << "\n"; } // a method with no return type and the same name as // the struct is a CONSTRUCTOR. // If you define any constructor, then a constructor // will be called every time an object is created. It // can't be prevented. If none of the constructors are // suitable, the program will not compile. person(int s, string f, string l, int b) { ssn = s; fname = f; lname = l; bd = b; } }; person * * read_data() { ifstream in("/home/www/class/een118/labs/database1.txt"); if (in.fail()) { cerr << "No file\n"; exit(1); } person * * array = new person * [1010]; // array is a pointer to an array of pointers to persons // a pointer to an array, so we can be flexible and use new // and not need to know the size in advance // an array of pointers so that we don't find ourselves // using copies of objects int num = 0; while (num < 1010) { int a, b; string c, d; in >> a >> c >> d >> b; // The file contains ssn fname lname bd if (in.fail()) break; array[num] = new person(a, c, d, b); num += 1; } in.close(); return array; } person * find_oldest(person * * data, int size) { int oldest = data[0]->bd, oldestpos = 0; for (int i=1; ibd < oldest) { oldest = data[i]->bd; oldestpos = i; } return data[oldestpos]; } void main() { person * * data = read_data(); person * x = find_oldest(data, 1000); // x is a pointer to the object from the array, not a copy // of it, so it allows us to change the one and only person // object for the oldest man x->print(); cout << "Change his name...\n"; x->fname = "Old Man"; x->print(); int a = 2+2; person * y = find_oldest(data, 1000); y->print(); }