In bold is problem E3 #include<iostream> #include<string> #include<vector> using
ID: 666025 • Letter: I
Question
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.
Explanation / Answer
Answer:
/****** CPP Program for car Class***********/
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<fstream>
#include<conio.h>
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 ********** */
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();
}
}
virtual void push(Car *car)
{
cars[carCount++]=*car;
}
virtual void push(FreightCar *fc)
{
carCount++;
cars[carCount]=*fc;
}
virtual void push(PassengerCar *pc)
{
carCount++;
cars[carCount]=*pc;
}
Car pop()
{
Car lastObj = cars[--carCount]; //getting top of stack of cars
return lastObj;
}
void input(StringOfCar stack1)
{
string carType;
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
ifstream carfile("inputfile.txt");
if(carfile.is_open())
{
while(carfile>>carType)
{
carfile>>reportingMark;
carfile>>carNumber;
carfile>>kind;
carfile>>loaded;
carfile>>destination;
if(carType=="Car")
{
stack1.push(new Car(reportingMark,carNumber,kind,loaded,destination));
}
else if (carType=="PassengerCar")
{
stack1.push(new PassengerCar(reportingMark,carNumber,kind,loaded,destination));
}
else if(carType=="FreightCar")
{
stack1.push(new FreightCar(reportingMark,carNumber,kind,loaded,destination));
}
}
}
}
};
int main()
{
StringOfCar stack1;
stack1.input(stack1);
stack1.output();
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.