Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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, other FreightCar: box, tank, flat, otherFreight PassengerCar: chair, sleeper, otherPassenger

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, other, box, tank, flat, otherFreight, chair, sleeper, otherPassenger 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 functionm pass it a constant reference to the string provided by the user. If the string is not business or maintenance 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, otherFreight The setKind function for the PassengerCar class uses only the values: chair, sleeper, otherPassenger

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 is called cardata5.txt, and must contain (without headers, of course):

Type order ARR number kind loaded destination

Car car1 CN 819481 maintenance false NONE

Car car2 SLSF 46871 business true Memphis

Car car3 AOK 156 tender true McAlester

FreightCar car4 MKT 123456 tank false Fort Worth

FreightCar car5 MP 98765 box true Saint Louis

FreightCar car6 SP 567890 flat true Chicago

FreightCar car7 GMO 7878 hopper true Mobile

PassengerCar car8 KCS 7893 chair true Kansas City

PassengerCar car9 PAPX 145 sleeper true Tucson

PassengerCar car10 GN 744 combine false NONE

=========Following is the solution from problem 4.2=====================

#include

#include

#include

#include

#include

using namespace std;

class car

{

private:

string reportingMark;

int carNumber;

string kind;

bool loaded;

string destination;

public:

car()

{

reportingMark = "NONE";

carNumber = 0;

kind = "NONE";

loaded = false;

destination = "NONE";

}

car(const car &object)

{

reportingMark = object.reportingMark;

carNumber = object.carNumber;

kind = object.kind;

loaded = object.loaded;

destination = object.destination;

}

car(string repmark, int num, string k, bool load, string dest)

{

setUp(repmark, num, k, load, dest);

}

~car()

{

cout << "Killing car function";

}

bool friend operator==(car &c1, car &c2);

car & operator=(const car & carB);

// output function

void output()

{

cout << setw(15) << "Reporting Mark: " << setw(15) << reportingMark << endl;

cout << setw(15) << "Car Number: " << setw(15) << carNumber << endl;

cout << setw(15) << "Kind: " << setw(15) << kind << endl;

if (loaded)

cout << setw(15) << "Loaded: " << setw(15) << "true" << endl;

else

cout << setw(15) << "Loaded: " << setw(15) << "false" << endl;

cout << setw(15) << "Destination: " << setw(15) << destination << endl;

}

// Setting input values to data memnbers

void setUp(string repmark, int num, string k, bool load, string dest)

{

reportingMark = repmark;

carNumber = num;

kind = k;

loaded = load;

destination = dest;

}

};

car & car::operator=(const car & carB)

{

reportingMark = carB.reportingMark;

carNumber = carB.carNumber;

kind = carB.kind;

loaded = carB.loaded;

destination = carB.destination;

return * this;

}

bool operator==(car &c1, car &c2)

{

if(c1.reportingMark == c2.reportingMark && c1.carNumber == c2.carNumber)

return true;

return false;

}

class StringOfCars

{

private:

static const int ARRAY_MAX_SIZE = 10;

car *arrCar;

int size;

public:

StringOfCars();

StringOfCars(const StringOfCars &other);

~StringOfCars();

void push(const car &c);

car pop();

void output();

};

StringOfCars::StringOfCars()

{

arrCar = new car[ARRAY_MAX_SIZE];

size = 0;

}

StringOfCars::StringOfCars(const StringOfCars &other)

{

arrCar = new car[ARRAY_MAX_SIZE];

size = 0;

for(int i = 0; i < other.size; i++)

push(other. arrCar[i]);

}

StringOfCars::~StringOfCars()

{

delete []arrCar;

}

void StringOfCars::output()

{

if(size == 0)

{

cout << "Empty " << endl;

return;

}

cout << endl<<" ******* List of cars ********" << endl;

for(int di = 0; di < size; di++)

{

cout << (di+1) << "." << endl;

arrCar[di].output();

cout << endl;

}

cout << " *******************************" << endl;

}

void StringOfCars::push(const car &c)

{

if(size < ARRAY_MAX_SIZE)

arrCar[size++] = c;

}

car StringOfCars::pop()

{

return arrCar[--size];

}

// input function that read data from the file

bool input(StringOfCars &strcar)

{

string line;

ifstream read;

read.open("cardata4.txt");

if(read.is_open()) {

while(read.peek() != EOF) {

getline(read, line);

istringstream iss(line);

string type, reporting, kind, loded, dst;

int Number;

if (!(iss >> type >> reporting >> Number >> kind >> loded >> dst)) {

cout << "break";

break;

}

car car1(reporting, Number, kind, (loded == "true" || loded == "TRUE"), dst);

strcar.push(car1);

//cout << "Contents of car 1: ";

//car1.output();

}

}

else

{

cout << endl << "Could not open file....";

return false;

}

read.close();

return true;

}

int main()

{

cout << " TEST 1" << endl;

car car1("SP", 34567, "box", true, "Salt Lake City");

car car2 = car1;

car2.output();

cout << "***********************" << endl;

StringOfCars string1;

cout << " TEST 2" << endl;

cout << "STRING 1" << endl;

input(string1);

string1.output();

cout << "***********************" << endl;

cout << " TEST 3" << endl;

car car3 = string1.pop();

cout << "CAR 3" << endl;

car3.output();

cout << "STRING 1" << endl;

string1.output();

cout <<" ******************" << endl;

system("pause");

return 0;

}

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

The Database Relational Model A Retrospective Review And Analysis

Authors: C. J. Date

1st Edition

0201612941, 978-0201612943

More Books

Students also viewed these Databases questions

Question

What are CASE tools used for?

Answered: 1 week ago

Question

7. It is advisable to do favors for people whenever possible.

Answered: 1 week ago