C++ Assignment Questions And Answers

CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 1 of 23
Department of Computer Science and Information Technology
La Trobe University
CSE1CPP 2019
Semester 2
Assignment Part B
Delays caused by computer downtime cannot be accepted as a valid reason for a late submission. Students must plan their work to allow for both scheduled and unscheduled downtime. There are no days late or extensions on this assignment as execution test marking will begin in your normal lab (Week 11)
This is an individual Assignment. You are not permitted to work as a Pair Programming partnership or any other group when writing this assignment.
Copying, Plagiarism: Plagiarism is the submission of somebody else’s work in a manner that gives the impression that the work is your own. The Department of Computer Science and Information Technology treats academic misconduct seriously. When it is detected, penalties are strictly imposed. Refer to the subject guide for further information and strategies you can use to avoid a charge of academic misconduct.
Assessment Objectives:
• to gain practice using vectors
• to practice friend functions
• to practice using function objects (or lambda’s if you want)
• to practice reading and writing to text files
Submission Details: Full instructions on how to submit electronic copies of your source code files from your latcs8 account are given at the end. If you have not been able to complete a program that compiles and executes containing all functionality, then you should submit a program that compiles and executes with as much functionality as you have completed. (You may comment out code that does not compile.)
Note you must submit electronic copies of your source code files using the submit command on latcs8. Ensure you submit all required files, one file at a time. For example, the file building.cpp would be submitted with the command:
> submit CPP building.cpp
PLEASE NOTE: While you are free to develop the code for this progress check on any operating system, your solution must run on the latcs8 system.
Marking Scheme: This assignment (all parts together) is worth 15% of your final mark in this subject.
Implementation (Execution of code) 100%
You may be required to attend a viva voce (verbal) assessment
(to be used as a weighting factor on the final mark). Do NOT use the LMS to submit your files, use latcs8 only
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 2 of 23
Return of Mark sheets:
The face to face execution test marking in the lab (Week 11) constitutes a return of the assignment mark, subject to the conditions on pages 3, and 22
Please note carefully:
The submit server will close on Tues Oct 15th at 10:00 am
After the submit server has closed, NO assignments can be accepted.
Please make sure that you have submitted all your assignment files before the submit server closes. (see page 22)
There can be NO extensions or exceptions.
Your assignment will be marked in your normal lab, starting Tues Oct 15th
You should attend your normal lab to have your assignment marked.
If you are unable to attend your normal lab for marking, you can attend any lab. Assignments must be marked be the end of the
semester.
Marking scheme:
100% for the code executing correctly
You may be asked to explain your code, potentially, the whole
assignment. Your explanations, or lack of explanation, may be used as a weighting factor to reduce your initial mark. This may result in the final mark of this assignment being reduced to 0.
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 3 of 23
Please also note carefully that whilst we encourage innovation and exploring C++ beyond what has been presented in the subject to date, above all, we encourage understanding.
All of the assignment that follows can be solved using techniques that have been presented in lectures, lecture / workshops and labs so far.
These are the techniques and knowledge that we will later be examining in the exam (70 marks).
Code and techniques that are outside the material presented will not be examined, of course.
You are free to solve the problem below in any way, provided that you can fully explain any code that is outside what has been taught.
All assignments in CPP are marked, face to face, in the lab, in an execution test. This means that we mark running code. Your code must compile and display a result to the screen. Regrettably, we don’t have the time or resources to look at code. The smallest amount of code that produces and displays a correct result will gain more marks than lots of code that doesn’t compile, run or display something to the screen.
Code that you are expected to use – READ THIS
How this assignment is marked
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 4 of 23
Problem Description
City Planning Partners (here after referred to as CPP) are a company that plans and maintains cities. A city consists of buildings. There are a number of different types of buildings.
Amongst these types are Warehouse, Residential and Commercial buildings.
CPP makes sure that new Buildings are always constructed on vacant land, that is, at new addresses. CPP also sets the rates that building’s pay, displays the details of the whole city and changes what is in the building.
At times, the details of the city need to be saved to a text file. Finally, the contents of this text file needs to be read back into the operational structure.
CPP would like you to write a C++ program to manage the operations of the City.
Problem Implementation
As CPP would like the program as soon as possible, some files have been provided for you.
These are address.h, address.cpp, building.h, building.cpp, city.h, city.cpp, commercial.h, commercial.cpp, product1.h, product1.cpp, product2.h, product2.cpp, product3.h, product3.cpp, residential.h, residential.cpp, warehouse.h, warehouse.cpp, and makefile.
There is also a text file that contains some sample data so that you can test your program. This is in the file a.dat
So long as you use the files provided, then using the makefile will compile all of the files into a C++ program.
To run the program, after it successfully compiles, just type city
These files can be copied by typing this command on latcs8
cp ~csilib/cse1cpp/assignB/* .
(don’t forget the . (dot) for your current directory)
READ THE WHOLE DOCUMENT FIRST.
class Address
The Address class is a stand-alone class. Every Building has an Address object as an
attribute.
number this is an int and is the street number part of the address
street this is text and is the street name part of the address, can be more
than one word
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 5 of 23
postCode this is an int and is the post code part of the address
The Address class requires the following functionality:
➢ A default constructor.
➢ A constructor that takes all three arguments as parameters
➢ overloaded friend functions for ostream, istream, ifstream and ofstream.
Just implement all 4 overloaded friend functions. You will need the istream
overloaded function as there is nothing unique in this class. Reading input
from the keyboard is allowable and will be required. The other three overloaded
friend functions will also be required
➢ An overloaded friend operator == that returns true or false if one Address object is exactly the same as another Address object. To be exactly the same, all corresponding attributes must be the same. That is, the street numbers are the same, the streets are the same and the post codes are the same in both objects.
class Building
The Building class is the abstract base class for all Buildings in the city.
The Building class has the following attributes:
rate this is a double and is the amount that the Building pays in rates
to CPP. This can only be determined when we know the actual,
concrete sub class.
type this is text and is the type of the Building. Currently this is one
of Commercial, Residential or Warehouse.
This can be more than one word. The type can only be set when we
know the actual, concrete sub class.
address this is an Address object, every Building has an address.
The address of every Building must be unique. This is why you wrote
the overloaded friend operator == function in the Address class.
The Building class requires the following functionality:
➢ A default constructor
➢ An overloaded constructor that takes values for both attributes, type and address, as parameters.
➢ A virtual destructor that indicates that it expects not to throw any exceptions. This is absolutely necessary as this class is the base class for all Building sub classes.
➢ overloaded friend functions for ostream, ofstream, istream and ifstream
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 6 of 23
➢ This class requires an accessor function that returns a const reference to the Address object that is a private attribute of this class. This has been written for you, since it is just one line.
➢ The overloaded friend ostream function, and the other overloaded friend functions (ofstream, istream, and ifstream) display information about the Building object to the screen, write the information about the Building class to a text file, read in the information from a text file to populate a Building object and to obtain information from the keyboard to populate a Building.
These overloaded friend operators are similar (not the same) to the ones that we used in assignment A and they have the same purpose.
There are a number of “wrapper” member functions in the Building class. For example, display( ) and saveToFile( ) amongst other “wrapper” functions.
These are member functions. Member functions can be made virtual and
can be overridden in the sub classes, polymorphism.
friend functions are not members, so they can never be virtual and so can never be
overridden. We use the “wrapper” member functions so that c++ can identify the
actual sub class. Once c++ goes to that sub class, then the member function can
call its friend function to do the output or input.
There is an abstract add( ) function. The purpose of this function is to change one (or more) of the attributes, once we know the actual sub class. We identity the actual element of the vector using the address of the Building. Each actual sub class has different things that can be changed. This is explained in the actual sub classes.
There is also an abstract setRate( ) function. The purpose of this function is calculate the rates due for a Building. As the actual concrete sub classes have different rules for calculating this, once again we must override this function in the
actual sub classes.
The rates result however, is stored in the Building base class, as every Building must pay rates. This is the same idea as calculating the age of a Tree back in lab 5.
Every Tree has an age, but we don’t know how to calculate that age until we are
in the actual concrete sub class as each sub class has different growing rates.
The member functions could do the task directly, this is just a good chance to see the difference between member functions and friend functions.
This was covered in lab 07 of week 8
Be careful when writing any and all classes that need to be written to text files (saved) that you follow, exactly, the format of the input file, see pages 12 and 13
We have to be able to use your output files as input files the next time the program is run.
This will be a little bit more complex than assignment A. As in lab 7, we are
using text files to simulate binary files. Hence the input files have various
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 7 of 23
text identifiers that are not part of any object, they just tell us for which class
the following record refers to. These text identifiers must be written back to
the output text file, in the appropriate places.
Many of the functions in the Building class are abstract, they must be
overridden (implemented) in the sub classes or else the sub classes will
be abstract classes themselves
There is a member function operator that returns if one Building is the same as
another Building. What makes one Building the same as another Building is
detailed above, the Buildings have the same address.
class Commercial
This class is about the concrete sub class Commercial.
The Commercial class has the following attributes:
type this is a string and is the actual type of the sub class. This needs to be
passed to the base class. This is the same idea as Lab 5, the Trees.
openHour this is an int and is the opening hour of the Commercial building
in 24-hour time
closeHour this is an int and is the closing hour of the Commercial building
in 24-hour time.
You can assume that the opening hour is always less than the
closing hour and that no Commercial buildings are open for
24 hours a day
The Commercial class requires the following functionality:
➢ A default constructor.
➢ An overloaded constructor that takes values for all three attributes, address, openingHour and closingHour as parameters.
➢ overloaded friend functions for ostream, istream, ifstream and ofstream.
➢ To use the overloaded friend functions, from the main driver program, city.cpp,
we will first call the appropriate member “wrapper” function. This was explained
above. Once C++ is in the correct concrete sub class, then the “wrapper” function
can call the friend function in that class to do the actual task.
Once again, refer back to Flying and Dragon in lab 7
This applies to all 4 overloaded friend functions, the 2 input and the 2 output functions.
As in Assignment A, you probably will not use the istream overloaded friend function
as the program must ensure that the user entered address is indeed unique.
The other 3 friend functions will be used and so you need to have the “wrapper”
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 8 of 23
functions implemented.
As mentioned above, you might want to have these overridden member functions
do the job that the friend functions will do. That is an acceptable alternative.
In all the sub class functions, always consider the base class Building. What do we
need to display from the Building class or write into the Building class?
Just because the Building class is abstract does NOT mean that it cannot have
concrete functions that need to be implemented and can be called.
➢ The ofstream and ifstream operators do the same function, just writing out to a text file or reading in from a text file. This is after either writing out text identifiers
or reading in those text identifiers as applicable and dealing with the base class
Building
The setRate( ) function and the add( ) function need to be implemented
For the rates, they are calculated as 4.50 * ( closingHour – openingHour)
The add( ) function allows the user to edit the opening and closing hour of
the Commercial building
class Residential
This class is about the concrete sub class Residential.
The Residential class has the following attributes:
type this is a string and is the actual type of the sub class. This needs to be
passed to the base class. This is the same idea as Lab 5, the Trees.
numberOfPeople this is an int and is the actual number of residents in this
particular Residential object.
The Residential class requires the following functionality:
➢ A default constructor.
➢ An overloaded constructor that takes values for both attributes, address and number of people as parameters.
➢ overloaded friend functions for ostream, istream, ifstream and ofstream.
➢ To use the overloaded friend functions, from the main driver program, city.cpp,
we will first call the appropriate member “wrapper” function. This was explained
above. Once C++ is in the correct concrete sub class, then the “wrapper” function
can call the friend function in that class to do the actual task.
Once again, refer back to Flying and Dragon in lab 7
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 9 of 23
This applies to all 4 overloaded friend functions, the 2 input and the 2 output functions.
As in Assignment A, you probably will not use the istream overloaded friend function
as the program must ensure that the user entered address is indeed unique.
The other 3 friend functions will be used and so you need to have the “wrapper”
functions implemented.
As mentioned above, you might want to have these overridden member functions
do the job that the friend functions will do. That is an acceptable alternative.
In all the sub class functions, always consider the base class Building. What do we
need to display from the Building class or write into the Building class?
➢ The ofstream and ifstream operators do the same function, just writing out to a text file or reading in from a text file. This is after either writing out text identifiers
or reading in those text identifiers as applicable and dealing with the base class
Building
The setRate( ) function and the add( ) function need to be implemented
For the rates, they are calculated as 4.78 * numberOfPeople
The add( ) function allows the user to edit the number of residents in that
Residential building (object)
class Warehouse
This class is about the concrete sub class Warehouse.
Warehouse is a templated sub class, more on this below.
The Warehouse class has the following attributes:
type this is a string and is the actual type of the sub class. This needs to be
passed to the base class. This is the same idea as Lab 5, the Trees.
product this is the templated data member, product can be anything for which
we have written a class. This attribute is always a stand alone
class, there is no inheritance involved.
quantity this is an int and is the quantity of the product that is stored in the
Warehouse.
The Warehouse class requires the following functionality:
➢ A default constructor.
➢ An overloaded constructor that takes values for attributes, address, product and quantity as parameters.
➢ overloaded friend functions for ostream, istream, ifstream and ofstream.
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 10 of 23
➢ To use the overloaded friend functions, from the main driver program, city.cpp,
we will first call the appropriate member “wrapper” function. This was explained
above. Once C++ is in the correct concrete sub class, then the “wrapper” function
can call the friend function in that class to do the actual task.
Once again, refer back to Flying and Dragon in lab 7
This applies to all 4 overloaded friend functions, the 2 input and the 2 output functions.
As in Assignment A, you probably will not use the istream overloaded friend function
as the program must ensure that the user entered address is indeed unique.
The other 3 friend functions will be used and so you need to have the “wrapper”
functions implemented.
As mentioned above, you might want to have these overridden member functions
do the job that the friend functions will do. That is an acceptable alternative.
In all the sub class functions, always consider the base class Building. What do we
need to display from the Building class or write into the Building class?
➢ The ofstream and ifstream operators do the same function, just writing out to a text file or reading in from a text file. This is after either writing out text identifiers
or reading in those text identifiers as applicable and dealing with the base class
Building
The setRate( ) function and the add( ) function need to be implemented
For the rates, they are calculated as 79.22 * quantity
The add( ) function allows the user to edit the quantity of the Warehouse object
class Product_1
The Product_1 class has the following attributes:
type this is a string and is the product type for this class, this is fixed.
screenSize this is an int and is the screen size of this product in centimetres
price this is a double and is the price of this product
The Product_1 class requires the following functionality:
➢ A default constructor.
➢ A constructor that takes both arguments, screenSize and price as parameters
➢ overloaded friend functions for ostream, istream, ifstream and ofstream.
Unlike the sub classes, we don’t need “wrapper” functions for this class, there is
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 11 of 23
no inheritance, so there are no virtual functions.
Just implement all 4 overloaded friend functions. You will need the istream
overloaded function as there is nothing unique in this class. Reading input
from the keyboard is allowable and will be required. The other three overloaded
friend functions will also be required
class Product_2
The Product_2 class has the following attributes:
type this is a string and is the product type for this class, this is fixed.
racks this is an int and is the number of toaster racks for this product
colour this is text. may be more than one word and is the colour of the
product
The Product_2 class requires the following functionality:
➢ A default constructor.
➢ A constructor that takes both arguments, colour and racks, as parameters
➢ overloaded friend functions for ostream, istream, ifstream and ofstream.
Unlike the sub classes, we don’t need “wrapper” functions for this class, there is
no inheritance, so there are no virtual functions.
Just implement all 4 overloaded friend functions. You will need the istream
overloaded function as there is nothing unique in this class. Reading input
from the keyboard is allowable and will be required. The other three overloaded
friend functions will also be required
class Product_3
type this is a string and is the product type for this class, this is fixed.
range this is a double and is the range of the product
model this is text, may be more than one word and is the model of the
product
numberOfPassengers
this is an int and is the number of passengers in the product.
product
The Product_3 class requires the following functionality:
➢ A default constructor.
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 12 of 23
➢ A constructor that takes all three arguments, range, model and number of passengers, as parameters
➢ overloaded friend functions for ostream, istream, ifstream and ofstream.
Unlike the sub classes, we don’t need “wrapper” functions for this class, there is
no inheritance, so there are no virtual functions.
Just implement all 4 overloaded friend functions. You will need the istream
overloaded function as there is nothing unique in this class. Reading input
from the keyboard is allowable and will be required. The other three overloaded
friend functions will also be required
Program functionality
The program starts by prompting (asking) the user for the name of input text file.
The format of this file is shown below
File line Meaning
W
This is the identifier to indicate that what follows is for a Warehouse object.
P3
This is the identifier to indicate what follows is for a Product_3 object. Recall that the Warehouse is a templated class. We need to know the actual type of the Product so that we instantiate a Warehouse object.
14000
This is the range of a Product_3 object
Boeing 787EER
This is the model of Product_3
450
This is the number of passengers in Product_3
Warehouse
This is the actual type of the Building sub class
56
This is the street number of the Building (part of the Address object that is an attribute of the Building class)
Happy Way
This is the street name of the Building (part of the Address object that is an attribute of the Building class)
3086
This is the post code of the Building (part of the Address object that is an attribute of the Building class)
396.1
This is the rates for this Building
5
This is the quantity of the product in this Warehouse
R
This is the identifier to indicate that what follows is for a Residential object.
Residential
This is the actual type of the Building sub class
55
This is the street number of the Building (part of the Address object that is an attribute of the Building class)
Happy Way
This is the street name of the Building (part of the Address object that is an attribute of the Building class)
3086
This is the post code of the Building (part of the Address object that is an attribute of the Building class)
23.9
This is the rates for this Building
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 13 of 23
5
This is the number of residents in this object
W
This is the identifier to indicate that what follows is for a Warehouse object.
P2
This is the identifier to indicate what follows is for a Product_2 object. Recall that the Warehouse is a templated class. We need to know the actual type of the Product so that we instantiate a Warehouse object.
red and green
This is the colour the Product, can be more than one word
4
This is the number of racks of the Product
Warehouse
This is the actual type of the Building sub class
54
This is the street number of the Building (part of the Address object that is an attribute of the Building class)
Happy Way
This is the street name of the Building (part of the Address object that is an attribute of the Building class)
3086
This is the post code of the Building (part of the Address object that is an attribute of the Building class)
3961
This is the rates for this Building
50
This is the number of this Product stored in the Warehouse
C
This is the identifier to indicate that what follows is for a Commercial object.
Commercial
This is the actual type of the Building sub class
53
This is the street number of the Building (part of the Address object that is an attribute of the Building class)
Happy Way
This is the street name of the Building (part of the Address object that is an attribute of the Building class)
3086
This is the post code of the Building (part of the Address object that is an attribute of the Building class)
49.5
This is the rates for this Building
7
This is an int and is the opening hour of this Building, in 24-hour time
18
This is an int and is the closing hour of this Building, in 24-hour time (will always be bigger than the opening hour)
W
This is the identifier to indicate that what follows is for a Warehouse object.
P1
This is the identifier to indicate what follows is for a Product_1 object. Recall that the Warehouse is a templated class. We need to know the actual type of the Product so that we instantiate a Warehouse object.
195
This is the an int and is the screen size of the Product, in centimetres
3981.55
This is a double and is the price of the Product
Warehouse
This is the actual type of the Building sub class
52
This is the street number of the Building (part of the Address object that is an attribute of the Building class)
Happy Way
This is the street name of the Building (part of the Address object that is an attribute of the Building class)
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 14 of 23
3086
This is the post code of the Building (part of the Address object that is an attribute of the Building class)
1980.5
This is the rates for this Building
25
This is the number of this Product stored in the Warehouse
If the file can be opened, then the format of the file is guaranteed to be correct.
There can be any number of records in the file. The records may be in any order.
What is shown above is just an example, not the hard-coded contents of the file.
The format of each record is guaranteed to be correct. What is a record?
It is all the information required to instantiate one of the concrete sub classes of the
Building class.
You must not hard code the input (or output) file name, the user must be able to enter any file name.
Once the input file has been read into the data structure, then the file is closed and
all interactions with the program are with the map or whichever data structure you
have used.
After the input file has been read into the data structure that you are using, then
the user is presented with a menu, as shown:
City Menu
1. Add Building
2. Display
3. Add to an existing Building
4. Save the city to a file
5. Close the program
Enter choice >>
Explanation of the menu choices:
1. Add Building
Selecting this main menu choice presents the user with a sub menu:
Building type add menu
1. Add Warehouse
2. Add Commercial
3. Add Residential
4. Return to main menu
Enter choice >>
1. Add Warehouse
Selecting this sub menu choice prompts the user to enter an address
This is the same for all Building objects. If the address is not unique, then
there is no point in asking for any further information as we are not going to
instantiate that object.
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 15 of 23
Provided that the address is not in use, then the user is presented with another
sub menu, this time to select the actual Product that will be stored in the
Warehouse. (Recall that the Warehouse is templated, so we need the actual
Product type in order to instantiate the Warehouse object)
Product Menu
1. Product 1
2. Product 2
3. Product 3
Enter Product choice >>
Then the user is prompted (asked) for the quantity of the product.
Depending on the user choice at this point, the corresponding Product class
keyboard overloaded operator will be called and the Product object
instantiated. (Whether it is actually Product_1, Product_2 or Product_3)
This will vary depending on the Product.
The Warehouse object, with its Product, will be added to the vector.
The user is given the option of staying in this sub menu to add another
Building. The user MUST be prompted (asked) to enter the address first,
with the check on uniqueness. If this is not done, then it would be possible to
construct more than one Building at the same address.
The alternative is to return to the main menu as soon as the user has added
a Building. That way, the user must enter an address at the top of the sub menu
every time they go into this sub menu.
Which one you choose is up to you.
Your program execution will test trying to add a Building at an existing address.
Equally, if the address is not unique, the user should be informed, and they should not be asked to enter any more information. If we already know that
we are not going to be able to add that Building, don’t waste the user’s time asking what are now, irrelevant questions, then inform them that the address is not unique.
2. Add Commercial
Selecting this sub menu choice prompts the user to enter an address
This is the same for all Building objects. If the address is not unique, then
there is no point in asking for any further information as we are not going to
instantiate that object.
Provided that the address is not in use, then the user is prompted (asked) to enter the remaining information for a Commercial object, that is, the open and closing hours as int. Recall that you may assume that the closing hour is always greater than the opening hour and that no Commercial object is open 24 hours a day.
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 16 of 23
3. Add Residential
Selecting this sub menu choice prompts the user to enter an address
This is the same for all Building objects. If the address is not unique, then
there is no point in asking for any further information as we are not going to
instantiate that object.
Provided that the address is not in use, then the user is prompted (asked) to enter the remaining details for a Residential object, that is, the number of residents in this actual object
4. Return to main menu
Selecting this sub menu choice returns to the main menu
In all the user entries, the user is NEVER asked to enter the rate, that is calculated as
described above NOR is the user ever prompted (asked) to enter the type, even for Products.
Assignments that ask the user to enter either of these 2 will have marked deducted.
Why? Because if the user can enter any type, then what is supposed to be a Residential object could in fact have a type of “purple balloon”. Equally, a Product that is supposed to
be a TV could have a type of “cuddly bear”.
Knowing the actual type of class, we are in, and that is what the program does, the class has its type hard coded into the class. Just like Evergreen and Deciduous in lab 5.
2. Display
Choosing this main menu option displays the contents of the vector (or whatever data
structure you are using) to the screen. See the sample runs later in this document
for an idea on the format. Note, you do not have to follow this format. The
output has to be easily readable though.
3. Add to an existing Building
Choosing this main menu option, the user is prompted (asked) for an address.
Provided that the user address can be found, then the user is prompted (asked) to edit
one or more of the attributes of the object. Which objects and which attributes in
those classes are described above.
How does C++ choose the correct actual sub class? This is where the overridden
virtual functions come into play. The sub class had to implement the add( ) function
in its class, otherwise the sub class would be abstract too. So, all we need to do is
call the add( ) function and polymorphism will do the rest, taking us to the actual
concrete sub class.
Remember to recalculate the rate as one (or more) of the factors that determine
the rate has changed.
4. Save to a text file
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 17 of 23
Choosing this menu option, the user is prompted (asked) for the name of an output
text file. This can be the same file as the input file, or it can be a different file. Provided
that the file can be opened, then the contents of the vector is written to the text file.
Be sure to use the same format as the input file, see pages 12 and 13.
Recall that we must be able to use this output file as an input file the next time
that the program is run.
Remember to write the identifiers for the various actual Product and Building
concrete sub classes to the output file in the correct format.
Choosing this option does NOT close the program. This option can be chosen
at any time while the program is running.
5. Close the program
This menu option closes the program. It does not ask the user if they want to
save the changes to the vector (or whatever data structure you have chosen).
Sample run of the program
Note that not every condition is shown in the sample below, because a condition is not shown does NOT mean that it must not be implemented, or that it will not be tested and marked.
User input is in bold
> city
Please enter input file name >> a.dat
City Menu
1. Add Building
2. Display
3. Add to an existing Building
4. Save the city to a file
5. Close the program
Enter choice >> 2
Building
Type: Warehouse
Address: Number 56 Happy Way post code 3086
Rates: $396.1
Product_3 is an Aircraft
“Range” : 14000
“Model” : Boeing 787EER
“Number of passengers” : 450
Quantity currently in store 5
Building
Type: Residential
Address: Number 55 Happy Way post code 3086
Rates: $23.9
Number of people in this building: 5
Building
Type: Warehouse
Address: Number 54 Happy Way post code 3086
Rates: $3961
Product_2 is a Toaster
“Colour” : red and green
“Number of racks” : 4
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 18 of 23
Quantity currently in store 50
Building
Type: Commercial
Address: Number 53 Happy Way post code 3086
Rates: $49.5
Open from 7 to 18
Building
Type: Warehouse
Address: Number 52 Happy Way post code 3086
Rates: $1980.5
Product_1 is a TV
“screen size:” 195 “price” $3981.55
Quantity currently in store 14
City Menu
1. Add Building
2. Display
3. Add to an existing Building
4. Save the city to a file
5. Close the program
Enter choice >> 1
Building type add menu
1. Add Warehouse
2. Add Commercial
3. Add Residential
4. Return to main menu
Enter choice >> 2
Add new address
Enter street number >> 52
Enter street name >> Happy Way
Enter post code >> 3086
There is already a building at that address
Building type add menu
1. Add Warehouse
2. Add Commercial
3. Add Residential
4. Return to main menu
Enter choice >> 3
Add new address
Enter street number >> 56
Enter street name >> Happy Way
Enter post code >> 3099
Enter number of people in this building >> 4
Building type add menu
1. Add Warehouse
2. Add Commercial
3. Add Residential
4. Return to main menu
Enter choice >> 1
Add new address
Enter street number >> 56
Enter street name >> CPP Highway
Enter post code >> 3086
Product Menu
1. Product 1
2. Product 2
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 19 of 23
3. Product 3
Enter Product choice >> 3
Enter quantity >> 3
Enter range >> 12548
Enter model >> Lockheed SR71
Enter number of passengers >> 2
Building type add menu
1. Add Warehouse
2. Add Commercial
3. Add Residential
4. Return to main menu
Enter choice >> 2
Add new address
Enter street number >> 12
Enter street name >> CPP Highway
Enter post code >> 3099
Enter opening hour >> 6
Enter closing hour >> 15
Building type add menu
1. Add Warehouse
2. Add Commercial
3. Add Residential
4. Return to main menu
Enter choice >> 4
City Menu
1. Add Building
2. Display
3. Add to an existing Building
4. Save the city to a file
5. Close the program
Enter choice >> 2
Building
Type: Warehouse
Address: Number 56 Happy Way post code 3086
Rates: $396.1
Product_3 is an Aircraft
“Range” : 14000
“Model” : Boeing 787EER
“Number of passengers” : 450
Quantity currently in store 5
Building
Type: Residential
Address: Number 55 Happy Way post code 3086
Rates: $23.9
Number of people in this building: 5
Building
Type: Warehouse
Address: Number 54 Happy Way post code 3086
Rates: $3961
Product_2 is a Toaster
“Colour” : red and green
“Number of racks” : 4
Quantity currently in store 50
Building
Type: Commercial
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 20 of 23
Address: Number 53 Happy Way post code 3086
Rates: $49.5
Open from 7 to 18
Building
Type: Warehouse
Address: Number 52 Happy Way post code 3086
Rates: $1980.5
Product_1 is a TV
“screen size:” 195 “price” $3981.55
Quantity currently in store 14
Building
Type: Residential
Address: Number 56 Happy Way post code 3099
Rates: $19.12
Number of people in this building: 4
Building
Type: Warehouse
Address: Number 56 CPP Highway post code 3086
Rates: $237.66
Product_3 is an Aircraft
“Range” : 12548
“Model” : Lockheed SR71
“Number of passengers” : 2
Quantity currently in store 3
Building
Type: Commercial
Address: Number 12 CPP Highway post code 3099
Rates: $40.5
Open from 6 to 15
City Menu
1. Add Building
2. Display
3. Add to an existing Building
4. Save the city to a file
5. Close the program
Enter choice >> 3
Enter an address
Enter street number >> 12
Enter street name >> CPP Highway
Enter post code >> 3099
Enter new opening hour >> 6
Enter new closing hour >> 20
City Menu
1. Add Building
2. Display
3. Add to an existing Building
4. Save the city to a file
5. Close the program
Enter choice >> 2
Building
Type: Warehouse
Address: Number 56 Happy Way post code 3086
Rates: $396.1
Product_3 is an Aircraft
“Range” : 14000
“Model” : Boeing 787EER
“Number of passengers” : 450
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 21 of 23
Quantity currently in store 5
Building
Type: Residential
Address: Number 55 Happy Way post code 3086
Rates: $23.9
Number of people in this building: 5
Building
Type: Warehouse
Address: Number 54 Happy Way post code 3086
Rates: $3961
Product_2 is a Toaster
“Colour” : red and green
“Number of racks” : 4
Quantity currently in store 50
Building
Type: Commercial
Address: Number 53 Happy Way post code 3086
Rates: $49.5
Open from 7 to 18
Building
Type: Warehouse
Address: Number 52 Happy Way post code 3086
Rates: $1980.5
Product_1 is a TV
“screen size:” 195 “price” $3981.55
Quantity currently in store 14
Building
Type: Residential
Address: Number 56 Happy Way post code 3099
Rates: $19.12
Number of people in this building: 4
Building
Type: Warehouse
Address: Number 56 CPP Highway post code 3086
Rates: $237.66
Product_3 is an Aircraft
“Range” : 12548
“Model” : Lockheed SR71
“Number of passengers” : 2
Quantity currently in store 3
Building
Type: Commercial
Address: Number 12 CPP Highway post code 3099
Rates: $63
Open from 6 to 20
City Menu
1. Add Building
2. Display
3. Add to an existing Building
4. Save the city to a file
5. Close the program
Enter choice >> 4
Please enter output file name >> d.dat
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 22 of 23
City Menu
1. Add Building
2. Display
3. Add to an existing Building
4. Save the city to a file
5. Close the program
Enter choice >> 5
Electronic Submission of the Source Code
• Submit all the C++ files that you have developed in the tasks above.
• The code has to run under Unix on the latcs8 machine.
• You submit your files from your latcs8 account. Make sure you are in the same directory as the files you are submitting. Submit each file separately using the submit command.
submit CPP address.h
submit CPP address.cpp
submit CPP building.h
submit CPP building.cpp
submit CPP city.h
submit CPP city.cpp
submit CPP commercial.h
submit CPP commercial.cpp
submit CPP product1.h
submit CPP product1.cpp
submit CPP product2.h
submit CPP product2.cpp
submit CPP product3.h
submit CPP product3.cpp
submit CPP residential.h
submit CPP residential.cpp
submit CPP warehouse.h
submit CPP warehouse.cpp
submit CPP makefile
After submitting the files, you can run the following command that lists the files submitted from your account:
verify
You can submit the same filename as many times as you like before the assignment deadline; the previously submitted copy will be replaced by the latest one.
Please make sure that you have read page 2 about the submission close off date and time and the compulsory requirement to attend an execution test, preferably in Week 11, but may be at another time by arrangement.
Failure to do both of these things will result in your assignment be awarded a mark of 0, regardless of the correctness of the program.
Execution test marks are provisional and subject to final plagiarism checks and checks on the compliance of your code to this assignment document.
As such, final assignment marks may be lower or withdrawn completely.
CPP Assignment Part B Semester 2 – due: 10:00 am Tues Oct 15th this is the first and final hand in date p. 23 of 23
Final Notes: – transferring files between Windows and Unix
Be very careful transferring files from Windows to Unix. If you do transfer a file from Windows to Unix open the file, in Unix, using vi.
For example, if you transferred a file named b.txt from Windows to Unix
open the file in Unix with the command
vi –b b.txt
you will (might) see a lot of ^M’s at the end of each line.
These MUST be removed using the command shown below or else your input file will have too many newline characters and will not translate properly. That is, your code will not correctly read the input file.
Your code will work on Windows but NOT on Unix.
Still in vi, in command mode (press the Esc key first) do the following
:%s/ctrl-v ctrl-m//g
ctrl-v ctrl-m means hold down the control key and with the control key down press v then press m.
Final, final notes
Don’t let anyone look at your code and definitely don’t give anyone a copy of your code. The plagiarism checker will pick up that the assignments are the same and you will both get 0 (it doesn’t matter if you can explain all your code).
There will be consultation sessions for the assignment, the times will be posted on LMS, if you have problems come to consultation.
And a final, final, final note, “eat the dragon in little bits”. Do a little bit every night; before you know it you will be finished. The assignment is marked with running code, so you are better to have 1 or 2 parts completed that actually compile and run, rather than a whole lot of code that doesn’t compile.
The execution test is done on latcs8 so please make sure that your code runs on latcs8 before you submit.