Exercise 3 Constructing a Train ADT (24 pts) This train will be made up of a ser
ID: 3667518 • Letter: E
Question
Exercise 3 Constructing a Train ADT (24 pts)
This train will be made up of a series of linked TrainCar types. Each TrainCar should have a field for the car’s type (Engine, coal, etc), its cargo capacity in kilograms, a serial number, and a pointer to the car behind it on the train. You should define three functions for this ADT, which will be used in constructing and manipulating a train:
1. createTrainCar(kind, cargo, serial): will create a new TrainCar, not connected to any other car.
2. connectTrains(front, back): given two pointers to tranCars, join them together (connect the first car in the back to the last car in the front). For example: if we have two trains
A-->B-->C
X-->Y
the result of connectTrains would be A-->B-->C-->X-->Y. In this function, the second train really becomes part of the first train.
3. disconnectCars(train, serial): will break a train into two smaller trains, splitting the train after the TrainCar containing the given serial number.
Return a refToTrainCar which points to the car after the one with the given serial number.
For example: if we have a train A-->B-->C-->X-->Y, disconnecting at B would cause B to have no train after it, and would return the refToTrainCar for C.
In this case C would be the start of a smaller train C-->X-->Y.
4. printTrain(train): given a pointer to a trainCar, display the cars one by one from the first to the last car.
When you are done, you will have three files:
1. The ADT interface file trainCar.h containing the record type definition, and the function prototypes for the operations (and the algorithm headers, of course).
2. The ADT implementation file trainCar.cc containing the implementation of the operations.
3. The application file testTrainCar.cc containing the testing you did on your operations.
You can use the main function we gave as an example in Assignment 4 as a starting point. What to hand in
1. The ADT interface file trainCar.h containing the record type definition, and the function prototypes for the operations. This file should not contain any implementation code at all. 2. The ADT implementation file trainCar.cc containing the implementation of the operations.
3. The application file testTrainCar.cc containing the testing you did on your operations. You can use the main function we gave as an example in Assignment 4 as a starting point.
4. a5e3output.txt containing the console input/output for your testing in testTrainCar.cc.
Explanation / Answer
#include<iostream.h>
class connectTrains
{
public:
class *front,*back;
int data;
};
class TrainCar:public connectTrains{
private:
char serial[20];
char kind[20];
char cargo[20];
char train;
public :
void display(){
}
void setTrain(char train){
this.train = train
}
void getTrain(){
cout<< this.train;
}
void setSerial(char serial){
this.serial = serial;
}
char getSerial(){
cout<<this.serial;
}
void setCargo(char cargo){
this.cargo = cargo;
}
char getCargo(){
cout<<this.cargo;
}
void setKind(char kind){
this.kind =kind;
}
char getKind(){
cout<<this.kind;
}
};
//
//
// File: trainCar.h
// Author: HARE KRISHNA
//
// Created on 13 February, 2016, 8:21 PM
//
#ifndef _TRAINCAR_H
#define _TRAINCAR_H
#include<iostream.h>
class connectTrains
{
public:
class TrainCar *front,*back;
int data;
};
class TrainCar:public connectTrains{
private:
char serial[20];
char kind[20];
char cargo[20];
char train;
public :
void setTrain(char train){
this.train = train
}
void getTrain(){
cout<< this.train;
}
void setSerial(char serial){
this.serial = serial;
}
char getSerial(){
cout<<this.serial;
}
void setCargo(char cargo){
this.cargo = cargo;
}
char getCargo(){
cout<<this.cargo;
}
void setKind(char kind){
this.kind =kind;
}
char getKind(){
cout<<this.kind;
}
};
#endif /* _TRAINCAR_H */
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.