LINKÖPING UNIVERSITY Department of Computer and Information Science Software and Systems Eric Elfving

advertisement
LINKÖPING UNIVERSITY
Department of Computer and Information Science
Software and Systems
Eric Elfving
2016-01-14
Computer examination in
TDDD38 Advanced Programming in C++
Date 2016-01-14
Administrator
Time 08-13
Anna Grabska Eklund, 28 2362
Department IDA
Course code TDDD38
Teacher on call
Exam code DAT1
Eric Elfving (eric.elfving@liu.se, 013-28 2419)
Will primarialy answer exam questions using
the student client.
Will only visit the exam rooms for systemrelated problems.
Examiner
Klas Arvidsson (klas.arvidsson@liu.se)
Allowed Aids (tillåtna hjälpmedel)
An English-* dictionary may be brought to the exam.
No other printed or electronic material are allowed.
The cplusplus.com reference is available in the exam system.
Grading
The exam has a total of 25 points.
0-10 for grade U/FX
11-14 for grade 3/C
15-18 for grade 4/B
19-25 for grade 5/A.
Special instructions
• Don’t log out at any time during the exam, only when you have finished.
• Given files are found in subdirectory given_files (write protected).
The exam will be available as a pdf in this directory at the start of the exam.
• Files for examination must be submitted via the Student Client, see separate instructions
(given_files/student_client.pdf)!
• When using standard library components, such as algorithms and containers, try to chose
“best fit” regarding the problem to solve. Avoid unrelated/unnecessary computations and
unnecessary data structures.
• C style coding may cause point reduction where C++ alternatives are available.
TDDD38
Page 2 of 5
2016-01-14
Theory questions
Answers may be given in both Swedish and English. Submit your answers to all questions in
one text file as Assignment #1 or one file for each question.
1. Give the three cases where member initialization in a member initializer list is required.
[1p]
2. What is meant by the mutable keyword for class data members?
[1p]
3. What is meant by an inline namespace?
[1p]
4. Give the three steps of overload resolution performed before overload resolution failure.
[1p]
5. The following declarations will give vectors with two different sets of values. Which values
will they have and why does the content differ?
[1p]
v e c t o r <i n t > v1 { 2 , 3 } ;
v e c t o r <i n t > v2 ( 2 , 3 ) ;
TDDD38
Page 3 of 5
2016-01-14
Programming exercises
6. Write your code on a file named program6.cc
Define a polymorphic class hierarchy for handling grill models. All grill models shall have a
model designation, called model for short, which is a string composed by the manufacturer
name and a model number, e.g. “Weber 4711”, and a price (double).
Grill model objects are always to be dynamically allocated and always be referenced by
pointers.
There are two main types of grills, LPG grills, which burn gas, e.g. Propane or Butane,
and charcoal grills. LPG grills come in two variants, with or without a side burner, e.g. for
boiling potatoes on the side. Charcoal grills have no specific attributes besides model and
price.
• Define a polymorphic, abstract class Grill to be used as base class for all grill model
classes. When a new Grill subobject is created, it shall always be initialized with
specific values for model and price.
• Define a direct, concrete subclass to Grill named Grill_Charcoal. When a new
Grill_Charcoal object is created, it shall always be initialized with model and price.
• Define a direct, concrete subclass to Grill named Grill_LPG to represent all LPG
grill models. Besides model and price, a Grill_LPG object shall store the type of gas
to be used (std::string). When a new Grill_LPG object is created, it shall always
be initialized with model and price. If an initial value for gas is not given explicitly,
”Propane” is default.
• Define a direct subclass to Grill_LPG named Grill_LPG_Side to represent LPG grills
with a side burner. A Grill_LPG_Side object shall store the same data as Grill_LPG
objects, and is to be initialized in the same way, see above.
All grill models shall have the following public member functions, besides required/desired
special member functions:
get_model() returns the model designation string
set_price() takes a price as argument and sets the stored price to this value
get_price() returns the price
clone() shall create a correct polymorphic copy of the object in question and return a
pointer to the new object. clone() shall be the only public way to copy grill objects.
Grill_Charcoal has no operations besides those common for all grill models, see above.
Grill_LPG shall have the following member function besides the common operations above:
get_lpg() returns the type of gas required
Declare a heterogeneous vector for storing Grill objects, and create at least one object of
each kind of concrete subclass to Grill and store in the vector. Iterate over the elements in
the vector and
• for each object, make a copy and use the copy to print model and price; if an LPG grill,
also print ”LPG” and the kind of gas required, if a charcoal grill, print “Charcoal”
• delete the copy
End the program by deleting all objects stored in the vector (not by deleting through any
separate pointers used to also refer to the objects in the vector).
No other member functions than those mentioned above are allowed! There is a purpose...
Assignment is not to be allowed, in any way!
Design with care – use C++ well – keep with the given specifications!
[5p]
TDDD38
Page 4 of 5
2016-01-14
7. The file program7.cc is written using a lot of standard iteration statements and it is not
easy to see exactly what is going on. Your task is to fix this by copying the file to your
working directory and modify it so that the code is made up of well chosen containers
and standard algorithms. The output of the program before and after your modifications
shall of course be the same, but some alterations on the order of calculation is allowed if
deemed necessary (but please comment your decisions). No standard iteration statements
are needed in your solution!
[5p]
8. Copy file program8.cc to your working directory, add your own code.
Define a template class Fixed_Buffer to be a sequence container adapter with a specific
fixed size and element type given as template arguments. Internally, the elements should
be stored in a member of type std::array. Your container shall only support the following
operations:
[5p]
push Add (overwrite) a new element after the current element.
top Access the current element.
pop Change current element.
To make the Buffer as usable as possible, you are to create two policy classes that can be
used to select what should happen at the bounds of the internal container;
Wrap Used to get a ring buffer. When this policy is used, the next element when we stand
at the last position will be the first element (and vice versa when at the beginning).
This shall be the default behavior for Fixed_Buffer.
Out_Of_Bounds Used to get a bounded buffer. Should throw a std::out_of_range
exception when trying to get the next element when at the end or when accessing
previous when at the first element.
Please note that since Fixed_Buffer is built on a fixed size array, no elements will be
removed. The pop operation only updates the current element and push will overwrite an
(possible default initialized) element.
If the user tries to access an element that hasn’t been added (for example using top on a
Fixed_Buffer without added values) a default-initialized value of the given element type
shall be returned.
See the given file for use cases on your finished class.
TDDD38
Page 5 of 5
2016-01-14
9. The file given_files/program9.cc uses the C library curses to create simple terminalbased graphics. Copy this file to your working directory and start by compiling it with the
link-flag -lcurses and run the program. The full compile command is shown below (the
link-flag must be at the end of the command!).
w++11 program9.cc -lcurses
When you run the program, you can see that the program shows an asterisk moving over
the screen and then exits gracefully. There is one problem with the given code though. If
there are any errors in our code (such as the commented out thow statement at line 36)
the curses window with all alterations to the environment (such as whether the cursor is
enabled) will be left behind.
You can see this behavior by uncommenting the throw statement (you can always run the
command reset in the terminal to get the default behavior back).
Your task is to create a class Window to encapsulate both the WINDOW pointer and the state
of the cursor (0 being off and 1 on, showing). Since the user of your class might want to
change the state of the curser during runtime (possibly to ask user for input), your class
shall have a public member function with signature void curs_set(int status); that
changes the status. Copying and moving Window objects shall not be allowed.
When you have implemented the Window class successfully, the result of the program with
the throw-statement shall be the output Some error in main! in a standard terminal, i.e.
nothing of the graphical program shall be visible.
[5p]
Download