The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Hi! So I'm having an issue getting started with the first part of this assignment (see uploaded Assignment E1.doc for instructions); I have attached my starter code as this assignment is based on a previous assignment (see Problem_E1.cpp for code)
Assignment E1.doc
Use the style guide, including Additonal styles beginning with
Assignment D.
In Problem E1 we use an array of pointers to Car and make a copy of
the users Car objects in the heap.
In Problem E2 we will use inheritance to provide specizations of the Car
class in the child classes FreightCar and PassengerCar.
Then in Problem E3 we will put this all together with a StringOfCars
class that can contain Car, FreightCar, or PassengerCar objects, or a
mix of these three.
Problem E1
Copy the solution from problem D2 and make the name E1.
Keep the same order for the functions as in problem D2.
1. Car constructors and destructor within the Car class definition
a. default constructor
b. copy constructor
c. other constructors
d. destructor
2. StringOfCars constructors and destructor within the StringOfCar
class definition
a. default constructor
b. copy constructor
c. other constructors
d. destructor
3. main
4. Car member functions declared within the Car class but defined
later
a. setup
b. output
c. operator=
5. StringOfCars member functions declared within the StringOfCars
class but defined later
a. output
b. push
c. pop
6. global functions
a. operator== with Car parameters b. input
In the StringOfCars class, Change the array of Car objects to an array
of pointers to Car objects. To do this you need to change the pointer
type from Car * to Car **
Then we will need to make changes to use these pointers. The
constructors, destructor, and member functions will have similar
operation, but will need changes to work with pointers. I suggest you
make the following changes, one at a time. All the StringOfCars constructors will get space for Car *
elements, rather than Car elements in the array. To allow this the
pointer in the private data for the StringOfCars class will be: Car **
ptr;
All the StringOfCars constructors will set each unused element in
the array to zero. This might best be done by setting them all to
zero before using the array.
The output function needs to dereference the pointers to get at
the Car objects.
Change the push function. It will take a parameter that is a Car by
constant reference. It will allocate space in the heap for one Car
object that is a copy of the Car parameter and then put the pointer
to that Car in the array. Then it will increment the carCount.
The copy constructor will get space for the array of Car* elements
and set them to zero. It will set the carCount to zero. Then it will
use the push function to push each each Car object into the array.
The pop function will take a Car parameter by reference. It will
copy the Car to the reference parameter. Then it will delete the
Car from the heap. It will set the entry in the array that is no longer
used to 0. It will decrement the carCount.
The distructor will need to delete each Car pointed to by the array,
and then delete the array.
/*Jill Kawakami
CIS 22B, WINTER 2017
Assignment E, Problem E1
Utilize code from Problem D2; change array of Car objects to array of pointers
to Car objects (Car * to Car **)
Order of functions is unchanged from D2
1. Car constructors/destructor in Car class definition
default, copy, other constructors, destructor
2.StringofCars constructors/desctructors in StringOfCars class definition
default, copy, other constructors, destructor
3. Main
4. Car member functions declared in Car class
setup, output, operator=
5. StringOfCars member functions declared in StringOfCars class
output, push, pop
6. Global functions
operator== with Car parameters, input*/
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
class Car {
private:
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
public:
/*Default constructor*/
Car();
/*Copy constructor*/
Car(const Car &car);
/*Constructor with five parameters*/
Car(const string reportingMark, const int carNumber, const string kind,
const bool loaded, const string destination);
/*Destructor*/
~Car() {
}
/*Car member function prototypes*/
void setup(string reportingMark, int carNumber, string kind, bool loaded,
string destination);
void output();
friend bool operator==(const Car car1, const Car car2);
Car &Car::operator=(const Car & carB);
};
/* ******************** Car::Car ********************
Member function; default constructor; calls setup()*/
Car::Car() {
reportingMark = "";
carNumber = 0;
kind = "other";
loaded = false;
destination = "NONE";
}
/* ******************** Car::Car ********************
Member function; copy constructor; calls setup()*/
Car::Car(const Car &car) {
setup(car.reportingMark, car.carNumber, car.kind, car.loaded,
car.destination); }
/* ******************** Car::Car ********************
Member function, parameterized constructor; calls setup()*/
Car::Car(const string reportingMark, const int carNumber, const string kind,
const bool loaded, const string destination) {
setup(reportingMark, carNumber, kind, loaded, destination);
}
class StringOfCars {
private:
Car *ptr;
const static int ARRAY_SIZE = 10;
int carCount;
public:
/*Default constructor*/
StringOfCars();
/*Copy constructor*/
StringOfCars(const StringOfCars &carString);
/*Destructor*/
~StringOfCars() {
delete ptr;
}
/*StringOfCars member function prototypes*/
void output();
void push(Car carElement);
void pop(Car &carObject);
};
/* ******************** StringOfCars::StringOfCars ********************
Member function; default constructor; gets space for array of Car elements;
carCount = 0*/
StringOfCars::StringOfCars() {
ptr = new Car[ARRAY_SIZE];
carCount = 0;
}
/* ******************** StringOfCars::StringOfCars ********************
Member function; copy constructor; gets new space for array of Car elements;
copies each Car and carCount*/
StringOfCars::StringOfCars(const StringOfCars &carString) {
ptr = new Car[ARRAY_SIZE];
carCount = carString.carCount;
for (int i = 0; i < ARRAY_SIZE; i++) {
ptr[i] = carString.ptr[i];
}
}
/*Global function prototypes*/
void input(StringOfCars &carString);
bool operator==(const Car car1, const Car car2);
int main() {
cout << "TEST 1\n";
Car car1("SP", 34567, "business", true, "\tSalt Lake City");
Car car2;
car2 = car1;
car2.output();
cout << "TEST 2";
StringOfCars string1;
input(string1);
cout << "\nSTRING 1\n";
string1.output();
cout << "TEST 3"; Car car3;
string1.pop(car3);
cout << "\nCAR 3\n";
car3.output();
cout << "STRING 1\n";
string1.output();
return 0;
}
/***************************************************
CAR CLASS FUNCTIONS - setup, output
***************************************************/
/* ******************** Car::setup ********************
Car function; stores data in object; void */
void Car::setup(string reportMark, int carNum, string carKind, bool isLoaded,
string dest) {
reportingMark = reportMark;
carNumber = carNum;
kind = carKind;
loaded = isLoaded;
destination = dest;
}
/* ******************** Car::output ********************
Car function; displays data; void */
void Car::output() {
cout << "reportingMark:\t" << reportingMark << "\n";
cout << "carNumber:\t" << carNumber << "\n";
cout << "kind:\t\t" << kind << "\n";
if (loaded) {
cout << "loaded:\t\ttrue" << endl;
cout << "destination:" << destination << "\n" << endl;
}
else {
cout << "loaded:\t\tfalse" << endl;
cout << "destination: " << destination << "\n" << endl;
}
}
/* ******************** Car::operator= ********************
sets the values in the left hand object from the right hand object*/
Car &Car::operator=(const Car & carB) {
setup(carB.reportingMark, carB.carNumber, carB.kind, carB.loaded,
carB.destination);
return *this;
}
/***************************************************
STRINGOFCARS CLASS FUNCTIONS - output, push, pop
***************************************************/
/* ******************** StringOfCars::output ********************
StringOfCars function; prints a heading for each car; calls Car ouput function
to print data*/
void StringOfCars::output() {
for (int i = 0; i < carCount; i++) {
if (carCount == 0) {
cout << "NO cars" << endl;
}
else {
cout << "Car number " << i + 1 << endl;
ptr[i].output();
}
}
}
/* ******************** StringOfCars::push ******************** StringOfCars function; adds a car to string of cars; exit with error message if
stack is full*/
void StringOfCars::push(Car carObject) {
if (carCount >= ARRAY_SIZE) {
cout << "Error, stack is full." << endl;
exit(0);
}
else {
ptr[carCount] = carObject;
carCount++;
}
}
/* ******************** StringOfCars::pop ********************
StringOfCars function; removes a car from string of cars LIFO; exit with error
message if stack is empty*/
void StringOfCars::pop(Car &carObject) {
if (carCount == 0) {
cout << "Error, stack is empty." << endl;
exit(0);
}
else {
carObject = ptr[carCount - 1];
carCount--;
}
}
/***************************************************
GLOBAL FUNCTIONS - operator==, input
***************************************************/
/* ******************** operator== ********************
Friend function; tests 2 objects equality based on reportingMark and carNumber
ONLY*/
bool operator== (const Car car1, const Car car2) {
return (car1.reportingMark == car2.reportingMark && car1.carNumber ==
car2.carNumber);
}
/* ******************** input ********************
Allows user to input data; void */
void input(StringOfCars &carString) {
string carType;
string reportingMark;
int carNumber;
string kind;
bool loaded = false;
string isLoaded;
string destination;
ifstream inputFile;
inputFile.open("C:\\Users\\X301-Jill\\Documents\\carfileD1.txt");
if (!inputFile) {
fputs("Error in opening file", stderr);
exit(0);
}
else {
while (inputFile.peek() != EOF) {
inputFile >> carType;
inputFile >> reportingMark;
inputFile >> carNumber;
inputFile >> kind;
inputFile >> isLoaded;
if (isLoaded == "true") {
loaded = true; destination); }
else if (isLoaded == "false") {
loaded = false;
}
while (inputFile.peek() == ' ') {
inputFile.get();
getline(inputFile, destination);
if (carType == "Car") {
Car temp(reportingMark, carNumber, kind, loaded,
carString.push(temp);
}
else {
cout << "Error in car type.\n";
} }
}
inputFile.close();
} } /* Execution results
*/
Â
Attachments: