// arr5.cpp By: Aiman Hanna - ©1993-2002 Aiman Hanna // This program illustrates arrays of class objects. The program also illustrates how // an array of objects can be created using the default constructor then // be initialized from an input file. // Notice that this program will NEVER compile until you have "tstring.h" which defines // the String class, which was provided by a 3rd party (some textbook that we previously used). // This is just an example of how severely your program may suffer when you depend on the use // a non-standard types. // You should replace "String" by char* if you do not have access to that file. // Key Points: 1) Arrays of class objects // 2) default constructor #include "arr5.h" Car::Car(String mdl , int nd , double mil ) { model = mdl; numdrs = nd; mileage = mil; } void Car::SetModel( String mdl) { model = mdl; } String Car::GetModel( ) { return model; } int Car::GetNumDrs() { return numdrs; } void Car::SetNumDrs(int nd) { numdrs = nd; } double Car::GetMileage() { return mileage; } void Car::SetMileage(double mil) { mileage = mil; } // Free function to initialize an array of Car objects void CarInitialize( Car c[], int numobjs, ifstream& infile ) { String st; int nd, counter = 0; double mil; infile >> st >> nd >> mil; while ((infile) && (counter < numobjs)) { c[counter].SetModel(st); c[counter].SetNumDrs(nd); c[counter].SetMileage(mil); infile >> st >> nd >> mil; counter++; } } int main() { // Create an array of 3 cars. Car cararr1[3] = {Car("Mercedes", 4, 1000), Car("BMW", 2, 20000), Car("VW", 3, 12000)}; for (int i = 0; i < 3; i++) { cout << "\n Here is the information of car #: " << i+1 << endl; cout << " The model of the car is: " << cararr1[i].GetModel() << endl; cout << " The car has " << cararr1[i].GetNumDrs() << " doors." << endl; cout << " The car mileage is: " << cararr1[i].GetMileage() << "." << endl; } // Create an array of 10 car objects. Information about the objects is // placed in the file carinfo.txt, so the objects are to be initialized // from this file. Car cararr2[10]; // This uses the default constructor // Attempt to open the input file. If successful then pass this open file along with // the array of objects to the function CarInitialize() to initialize // the different objects. The number of objects to be initialized is // also passed to the CarInitialize() function. The file carinfo.txt is // assumed to have at least as many entries as the number of objects passed. ifstream fin; fin.open("carinfo.txt"); if (!fin) { cout << " Could not open file: carinfo.txt for reading. Will exit" << endl; exit(1); } else CarInitialize(cararr2, 10, fin); fin.close(); cout << "\n Here is the information for car objects initialized from carinfo.txt" << endl; cout << " ====================================================================" << endl; for ( i = 0; i < 10; i++) { cout << "\n Here is the information of car #: " << i+1 << endl; cout << " The model of the car is: " << cararr2[i].GetModel() << endl; cout << " The car has " << cararr2[i].GetNumDrs() << " doors." << endl; cout << " The car mileage is: " << cararr2[i].GetMileage() << "." << endl; } return 0; } // The output of the program /* Here is the information of car #: 1 The model of the car is: Mercedes The car has 4 doors. The car mileage is: 1000. Here is the information of car #: 2 The model of the car is: BMW The car has 2 doors. The car mileage is: 20000. Here is the information of car #: 3 The model of the car is: VW The car has 3 doors. The car mileage is: 12000. Here is the information for car objects initialized from carinfo.txt ==================================================================== Here is the information of car #: 1 The model of the car is: Mazda The car has 4 doors. The car mileage is: 12700. Here is the information of car #: 2 The model of the car is: Honda The car has 5 doors. The car mileage is: 23400. Here is the information of car #: 3 The model of the car is: Nissan The car has 3 doors. The car mileage is: 12500. Here is the information of car #: 4 The model of the car is: Golf The car has 3 doors. The car mileage is: 12300. Here is the information of car #: 5 The model of the car is: Ford The car has 4 doors. The car mileage is: 56000. Here is the information of car #: 6 The model of the car is: Chevrolet The car has 4 doors. The car mileage is: 21500. Here is the information of car #: 7 The model of the car is: RR The car has 4 doors. The car mileage is: 45000. Here is the information of car #: 8 The model of the car is: Mercedes The car has 4 doors. The car mileage is: 14500. Here is the information of car #: 9 The model of the car is: BMW The car has 2 doors. The car mileage is: 54200. Here is the information of car #: 10 The model of the car is: Honda The car has 2 doors. The car mileage is: 14800. */