Can someone tell me what\'s wrong with this program? and is it in c++? If it isn
ID: 666159 • Letter: C
Question
Can someone tell me what's wrong with this program? and is it in c++? If it isn't, please make it c++
Below is the program directions as well as the code.
Problem E4
Copy the solution from problem E3 and make the name E4.
In this problem we will use the StringOfCars class to contain Car,
FreightCar, and PassengerCar objects all in the same string of cars.
This works because a pointer of type Car * can point to Car objects as
well as point to the child FreightCar and PassengerCar objects.
Because we have pointers of type Car * that may point to any one of the
three types of objects, a StringOfCars object does not know which type
object will be encountered until execution time. The mechanism to select
the correct version of the function at execution time, rather than
having it fixed at compile time, is to use virtual functions. That is
why we made the setKind and destructor functions virtual earlier in this
assignment.
In the input function loop read one line from the file each time
throught the loop, look at the Type field in the record. If the type is
Car create a Car object and call the push function to put it in the
StringOfCars object. Similarly if the type is FreightCar, create and
push a FreightCar object. If it is a PassengerCar, create and push a
PassengerCar object.
We have a push member function that accepts a Car parameter, creates a
copy of the Car parameter that is a Car object in the heap, then puts
the pointer to that Car object into the array. Build another member
function, also named push, that takes a FreightCar parameter, creates a
FreightCar in the heap, and then puts the pointer to that FreightCar
object into the array. Also build a similar member function for a
PassengerCar.
The file for this problem should contain:
Type ARR number kind loaded destination
Car CN 819481 maintenance false NONE
Car SLSF 46871 business true Memphis
Car AOK 156 tender true McAlester
FreightCar MKT 123456 tank false Fort Worth
FreightCar MP 98765 box true Saint Louis
FreightCar SP 567890 flat true Chicago
FreightCar 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
Remove the code from the main funtion and replace it with the following
Define a StringOfCars object in the stack. Then pass that object to the
input function. Then call the output function for that object.
You may note that the pop member function and copy constructor for a
StringOfCars do not determine what type of car is being retrieved.
Fixing this problem is more than what I want you to do in the
assignment. So, ignore this problem.
#include <iostream>
#include <string>
#include <iomanip>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sstream>
#include <fstream>
using namespace std;
class Car{
//set data of the class as private
private:
string reportingMark; // string variable to store car member reportingMark
int carNumber; // int variable to store car member carNumber
string kind; // string variable to store car member kind
bool loaded; // boolean variable to store car member loaded
string destination; // string variable to store car member destination
public:
//Constructors
// Default constructor
Car()
{
reportingMark = ""; // an empty string
carNumber = 0;
kind = "other";
loaded = false;
destination = "NONE";
}
// Copy constructor
Car(const Car &obj)
{
reportingMark = obj.reportingMark;
carNumber = obj.carNumber;
kind = obj.kind;
loaded = obj.loaded;
destination = obj.destination;
}
// A constructor that accepts five constant reference parameters and calls setUp member function
Car(string mark, int num, string make, bool state, string dest);
// destructor
~Car()
{
};
// mutator member functions
void setReportingMark(string mark);
void setCarNumber(int num);
void setKind(string make);
void setLoaded(bool state);
void setDestination(string dest);
// accessor member functions
string getReportingMark() const;
int getCarNumber() const;
string getKind() const;
bool getLoaded() const;
string getDestination() const;
// member functions
void setUp(string, int, string, bool, string, Car);
void output(Car);
// Car operator=
Car &operator=(const Car &carB);
};
//function definition
void Car::setReportingMark(string mark)
{
reportingMark = mark;
};
void Car::setCarNumber(int num)
{
carNumber = num;
};
void Car::setKind(string make)
{
kind = make;
};
void Car::setLoaded(bool state)
{
loaded = state;
};
void Car::setDestination(string dest)
{
destination = dest;
};
string Car::getReportingMark() const
{
return reportingMark;
};
int Car::getCarNumber() const
{
return carNumber;
};
string Car::getKind() const
{
return kind;
};
bool Car::getLoaded() const
{
return loaded;
};
string Car::getDestination() const
{
return destination;
};
// Car operator= **************************************************
Car & Car::operator=(const Car & carB)
{
reportingMark = carB.reportingMark;
carNumber = carB.carNumber;
kind = carB.kind;
loaded = carB.loaded;
destination = carB.destination;
return * this;
}
// function prototypes
void input ();
void output (Car);
void setUp (string, int, string, bool, string, Car&);
class StringOfCars{
static const int ARRAY_MAX_SIZE = 10;
Car *object[ARRAY_MAX_SIZE];
int currentCar;
// default constructor
StringOfCars();
// copy constructor
StringOfCars(const Car &);
// destructor
~StringOfCars();
// push function
// pop function
// output function
};
/* ********** StringOfCars member functions ********** */
// default constructor
StringOfCars()
{
static const int ARRAY_MAX_SIZE = 10;
Car *object[ARRAY_MAX_SIZE];
int curNum = 0;
}
// copy constructor
StringOfCars(const StringOfCars &obj)
{
static const int ARRAY_MAX_SIZE = 10;
Car *object[ARRAY_MAX_SIZE] = new object[ARRAY_MAX_SIZE];
}
// destructor
~StringOfCars()
int main()
{
// call input function to read the data from user
input();
return 0;
}
/* ******************** output ********************
print all data
*/
void output(Car car)
{
cout << "ReportingMark: ";
// convert string to character array
char u;
char str[(car.getReportingMark()).length()];
strcpy(str, (car.getReportingMark()).c_str());
// change the lowercase to uppercase letter by letter
for(int n = 0; n < (car.getReportingMark()).length(); n++)
{
u = str[n];
putchar (toupper(u));
}
cout << endl;
cout << "CarNumber: " << car.getCarNumber() << endl;
cout << "Kind: " << car.getKind() << endl;
if(car.getLoaded() == true)
{
cout << "Loaded: true ";
cout << "Destination: " << car.getDestination() << endl;
}
else if(car.getLoaded() == false)
{
cout << "Loaded: flase ";
if(car.getDestination() == "none" || car.getDestination() == "NONE")
cout << "Destination: NONE ";
else
cout << "Destination: " << car.getDestination() << endl;
}
cout << endl;
}
/* ******************** setUp ********************
pass data of variables got from input function to variables declared in class car
*/
void setUp(string mark, int num, string make, bool state, string dest, Car &car1)
{
// call the mutator member functions to pass the data to the class
car1.setReportingMark(mark);
car1.setCarNumber(num);
car1.setKind(make);
car1.setLoaded(state);
car1.setDestination(dest);
};
/* ******************** input ********************
read data from a file
*/
void input()
{
// define variables
string mark;
int num;
string make;
bool state;
string dest;
// declare the string "Type" to get the first column of the data out of the way
string Type;
// declare a string for reading boolean type data
string st;
// declare and open the file
ifstream inputFile;
inputFile.open("inputFile.txt");
// declare a Car object
Car temp;
// print error message and exit the program if the file does not exist
if(!inputFile) // if the file does not exist
{
fputs("Fail to open the file", stderr);
exit(0); // exit the program
}
else
{
// use while loop to read the file line by line;
// the peek function returns EOF is no next character exists
while(inputFile.peek() != EOF)
{
// read data from file
inputFile >> Type;
inputFile >> mark;
inputFile >> num;
inputFile >> make;
// declare what false and true readings from the file means
inputFile >> st;
if(st == "false")
state = false;
else
state = true;
// get rid of the leading whitespace before using getline function to read the destination
while(inputFile.peek() == ' ')
{
inputFile.get();
}
getline(inputFile, dest);
// call the setUp function to pass the data to the class
setUp(mark,num,make,state,dest,temp);
// call the output function to print the data
output(temp);
}
}
// close the file
inputFile.close();
cout << endl;
}
Explanation / Answer
In bold is problem E3
#include<iostream>
#include<string>
#include<vector>
using namespace std;
//Car class
//Defined enum here
enum Kind{business,maintenance,other,box,tank,flat,otherFreight,chair,seater,otherPassenger};
//Defined array of strings
string KIND_ARRAY[]={"business","maintenance","other,box","tank,flat","otherFreight","chair","seater","otherPassenger"};
class Car
{
protected: //changed to protected
string reportingMark;
int carNumber;
Kind kind; //changed to Kind
bool loaded;
string destination;
public:
//Defined setKind function
Kind setKind(string knd)
{
for(int i=0;i<8;i++) //repeat upto 8 kinds
{
if(knd.compare(KIND_ARRAY[i])==0) //if matched in Array
kind=(Kind)i; //setup that kind
}
return kind;
}
//Default constructor
Car()
{
}
//Parameterized constructor
Car(string _reportingMark,int _carNumber,string _kind,bool _loaded,string _destination)
{
reportingMark = _reportingMark;
carNumber = _carNumber;
kind = setKind(_kind);
loaded = _loaded;
destination = _destination;
}
//Destructor
~Car()
{
}
//Copy constructor
Car( const Car &other)
{
reportingMark = other.reportingMark;
carNumber = other.carNumber;
kind = other.kind; //calls setup function
loaded = other.loaded;
destination = other.destination;
}
Car operator=(Car other)
{
reportingMark = other.reportingMark;
carNumber = other.carNumber;
kind = other.kind;
loaded = other.loaded;
destination = other.destination;
return *this;
}
friend bool operator==(Car c1,Car c2);
void setUp() //Implemented setup() code here
{
string knd;
//cout<<"Implementing Progress ";
cout<<"Enter Type/Kind of car : ";
cin>>knd;
setKind(knd);
}
void output()
{
cout<<"Reporting Mark: "<<reportingMark<<endl;
cout<<"Card Number: "<< carNumber<<endl;
cout<<"Kind: "<< KIND_ARRAY[kind]<<endl; //Modified printing of Kind
cout<<"Loaded: "<< loaded<<endl;
cout<<"Destination: "<< destination<<endl;
}
};
//Friend function
bool operator==(Car c1,Car c2)
{
if(c1.reportingMark==c2.reportingMark
&& c1.carNumber == c2.carNumber
&& c1.kind == c2.kind
&& c1.loaded == c2.loaded
&& c1.destination == c2.destination)
{
return true;
}
else
{
return false;
}
}
//Class FreightCar
class FreightCar:public Car
{
public:
//Default constructor
FreightCar()
{
Car();
}
//Parameterized constructor
FreightCar(string _reportingMark,int _carNumber,string _kind,bool _loaded,string _destination):Car(_reportingMark,_carNumber,_kind,_loaded,_destination) //calling base class constructor
{
}
//Defined virtual setKind() function
virtual Kind setKind(string knd)
{
for(int i=0;i<8;i++) //repeat upto 8 kinds
{
if(knd.compare(KIND_ARRAY[i])==0) //if matched in Array
kind=(Kind)i; //setup that kind
}
return kind;
}
//Virtual Destructor
virtual ~FreightCar()
{
}
};
//Class PassengerCar here
class PassengerCar:public Car
{
public:
//Default constructor
PassengerCar()
{
Car();
}
//Parameterized constructor
PassengerCar(string _reportingMark,int _carNumber,string _kind,bool _loaded,string _destination):Car(_reportingMark,_carNumber,_kind,_loaded,_destination)
{
}
//Defined virtual setKind() function
virtual Kind setKind(string knd)
{
for(int i=0;i<8;i++) //repeat upto 8 kinds
{
if(knd.compare(KIND_ARRAY[i])==0) //if matched in Array
kind=(Kind)i; //setup that kind
}
return kind;
}
//Virtual Destructor
virtual ~PassengerCar()
{
}
};
/* ********** StringOfCars member functions ********** */
/* dont use StringOfCar in this
class StringOfCar
{
private:
Car *cars; //pointer of cars
int carCount; //defined additional data member carCount
public:
//Default constructor
StringOfCar()
{
cars=NULL;
carCount=0;
}
//Parameterized constructor
StringOfCar(Car *_cars)
{
cars = _cars;
carCount=0;
}
//Copy construtor
StringOfCar( const StringOfCar &obj)
{
cars = obj.cars;
carCount=0;
}
//Destructor
~StringOfCar()
{
//deleting each car allocated
for(int i=0;i<carCount;i++)
delete cars;
}
int getCount() //UFD here defined here
{return carCount;}
void output()
{
for(int i=0; i<carCount; i++)
{
cars[i].output();
}
}
void push(Car car)
{
cars[carCount++]=car;
}
Car pop()
{
Car lastObj = cars[--carCount]; //getting top of stack of cars
return lastObj;
}
void input()
{
cout<<"In progress! ";
}
};*/
int main()
{
//Removed main() code and added modified code here
Car car1("SLSF",46871,"wrecker",true,"Memphis");
car1.output();
FreightCar car2("MP",98765,"gondola",true,"Saint Louis");
car2.output();
PassengerCar car3("PAPX",145,"combine",true,"Tucson");
car3.Car::output();
return 0;
}
Problem E4
Copy the solution from problem E3 and make the name E4.
In this problem we will use the StringOfCars class to contain Car,
FreightCar, and PassengerCar objects all in the same string of cars.
This works because a pointer of type Car * can point to Car objects as
well as point to the child FreightCar and PassengerCar objects.
Because we have pointers of type Car * that may point to any one of the
three types of objects, a StringOfCars object does not know which type
object will be encountered until execution time. The mechanism to select
the correct version of the function at execution time, rather than
having it fixed at compile time, is to use virtual functions. That is
why we made the setKind and destructor functions virtual earlier in this
assignment.
In the input function loop read one line from the file each time
throught the loop, look at the Type field in the record. If the type is
Car create a Car object and call the push function to put it in the
StringOfCars object. Similarly if the type is FreightCar, create and
push a FreightCar object. If it is a PassengerCar, create and push a
PassengerCar object.
We have a push member function that accepts a Car parameter, creates a
copy of the Car parameter that is a Car object in the heap, then puts
the pointer to that Car object into the array. Build another member
function, also named push, that takes a FreightCar parameter, creates a
FreightCar in the heap, and then puts the pointer to that FreightCar
object into the array. Also build a similar member function for a
PassengerCar.
The file for this problem should contain:
Type ARR number kind loaded destination
Car CN 819481 maintenance false NONE
Car SLSF 46871 business true Memphis
Car AOK 156 tender true McAlester
FreightCar MKT 123456 tank false Fort Worth
FreightCar MP 98765 box true Saint Louis
FreightCar SP 567890 flat true Chicago
FreightCar 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
Remove the code from the main funtion and replace it with the following
Define a StringOfCars object in the stack. Then pass that object to the
input function. Then call the output function for that object.
You may note that the pop member function and copy constructor for a
StringOfCars do not determine what type of car is being retrieved.
Fixing this problem is more than what I want you to do in the
assignment. So, ignore this problem.
Problem E-3
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
enum Kind {business, maintenance, other, box, tank, flat, otherFreight, chair, sleeper, otherPassenger};
string KIND_ARRAY[]={"business", "maintenance", "other", "box", "tank", "flat", "otherFreight", "chair", "sleeper",
"otherPassenger"};
class Car
{
public:
string reportingMark;
int carNumber;
Kind kind;
bool loaded;
string destination;
public:
// #1 DEFAULT constructor
Car()
{
setUp("", 0, other,false, "NONE");
}
// #2 COPY constructor
Car(const Car &obj)
{
setUp(obj.reportingMark, obj.carNumber ,obj.kind, obj.loaded,obj.destination);
}
// #3 accept five constant referance parameters constructor
Car(string reportMark, int cNumber, Kind knd, bool load, string destition)
{
setUp(reportMark, cNumber, knd, load, destition);
}
// #4 DESTRUCTOR
virtual ~Car()
{
}
// Setup
virtual void setUp(string reportMark, int cNumber, Kind knd, bool load, string destition);
// output member function
void output();
// setKind
virtual void setKind(const Kind k)
{
if (k != business && k != maintenance)
kind = other;
}
//Operator Overloading
Car & operator = (const Car & carB);
//operator function
friend bool operator ==(Car A, Car b);
};
/**************************************
* FreightCar Class *
**************************************/
class FreightCar : public Car
{
public:
FreightCar():Car()
{
}
FreightCar(const FreightCar & old) : Car(old)
{
}
FreightCar(string a, int b, Kind c, bool d, string e):Car(a, b, c, d, e)
{
}
//This could be made Virtual but it doesn't have to since its a derived class.
virtual void setKind(const Kind k)
{
if (k != box && k != tank && k != flat)
kind = otherFreight
}
};
/*****************************************
* PassengerCar Class *
*****************************************/
class PassengerCar : public Car
{
public:
PassengerCar():Car()
{
}
PassengerCar(const PassengerCar &old) : Car(old)
{
}
PassengerCar(string a, int b, Kind c, bool d, string e) : Car(a, b, c, d, e)
{
}
//This could be made Virtual but it doesn't have to since its a derived class.
virtual void setKind(const Kind k)
{
if (k != chair && k != sleeper)
kind = otherPassenger;
}
};
/*****************************************
* StringOfCars Class *
*****************************************/
class StringOfCars
{
Car **ptr;
static const int ARRAY_MAX_SIZE = 10;
int size;
public:
// DEFAULT constructor
StringOfCars()
{
ptr = new Car*[ARRAY_MAX_SIZE];
size = 0;
}
//COPY constructor
StringOfCars(const StringOfCars &old)
{
ptr = new Car*[ARRAY_MAX_SIZE];
for (int i = 0; i < ARRAY_MAX_SIZE; i++)
{
Car *p = new Car;
p->reportingMark = old.ptr[i]->reportingMark;
p->carNumber = old.ptr[i]->carNumber;
p->kind = old.ptr[i]->kind;
p->loaded = old.ptr[i]->loaded;
p->destination = old.ptr[i]->destination;
ptr[i]=p;
}
size = old.size;
}
//DESTRUCTOR
virtual ~StringOfCars()
{
delete [] ptr;
}
//Push Function
void push(Car & add);
//Pop Function
void pop(Car & a);
//Output Function
void output();
};
void input(StringOfCars &a);
/********************************
* main function *
********************************/
int main()
{
StringOfCars StringOfcar1;
input(StringOfcar1);
StringOfCars StringOfcar2 = StringOfcar1;
StringOfcar2.output();
return 0;
}
void buildCar(string reportMark, int cNumber, Kind knd, bool load, string destition, StringOfCars & a)
{
Car Cartest(reportMark, cNumber, knd, load, destition);
Cartest.setKind(knd);
a.push(Cartest);
}
void buildPassengerCar(string reportMark, int cNumber, Kind knd, bool load, string destition, StringOfCars & a)
{
PassengerCar Cartest(reportMark, cNumber, knd, load, destition);
Cartest.setKind(knd);
a.push(Cartest);
}
void buildFreightCar(string reportMark, int cNumber, Kind knd, bool load, string destition, StringOfCars & a)
{
FreightCar Cartest(reportMark, cNumber, knd, load, destition);
Cartest.setKind(knd);
a.push(Cartest);
}
void Car::output()
{
cout << left;
cout << setw(20) << "reportingMark" << " " << reportingMark << endl;
cout << setw(20) << "carNumber" << " " << carNumber << endl;
cout << setw(20) << "kind" << " " << KIND_ARRAY[kind] << endl;
if (loaded)
cout << setw(20) << "loaded" << " " << "true" << endl;
else
cout << setw(20) << "loaded" << " " << "false" << endl;
cout << setw(20) << "destination" << " " << destination << endl;
cout << endl;
}
void Car::setUp(string reportMark, int cNumber, Kind knd, bool load, string destition)
{
reportingMark=reportMark;
carNumber=cNumber;
kind =knd;
loaded=load;
destination=destition;
}
/********************************
* operator function *
********************************/
bool operator == (Car A, Car B)
{
if (A.reportingMark == B.reportingMark && A.carNumber == B.carNumber)
return true;
else
return false;
}
/***********************************
* Operator Overloading *
***********************************/
Car & Car::operator=(const Car & carB)
{
setUp(carB.reportingMark, carB.carNumber, carB.kind, carB.loaded, carB.destination);
return * this;
}
/********************************************
* Output function for StringOfCar *
********************************************/
void StringOfCars::output()
{
cout << "car number is:" << size << endl;
for (int i = 0; i < size; i++)
{
ptr[i]->output();
cout << endl;
}
}
/****************************
* push function *
****************************/
void StringOfCars::push(Car &add)
{
Car *pointer = new Car;
*pointer = add;
ptr[size] = pointer;
size++;
}
/***************************
* pop function *
***************************/
void StringOfCars::pop(Car &a)
{
a = *ptr[size-1];
delete ptr[size-1];
size--;
}
/********************************
* input function *
********************************/
void input(StringOfCars & a)
{
ifstream inputFile;
inputFile.open("data.txt");
if (!inputFile)
{
cout << "File open failure";
}
else
{
string type;
string reportingMark;
string order;
int carNumber;
Kind kind = otherPassenger;
string kindinput;
bool loaded = false;
string loadedinput;
string destination;
while (inputFile.peek() != EOF)
{
inputFile >> type;
inputFile >> order;
inputFile >> reportingMark;
inputFile >> carNumber;
inputFile >> kindinput;
kind = otherFreight;
for (int i = 0; i < 9; i ++)
{
if (kindinput == KIND_ARRAY[i])
{
kind = static_cast<Kind>(i);
break;
}
}
inputFile >> loadedinput;
if (loadedinput == "true")
loaded = true;
else if (loadedinput == "false")
loaded = false;
else
cout << "wrong loaded input"<<endl;
while (inputFile.peek() == ' ')
inputFile.get();
getline(inputFile, destination);
if (type == "Car")
buildCar(reportingMark,carNumber,kind,loaded,destination, a);
else if (type == "FreightCar")
buildFreightCar(reportingMark, carNumber, kind, loaded, destination, a);
else if (type == "PassengerCar")
buildPassengerCar(reportingMark, carNumber, kind, loaded, destination, a);
}
}
inputFile.close();
}
=> here is my solution for E3, which is needed for question 4.
Question 4:
Copy the solution from problem E3 and make the name E4.
In this problem we will use the StringOfCars class to contain Car, FreightCar, and PassengerCar objects all in the same string of cars. This works because a pointer of type Car * can point to Car objects as well as point to the child FreightCar and PassengerCar objects.
Because we have pointers of type Car * that may point to any one of the three types of objects, a StringOfCars object does not know which type object will be encountered until execution time. The mechanism to select the correct version of the function at execution time, rather than having it fixed at compile time, is to use virtual functions. That is why we made the setKind and destructor functions virtual earlier in this assignment.
In the input function loop read one line from the file each time throught the loop, look at the Type field in the record. If the type is Car create a Car object and call the push function to put it in the StringOfCars object. Similarly if the type is FreightCar, create and push a FreightCar object. If it is a PassengerCar, create and push a PassengerCar object.
We have a push member function that accepts a Car parameter, creates a copy of the Car parameter that is a Car object in the heap, then puts the pointer to that Car object into the array. Build another member function, also named push, that takes a FreightCar parameter, creates a FreightCar in the heap, and then puts the pointer to that FreightCar object into the array. Also build a similar member function for a PassengerCar.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.