Create a default string of cars in main

Assignment Help Programming Languages
Reference no: EM131149005

Assignment 5

Use the same format for problem and function headings as assignment 1.

Problem 5.1

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

Problem 5.2

Copy the solution from problem 5.1.

In this problem you will change the StringOfCars class so it has an array of pointers to objects, rather than an array of objects themselves. This will allow us to have a string of cars that contains Car, FreightCar, and PassengerCar objects all in the same string of cars. This works because a pointer of type Car * can be made to point to Car objects as well as point to the child FreightCar and PassengerCar objects.

Remove the call to the output member function from the three build funtions: buildCar, buildFreightCar, and buildPassengerCar.

Because you have pointers of type Car * that may point to any one of the three types of objects, there is a problem. The system does not know what type object will be encountered until execution time. That means a system is needed so the functions that are overridden need to have a mechanism to select the correct version of the function at execution time, rather than having it fixed at compile time. This is done with the virtual declaration. To do this make the declaration of the setKind and the declaration of the ~Car functions virtual in the Car class.

This is only done in the declaration, not the definition of the function.

This is only done in the parent class, not the children classes.

To change the class StringOfCars, comment out all the code in the member functions of the StringOfCars class and fix them one or two at a time in the following order. These are similar to the previous functions, but changed to allow for the fact that we are putting pointers to cars in the array.

• Build the default constructor first. Create a default string of cars in main.
• Build an output function, similar to the old one, but dereferrencing the pointers.
• Write a push function which adds a car to the string of cars. It takes a Car by constant reference, allocates space in the heap, makes a copy of the Car, and puts the pointer to the Car in the array.
• Write a copy constructor similar to the old one, but it gets space for each car and copies each one, as well as getting space for the array.
• omit the pop member function.

Add to the build functions a call to push the objects onto the string of cars.

Remove the output from the build functions.

Test the copy constructor by making stringOfCars2 in the stack for main that is a copy of stringOfCars1.

Print stringOfCars2.

Attachment:- lab4.2_0.rar

Reference no: EM131149005

Questions Cloud

Prepare a defense for mr steele : Identify all major steps you will take to prepare a defense for Mr. Steele , including investigation of evidence and witnesses, should Mr. Steele be arrested for a felony such as armed robbery
Which offer should you accept and why : Bank A offers to pay you lump sum of $20,000 after 5 years, if you deposit $9,500 with them today. Bank B, on the other hand, says that they will pay you a lump sum of $22,000 after 5 years, if you deposit $10,700 with them today. Which offer should ..
Analyse the current state of the field of strategic hrm : Analyse the current state of the field of strategic HRM in the context of a global environment.- describe where you would like your scenario organisation to be in the future.
Perceive as violations of their codes of use-expectations : How do social media audiences respond to what they perceive as violations of their codes of use, expectations, or even policies? What problems can that cause an organization?
Create a default string of cars in main : Build the default constructor first. Create a default string of cars in main. Build an output function, similar to the old one, but dereferrencing the pointers.
Classification of crimes as misdemeanors or felonies : For this discussion, begin by researching your state statute regarding the classification of crimes as misdemeanors or felonies. In your main post, include the following: Summarize the delineating factors that differentiate felonies from misdemeano..
What other data collection methods could have been utilized : Do you think that there are any specific criteria that the choice of shopping mall should satisfy? Was the choice of a shopping mall appropriate? Support your answer with thorough explanations. What may have been considered as alternative locatio..
How much money will joanna’s dad have to deposit into the ac : Currently the average college tuition cost is $15,000 and is expected to increase by 4%. Joanna just turned 5, and is expected to start college when she turns 18. How much money will Joanna’s dad have to deposit into the account?
Explain how these affect public opinion : Discuss how your personal ethics are likely to affect your job behavior. In addition, define moral principles, and explain how these affect public opinion, which often set standards for behavior.

Reviews

Write a Review

Programming Languages Questions & Answers

  Display total amount owed in fixed-point notation

Enter your C++ instructions into a source file named Introductory11.cpp. Also enter appropriate comments and any additional instructions required by the compiler. Display the total amount owed in fixed-point notation with two decimal places.

  Write a regular expression for the given problem

You need to prepare a regular expression of the problem - The set of strings over alphabet {a, b, c} containing at least one a and at least one b.

  Create a console application called mythread

Create a console application called myThread. Within the application, create a thread called Updater. This thread will be used to update a running sum of values entered by the user.

  Using datagrid in an application

Update the field named fldQuestionDescription.

  Program by bernstein-s conditions to get maximum parallelism

Restructure program by using Bernstein's conditions in order to get maximum parallelism between processes. Specify which of the three conditions is not satisfied.

  Draw the class diagram for your modi ed system

Explain what is wrong. Modify the system so that it conforms to the pattern. Explain each of the changes you made to the system. Draw the class diagram for your modi ed system.

  Write complete main method using good programming practices

Write a complete main method using good programming practices when you rewrite the while loop as a do/while loop.

  Explain proper use of html with all of the elements

The proper use of HTML, with all of the elements, codes, tags, headers, forms, Metadata, and categories will assure that you will structure your site to be accessible to the users of your site, your target audience

  Develop unix shell script to input number of hours

Develop a Unix shell script to input number of hours worked and pay rate and calculate the total pay, then the social security amount (assume 5%), then the net pay.

  Program to report page number and offset of virtual address

Your program will report page number and offset of given virtual address with specified page size. Page sizes should be specified as power of 2 in the range 1024-16384 (inclusive).

  Values of variables of logical section of the pseudocode

What are the values of variables a, b, and c after each line of code of each logical section of the pseudocode. Show why the logical section is running.

  Computes the wind-chill factor for a given temperature

The user should be able to enter a temperature and wind speed via text boxes, then click a button to view the corresponding wind-chill factor in a separate text box. As part of your page, you should define a function named Wi ndChi 11 that accepts..

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd