70 likes | 247 Views
Specification. #include < iostream > # include <string > # include < fstream > using namespace std; class CAR / / class for train cars { public : CAR(); / / constructor
E N D
Specification #include <iostream> #include <string> #include <fstream> using namespace std; class CAR // class for train cars { public: CAR(); // constructor intgetWeight(); // returns the weight of the car in pounds void loadCar(int); // loads a cargo with parameter weight CAR *nextCar; // pointer to the next car on the train private: intcargoWeight; // the weight of the trains cargo };
Implementation (con’t) CAR::CAR () { cargoWeight = 75; nextCar= NULL; } intCAR::getWeight() { return cargoWeight; } void CAR::loadCar (int cargo) { cargoWeight= cargo; }
Specification class TRAIN // train class as a linked list of cars { private: inttrainWeight; public: TRAIN(); //constructor intgetWeight(); // returns the weight of the train void loadTrain(); // load train with initial cars void addCar_front(int); void addCar_end(int); // THIS IS CODE YOU MUST WRITE// ~TRAIN(); // destructor THIS IS CODE YOU MUST WRITE void displayTrainInfo(); CAR *firstCar; };
Implementation TRAIN::TRAIN() { trainWeight= 0; firstCar = NULL; } intTRAIN::getWeight() // returns the train weight { return trainWeight; } void TRAIN::loadTrain() // load train with initial cars { addCar_front(0); // add first car addCar_front(10); addCar_front(20); addCar_front(30); addCar_front(40); addCar_front(50); addCar_front(60); addCar_end(60); addCar_end(30); addCar_end(40); }
Implementation (con’t) void TRAIN::addCar_end (intcWeight) // add car to end { // YOU MUST WRITE THIS FUNCTION } void TRAIN::addCar_front (intcWeight) // add first car to train { // YOU MUST WRITE THIS FUNCTION }
DisplayTrainInfo Member Function void TRAIN::displayTrainInfo() { intcnt = 0; CAR* ptrCar; for (ptrCar = firstCar; ptrCar != NULL && ++cnt <11; ptrCar = ptrCar->nextCar) cout<< "+-------+ "; cout<< endl; cnt= 0; for (ptrCar = firstCar; ptrCar != NULL && ++cnt < 11; ptrCar = ptrCar->nextCar) cout<< "| | "; cout<< endl; cnt = 0; for (ptrCar = firstCar; ptrCar != NULL && ++cnt < 11; ptrCar = ptrCar->nextCar) { if ((*ptrCar).getWeight() < 10) cout << "| " << (*ptrCar).getWeight() << " |->"; else cout << "| " << (*ptrCar).getWeight() << " |->"; } cout<< endl; cnt = 0; for (ptrCar = firstCar;ptrCar!= NULL && ++cnt < 11; ptrCar = ptrCar->nextCar) cout << "| | "; cout<< endl; cnt= 0; for (ptrCar = firstCar; ptrCar != NULL && ++cnt < 11; ptrCar = ptrCar->nextCar) cout << "+-------+ "; cout << endl; }
The Main Function void main() { TRAIN MetroLiner; cout<< "PROGRAM OUTPUT:" << endl; MetroLiner.loadTrain(); MetroLiner.displayTrainInfo(); system("pause"); return 0; }