Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN C++, thank you! In Problem D1 we will use a file to contain the data which we will read into the program. In Problem

IN C++, thank you!

In Problem D1 we will use a file to contain the data which we will read into the program.

In Problem D2 we will build a StringOfCar class which will contain a sequence of Car objects. Also, an operator= member function will be added to the Car class.

Problem D1

Order of functions in the code is unchanged from C3.

Car constructors and destructor within the Car class definition

default constructor

copy constructor

other constructors

destructor

main

Car member functions declared within the Car class but defined later

setup

output

global functions

operator== with Car parameters

input

In this problem, we will read cars from a file, rather than typing them in from the keyboard. Do the steps in order to build the solution to this problem.

Copy and clean up code from problem C3.

Copy problem C3.

Change the name to Assignment D, Problem D1

Remove everything from the main function.

Remove the execution results.

Create a file to use for input. This is good because we will be using the input many times.Create a file containing the following three lines of data (Omit the heading line).

carType ARR number kind loaded destination Car CN 819481 maintenance false NONE Car SLSF 46871 business true Memphis Car AOK 156 tender true McAlester 

Modify the input function.

Remove the & from the parameters in the function header, so they are all values rather than references.

Move the parameters from the function header and put them within the input function, so they are now all local variables.

Remove the parameters from the prototype for the input function, so it matches the function header.

In the input function declare a string named carType. In the current file all the carType values are "Car". Later we will see different values.

Open the file. If the open fails, send a message to stderr and exit the program.

Use a loop to process each line from the file. Hint: you can do this with the following while statement, which loops as long as there is data in the file to be read:

 while(inputFile.peek() != EOF) 

The peek function will return EOF if there is no more data, so we will leave the loop then.Within this loop, each line in the file will provide the data for a Car. In all the reads within the input function, There are two differences from the read activity in assignment C:

Remove the user promp. We do not want it because we are reading from a file.

read using the inputFile object, rather than using the stdin object.

Read the carType field first. It just indicates that we are building an object of type Car.

Read each of the fields: ARR, number, kind, loaded

Always read the destination field even if the Car is not loaded. The destination will be NONE in the input file when that is the case.

Hint: We need to use getline when reading the destination. using >> skips leading white space before reading the data. getline does not skip this leading whitespace. So, before using getline use the following code:

 while(inputFile.peek() == ' ') inputFile.get(); 

peek looks at the next character you are about to read. If it is a space, get is used to read the space character, to get it out of the way.

If the carType is "Car", declare a Car object named temp using the constructor that takes 5 parameters.

Call the output function for the Car named temp.

If the carType is not "Car", send an error message and do nothing.

At the bottom of the input function, close the file.

Call the input function from the main function with no arguments.

Problem D2

Copy the solution for problem D1 and change the problem number to D2.

Order of functions in the code: Note that the member function operator= has been added to the Car class. Note that the StringOfCars class and its member functions has been added.

Car constructors and destructor within the Car class definition

default constructor

copy constructor

other constructors

destructor

StringOfCars constructors and destructor within the StringOfCar class definition

default constructor

copy constructor

other constructors

destructor

main

Car member functions declared within the Car class but defined later

setup

output

operator=

StringOfCars member functions declared within the StringOfCars class but defined later

output

push

pop

global functions

operator== with Car parameters

input

Copy the following operator= overload member function that returns the left hand operator by reference. Also code a prototype for this function within the Car class.

/* ******************** 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; } 

Several cars coupled together are referred to as a string of cars.

Create another class called StringOfCars, containing:

a pointer to an array of Car objects in the heap, named ptr.

a static const int set to 10, named ARRAY_SIZE.

an int containing the current number of Cars in the array, named carCount.

a default constructor which gets space in the heap for the array of Car elements, and sets the the carCount to zero.

a copy constructor which gets new space in the heap for an array of Car elements with size ARRAY_SIZE. It copies the each Car and also copies the carCount.

a destructor which returns the space to the heap.

a push function which adds a car to the string of cars. Exit with an error message if the string of cars is full.

a pop function which removes a car from the string of cars, Last In First Out (LIFO). The return type is void. There is one prarameter which is a reference to a Car object. Put the Car object that was taken out from the array into the parameter Car object. Exit with an error message if the string is empty.

an output function which prints a heading for each car: car number n where n is the position in the array starting from 1 for the first car and then uses the Car output function to print the data for each car Or, if the array is empty prints: NO cars

Make the following changes in the input function:

Change the input function to have a parameter that is a reference to a StringOfCars.

After creating a Car, do not print it.

Instead, push the Car onto the StringOfCars.

Replace the main function to do the following tests:

Test the Car operator= function. Print: TEST 1 Creat a Car object named car1 by using a constructor with the following constants as initial values: reportingMark: SP carNumber: 34567 kind: business loaded: true destination: Salt Lake City Create a Car object named car2 using the default constructor. Use the = operator to copy the data from car1 to car 2. Print car2

Test the StringOfCar push function. Add to main: Print: TEST 2 Create a default StringOfCars object named string1. Pass string1 to the input function. Use the same file as in problem D1. Print: STRING 1 In the main function, print string1.

Test the StringOfCars pop function. Add to main: Print: TEST 3 Create a car named car3. Pop one car from string1 into car3. Print: CAR 3 Print car3. Print: STRING 1 Then print the contents of string1 again

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_2

Step: 3

blur-text-image_3

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

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago