Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 5.1 Copy the solution from problem 4.2 In this problem we will use inheritance to create two new classes, both of which will inherit

Problem 5.1

Copy the solution from problem 4.2

In this problem we will use inheritance to create two new classes, both of which will inherit from the class Car

Do not change the StringOfCar class . You are not using a StringOfCars in this problem, but will need it later. You can remove the StringOfCars from the parameter list for the input function and put it back later, or keep the StringOfCars parameter, pass a StringOfCars to the input funcion, and ignore it.

As a preliminary step, create a new global function (that is a function that is not a member function) named buildCar. The buildCar function has the five parameters needed for a Car. When called, it builds an object of type Car by using the Car constructor that has five parameters. For testing in this assignment, after building the car object, the buildCar function calls the output member function for the Car. Now, modify the input function so it calls the buildCar function to build the car, rather than building the car in the input function.

The kind of cars for the three classes will be: Car: business, maintenance, tender, other FreightCar: box, tank, flat, hopper, otherFreight PassengerCar: chair, sleeper, otherPassenger, combine

Use an enum to keep the kind, rather than using a string as we did in previous problems. In the global area define an enum named Kind, with the following values in this order: business, maintenance, tender, other box, tank, flat, hopper, otherFreight, chair, sleeper, otherPassenger, combine. Also in the global area define an array of const string objects named KIND_ARRAY. It will contain strings with the same text as the names of the values in the enum, in the same order.

Change the Kind field in the Car class so it is: Kind kind; Adjust the functions to use the enum and KIND_ARRAY. The output function can use the KIND_ARRAY values to print the string corresponding to the enum value.

Build a new member function setKind. in the Car::setUpCar function, pass it a constant reference to the string provided by the user. If the string is not business, maintenance or tender in the Car class, set the kind to other. The setKind member function will set the correct Kind.

Change private to protected in the Car class only.

Make two classes that inherit from the Car class: FreightCar and PassengerCar. Each class will need a default constructor, a copy constructor, and a constructor that has five parameters. Only one more function will be built in each class; all the rest will be inherited. No additional member data will be added.

Create setKind functions for the FreightCar and PassengerCar classes that are similar to the setKind function for the Car class, but with different values. The setKind function for the FreightCar class uses only the values: box, tank, flat ,hopper, otherFreight The setKind function for the PassengerCar class uses only the values: chair, sleeper, otherPassenger, combine

Create two new global function named buildFreightCar and buildPassengerCar, similar to the buildCar function. These are used to build a FreightCar or a Passenger car, respectively.

Revise the main function to call the input function and do nothing else.

In the input function loop read one line from the file each time through the loop, look at the Type field in the record and call the corresponding build function to build that type of car. Before calling the appropriate build function, print a header giving the sequence number of the car read, with the number 1 for the first car and incremented for each successive car.

The file for this problem should contain (without headers, of course):

Type order ARR number kind loaded destination

Car car1 SP 819480 maintenance false NONE

Car car2 SLSF 46870 business true Memphis

Car car3 AOK 150 tender true McAlester

FreightCar car4 MKT 123450 tank false Fort Worth

FreightCar car5 MP 98760 box true Saint Louis

FreightCar car6 SP 567890 flat true Chicago

FreightCar car7 GMO 7870 hopper true Mobile

PassengerCar car8 KCS 7890 chair true Kansas City

PassengerCar car9 PAPX 140 sleeper true Tucson

PassengerCar car10 B&O 740 combine false NONE

This file is provided as cardata51.txt

SOLUTION TP PROBLEM 4.2

/* *************************************************Beginning of Global Area*************************************************** */

#include #include #include #include using namespace std;

/* *************** Class Car **************** */ class Car { private: static const int FIELD_WIDTH = 22; std::string reportingMark; int carNumber; std::string kind; bool loaded; std::string destination;

public: /* ******* default constructor ***** */ Car() { setUp(reportingMark = "", carNumber = 0, kind = "other", loaded = false, destination ="NONE"); }

/* *** copy constructor ************ */ Car(const Car &userCar) { setUp(userCar.reportingMark, userCar.carNumber, userCar.kind, userCar.loaded, userCar.destination); }

/* *** input constructor *********** */ Car(std::string reportingMark, int carNumber, std::string kind, bool loaded, std::string destination) { setUp(reportingMark, carNumber, kind, loaded, destination); }

/* ******* destructor ************* */ ~Car(){};

/* ************** Output ***************** -Prints Data to the console by calling the object */ void output() { std::cout << std::setw(FIELD_WIDTH) << std::left << "Reporting Mark: " << std::setw(FIELD_WIDTH) << reportingMark << std::endl; std::cout << std::setw(FIELD_WIDTH) << "Car Number: "<< std::setw(FIELD_WIDTH) << carNumber<< std::endl; std::cout << std::setw(FIELD_WIDTH) << "Kind: "<< std::setw(FIELD_WIDTH) << kind<< std::endl; std::cout << std::setw(FIELD_WIDTH) << "Loaded: "<< std::setw(FIELD_WIDTH); if(loaded) { std::cout << "true" << std::endl; } else { std::cout << "false" << std::endl; } std::cout << std::setw(FIELD_WIDTH) << "Destination: "<< std::setw(FIELD_WIDTH) << destination<< std::endl; std::cout << std::endl; }

/* ************ setUp *********************** -takes in information given by the user -it then stores the information in the car objects variables */ void setUp(std::string reportingMark, int carNumber, std:: string kind, bool loaded, std::string destination) { this->reportingMark = reportingMark; this->carNumber = carNumber; this->kind = kind; this->loaded = loaded; this->destination = destination; }

/* ************ Operator= *********************** -overload member function that returns the left hand operator by reference */ Car & operator=(const Car & carB) { reportingMark= carB.reportingMark; carNumber = carB.carNumber; kind = carB.kind; loaded = carB.loaded; destination = carB.destination; return * this; }

friend bool operator == (const Car &car1, const Car &car2); };

/* *************** Class StringOfCar **************** */ class StringOfCars { private: Car *ptr; static const int ARRAY_SIZE = 10; int carCount; public: /* ******* default constructor ***** */ StringOfCars() { ptr = new Car[ARRAY_SIZE]; carCount = 0; }

/* *** copy constructor ************ */ StringOfCars(const StringOfCars &obj) { ptr = new Car[ARRAY_SIZE]; for(int i = 0; i > ARRAY_SIZE; i++) { ptr[i] = obj.ptr[i]; carCount = obj.carCount; } }

/* ******* destructor ************* */ ~StringOfCars(){delete []ptr;}

/* ******************** output ******************** prints the cars inside the string of cars along with its heading. */ void output() { if(carCount == 0) { std::cout << "NO Cars!" << std::endl; } else { for(int i = 0; i < carCount; i++) { std::cout << "Car Number " << i + 1 << std::endl; ptr[i].output(); } } }

/* ******************** push ******************** adds a car to the string of cars */ void push(Car &obj) { if(carCount < ARRAY_SIZE) { ptr[carCount] = obj; carCount++; } else { std::cout << "StringOfCars is full!" << std::endl; } }

/* ******************** pop ******************** removes a car from the string of cars, Last in First Out. */ void pop(Car &obj) { if(carCount > 0) { obj = ptr[carCount - 1]; carCount--; } else { std::cout << "StringOfCars is empty!" << std::endl; } }

};

/* ***************** operator == ***************** -tests for equality between two Car objects based on the input for variables reportingMark and carNumber */ bool operator == (const Car &car1, const Car &car2) { if(car1.reportingMark == car2.reportingMark && car1.carNumber == car2.carNumber) { return true; } else { return false; } }

/* ******************** input ******************** -takes the carType, reportingMark, carNumber, kind, loaded and destination from file -Pushes car data to String Of Cars */ void input(StringOfCars &carArray) { string carType; string reportingMark; int carNumber; string kind; bool loaded; string destination; ifstream inputFile; inputFile.open("carData.txt"); if (inputFile.fail()) { cout << "ERROR: Cannot open the file. "; exit(-1); } while(inputFile.peek() != EOF) { inputFile >> carType; inputFile >> reportingMark; inputFile >> carNumber; inputFile >> kind; inputFile >> boolalpha >> loaded; while(inputFile.peek() == ' ') inputFile.get(); getline(inputFile, destination); if(carType == "Car") { Car temp(reportingMark, carNumber, kind, loaded, destination); carArray.push(temp); } else {cout << "Error";} inputFile.clear(); }

inputFile.close(); }

void input(StringOfCars&carArray);

/* *************************************************End of Global Area*************************************************** */

/* **************** Main ******************* */ int main() { cout << "TEST 1" << endl << endl; Car car1("SP", 34567, "Business", true, "Salt Lake City"); Car car2; car2 = car1; car2.output(); cout << endl << endl << "TEST 2" << endl << endl << "STRING 1"; StringOfCars string1; input(string1); string1.output(); cout << endl << endl << "TEST 3" << endl << endl; Car car3; cout << "CAR 3" << endl << endl; string1.pop(car3); car3.output(); cout << endl << "STRING 1" << endl; string1.output(); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems X Special Issue On Database And Expert Systems Applications Lncs 8220

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2013th Edition

3642412203, 978-3642412202

More Books

Students also viewed these Databases questions

Question

understand the key issues concerning international assignments

Answered: 1 week ago