1 / 7

#include < iostream > # include <string > # include < fstream > using namespace std;

Specification. #include < iostream > # include <string > # include < fstream > using namespace std; class CAR / / class for train cars { public : CAR(); / / constructor

dafydd
Download Presentation

#include < iostream > # include <string > # include < fstream > using namespace std;

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 };

  2. Implementation (con’t) CAR::CAR () { cargoWeight = 75; nextCar= NULL; } intCAR::getWeight() { return cargoWeight; } void CAR::loadCar (int cargo) { cargoWeight= cargo; }

  3. 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; };

  4. 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); }

  5. 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 }

  6. 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; }

  7. The Main Function void main() { TRAIN MetroLiner; cout<< "PROGRAM OUTPUT:" << endl; MetroLiner.loadTrain(); MetroLiner.displayTrainInfo(); system("pause"); return 0; }

More Related