Question
Description: I am quite new to C++ and I am trying to implement a program that can read in a file of trains and to
Description: I am quite new to C++ and I am trying to implement a program that can read in a file of trains and to check to see if they are valid or invalid. A train can be of any size but will always start with an engine and will end with a caboose. I specifically need the functions of Train.h all implemented in train.cpp to all work correctly and for all the files to run together without any errors.
Car.h is implemented in car.cpp
Each train is composed of cars so the train class is derived from the car class
Car header file`(for the nodes of the linked list)
#ifndef CAR_H #define CAR_H #include #include #include #include #include "Train.h" using namespace std; enum CARGO_TYPE { //All of the possible cargo types (or car types) BIOLOGICAL, POISONOUS, COMBUSTIBLE, OXIDIZER, RADIOACTIVE, LOCOMOTIVE, CABOOSE }; class Car { public: // Name: Car (Default Constructor) // PreCondition: none // PostCondition: instantiates a default-constructed Car object Car(); // Name: Car (Overloaded Constructor) // PreCondition: CARGO_TYPE is valid // PostCondition: instantiates a Car object with given cargo Car(CARGO_TYPE); // Name: ~Car (Destructor) // PreCondition: none // PostCondition: destroys car object and deallocates memory ~Car(); // Name: getCargo // PreCondition: none // PostCondition: returns current cargo CARGO_TYPE getCargo() const; // Name: setCargo // PreCondition: CARGO_TYPE is valid // PostCondition: sets a new cargo type for the object void setCargo(CARGO_TYPE); // Name: getNext // PreCondition: none // PostCondition: returns the next node in the list (or null) Car *const getNext() const; // Name: setNext // PreCondition: given Car* is a valid pointer to a car object // PostCondition: sets the next car to the given pointer. void setNext(Car*); // Name: getLength // PreCondition: none // PostCondition: returns the current length of the train, assuming callee // is the head of the list. int getLength() const; private: // variable equivalent CARGO_TYPE m_cargo; //Type of cargo in a car (max one) //(node *next) equivalent Car *m_next; //pointer to the next car in train (object) }; #endif /* CAR_H */
Car Cpp File
#include #include #include #include #include "Car.h" #include "Train.h" // Name: Car (Default Constructor) // PreCondition: none // PostCondition: instantiates a default-constructed Car object Car :: Car(){ Car *head; head = NULL; //sets the head of the node to NULL } // Name: Car (Overloaded Constructor) // PreCondition: CARGO_TYPE is valid // PostCondition: instantiates a Car object with given cargo Car :: Car(CARGO_TYPE cargo){ m_cargo = cargo; /* Car *head; head = cargo; //sets the head to a given cargo */ } // Name: ~Car (Destructor) // PreCondition: none // PostCondition: destroys car object and deallocates memory Car :: ~Car(){ Car *head; Car *current = head; Car *temp; while (current != NULL) { temp = current; current = current->m_next; delete temp; } } /* GETTERS AND SETTERS */ // Name: getCargo // PreCondition: none // PostCondition: returns current cargo CARGO_TYPE Car :: getCargo() const{ return m_cargo; } // Name: Car // PreCondition: CARGO_TYPE is valid // PostCondition: sets a new cargo type for the object void Car :: setCargo(CARGO_TYPE cargo){ m_cargo = cargo; } // Name: getNext // PreCondition: none // PostCondition: returns the next node in the list (or null) Car* const Car :: getNext() const{ return m_next; } // Name: setNext // PreCondition: given Car* is a valid pointer to a car object // PostCondition: sets the next car to the given pointer. void Car :: setNext(Car* next){ m_next = next; } // Name: getLength // PreCondition: none // PostCondition: returns the current length of the train, assuming callee // is the head of the list. int Car :: getLength() const{ int count = 0; Car *head; Car *current = head; while (current != NULL){ count++; current = current->m_next; } return count; }
Train header file (The linked list itself)
#ifndef TRAIN_H #define TRAIN_H /* NOTE: I can add additional functions only in this header file to help manage the linked list creation*/ #include "Car.h" class Train { public: // Name: Train (Default Constructor) // PreCondition: none // PostCondition: instantiates a default-constructed Train object Train(); // Name: Train (Overloaded Constructor) // PreCondition: none // PostCondition: instantiates a Train object with a given number Train(int); // Name: ~Train // PreCondition: none // PostCondition: destroys train object ~Train(); // Name: display // PreCondition: given ostream& is valid // PostCondition: appends own train information to a given ostream& void display(ostream&) const; // Name: isValid // PreCondition: none // PostCondition: returns a boolean indicating whether the current // arrangement of the train is valid bool isValid() const; // Name: addCar // PreCondition: given CARGO_TYPE is valid, given integer is a valid // index (where to insert in train) // PostCondition: adds car to the train, at given index. zero-indexed int addCar(const CARGO_TYPE&, int); // Name: removeCar // PreCondition: given integer is a valid index // PostCondition: removes Car from train at given index. zero-indexed int removeCar(int); // Name: removeCar // PreCondition: given CARGO_TYPE is valid // PostCondition: removes every car of given cargo_type from train. int removeCar(CARGO_TYPE); // Name: getLength // PreCondition: none // PostCondition: returns current length of the train int getLength() const; // Name: getNumber // PreCondition: none // PostCondition: returns current number of the train (The number is the name of the train) int getNumber() const; // Name: setNumber // PreCondition: none // PostCondition: sets a new number for the train void setNumber(int); // Name: operator<< // PreCondition: given arguments are valid // PostCondition: appends train information to ostream& // This function just calls the display function and allows us // to cout << myTrain friend std::ostream &operator<<(std::ostream&, const Train&); private: int m_len; //total size of train int m_number; //integer "name" of the train. For example, Train 441 Car *m_head; //pointer to the first car (should be a locomotive) }; #endif /* TRAIN_H */
Train cpp file
#include #include #include using namespace std; #include "Train.h" #include "Car.h" // Name: Train (Default Constructor) // PreCondition: none // PostCondition: instantiates a default-constructed Train object Train :: Train(){ Train *current = m_head; current = NULL; } // Name: Train (Overloaded Constructor) // PreCondition: none // PostCondition: instantiates a Train object with a given number Train :: Train(int data){ Train *current = m_head; current = data; } // Name: ~Train // PreCondition: none // PostCondition: destroys train object Train :: ~Train(){ delete m_head; } // Name: display // PreCondition: given ostream& is valid // PostCondition: appends own train information to a given ostream& void Train :: display(ostream& sout) const{ Train *current; //for (current = ) } // Name: isValid // PreCondition: none // PostCondition: returns a boolean indicating whether the current // arrangement of the train is valid bool Train :: isValid() const{ // needs link traversal to check all the nodes. if valid return true; //else{ return false if not valid} } // Name: addCar // PreCondition: given CARGO_TYPE is valid, given integer is a valid // index (where to insert in train) // PostCondition: adds car to the train, at given index. zero-indexed int Train :: addCar(const CARGO_TYPE&, int){ } // Name: removeCar // PreCondition: given integer is a valid index // PostCondition: removes Car from train at given index. zero-indexed int Train :: removeCar(int trainCar){ } // Name: removeCar // PreCondition: given CARGO_TYPE is valid // PostCondition: removes every car of given cargo_type from train. int Train :: removeCar(CARGO_TYPE trainCar){ } // Name: getLength // PreCondition: none // PostCondition: returns current length of the train int Train :: getLength() const{ return m_len; } // Name: getNumber // PreCondition: none // PostCondition: returns current number of the train // (The number is the name of the train) int Train :: getNumber() const{ return m_number; } // Name: setNumber // PreCondition: none // PostCondition: sets a new number for the train void Train :: setNumber(int trainNum){ m_number = trainNum; } // Name: operator<< // PreCondition: given arguments are valid // PostCondition: appends train information to ostream& // This function just calls the display function and allows us to // cout << myTrain friend std::ostream &operator<<(std::ostream& sout, const Train& train){ // display(); Train *current; for ( current = train.m_head; current != NULL; current = current->m_next){ sout << current->getType(m_data) << endl; return sout; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started