TDDD38 Advanced Programming in C++ Computer examination in

advertisement
LINKÖPINGS UNIVERSITET
Department of Computer and Information Science
Software and Systems
Tommy Olsson
2015-06-01
Computer examination in
TDDD38 Advanced Programming in C++
Exam code:
DAT1
Date:
2015-06-03
Time:
14-19
On duty:
Tommy Olsson, 28 1954
Teacher on duty will visit the locations approximately once every hour to
answer questions about the exam. Otherwise to be called upon only for
urgent matter, such as computer system problems.
Administrator:
Anna Grabska Eklund, 28 2362
Means of assistance: An English-* vocabulary may be brought to the exam.
cplusplus.com Reference, se instructions for how to browse.
Scrap paper and pen.
No other means of assistance (printed or electronic) are allowed.
Grading:
Each theory question is worth 1 credit, each programming problem 5 credits;
total 25: 0-10 for grade U/FX, 11-14 for grade 3/C, 15-18 for grade 4/B, and
19-25 for grade 5/A.
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).
Files for examination must be submitted via the Student Client, see separate
instructions!
In case names for program entities are specified, use those exactly as given.
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 when there are C++ alternatives.
Follow specifications – design with care – use C++ properly!
Good luck!
TDDD38 Advanced Programming in C++
2 (6)
Theory questions
Copy THEORY.TXT from directory given_files to login directory:
cp given_files/THEORY.TXT .
Note trailing period. Fill in your personal data at the top of the file, and give your answers to the theory
questions below in that file.
1.
Explain, by example and words, the concepts static type and dynamic type.
❏
2.
Some operators cannot be overloaded at all, some operators must be overloaded as member
functions, but for most overloadable operators there is a choice between member and non-member.
What you would chose, and why, if you were to overload, for some type,
a) operator<< to print objects of that type to an output stream
b) operator+= for that type
❏
3.
Suppose we have a type T. When passing an object of type T to a function, how would you declare
the parameter type in the following cases? Motivate!
a) T is a simple type, such as, for example int, and the parameter is used as an “in” parameter.
b) T is a complicated type, for example a non-trivial class type, and the parameter is used as an
“in” parameter.
❏
4.
Which are the (two) most common reasons for explicitly specializing a template?
❏
5.
What is expected from an operation promising ”strong exception safety”?
❏
TDDD38 Advanced Programming in C++
3 (6)
Programming problems
6.
Copy program6.cc from given_files. The file have instructions about creating and testing objects.
Construct a polymorphic class hierarchy for different type of pens.
• Pen shall be an abstract base class for all pen classes. All pens have an item number and
information about the colour of the ink supplied when shipping the pens from the factory. The
item number shall be stored as int and the ink colour as std:.string.
A new Pen shall be initialized with item number and ink colour, no defaults.
• Ballpoint shall be a concrete, direct subclass to Pen. Ballpoint shall have no attributes of its own.
A new Ballpoint pen shall be initialized with item number and ink colour, with "Blue" as default,
if no ink colour is explicitly given.
• Fountain shall be an abstract, direct subclass to Pen, and serve as a common base class for all
fountain pen types. A fountain pen have either a replaceable cartridge or a built-in reservoir for
the ink. Fountain shall have no data members of its own.
A new Fountain object shall be initialized with item number and ink colour, with "Blue" as
default, if not explicitly supplied.
• Cartridge shall be a concrete, direct subclass to Fountain, to represent pens with a replaceable
cartridge. Cartridge shall store which type of cartridge that fits the pen, as a std::string, for
example ”cr4711”.
A new Cartridge object shall be initialized with item number, cartridge type, and ink colour. If
not explicitly supplied, default for cartridge type is "cr1001", and default for ink colour is "Blue".
If only item number is given, cartridge type is always "cr1001", and ink colour is always "Blue",
by default.
• Reservoir shall be a concrete, direct subclass to Fountain, to represent pens with a built-in
reservoir. Reservoir shall store the volume of the pen reservoir, as an int (ml).
A new Reservoir object shall be initialized with item number, reservoir volume, and ink colour.
If not explicitly supplied, ink colour shall be "Blue" by default.
Data members for the classes above are never to be modified after initialization.
Objects are supposed to always be created dynamically, and handled with pointers.
All classes shall have the following public member functions
• Each class shall have a get function for each data member, for example Pen shall have member
functions get_item_number() and get_ink_colour().
• clone() shall be the only public way to make a copy of a pen object, including assignment.
clone() shall create a dynamically allocated object and return a pointer to the new object. The
copy shall be initialized using the copy constructor.
No other functions than those mentioned above are allowed.
Special member functions to be allowed, and the compiler can generate shall be defaulted; special
member functions which are not to be allowed, and the compiler will generate shall be deleted;
special member functions which are not to be allowed, and the compiler will not generate shall not
be declared.
Output from the program should be as below (the program shall create corresponding Pen objects
for testing, see the given file).
Pen
Pen
Pen
Pen
Pen
no.30001,
no.10001,
no.10205,
no.21015,
no.22020,
ballpoint,
cartridge,
cartridge,
reservoir,
reservoir,
Black
cr1001/Blue
cr2005/Red
15cl/Blue
20cl/Black
Design with care – use C++ well – keep with and enforce the given specifications!
❏
TDDD38 Advanced Programming in C++
7.
4 (6)
Write your code on a file named program7.cc.
In directory given_files there is a text file named humorous-xmas-poem.txt, containing words i
free format, which is to be used as input.
Please note! Standard library components shall be used whenever possible, and always try to find
“best fit” and natural solutions. Avoid hand-written loops (for, while or do, including range for
even if such could be a good alternative), use standard algorithms when suitable. If a user defined
function is required, use lambda expression or function object, not normal function.
Input shall be read from the standard input stream, cin. Input from a text file can the be read by
redirection on the command line.
a.out < humorous-xmas-poem.txt
Output from step 1–7 below shall be written to standard output stream, cout. Output from step
8–9 shall be written to a text file, always named result7.txt.
The program shall do the following, step by step, and in each step, no more, no less than specified!
Mark the beginning of each step with a comment on the form // 1, // 2, and so on, on a single line.
1) Make sure the output file result7.txt can be opened. If not, the program shall print an error
message and terminate.
2) Read all words from the input file and store in an std::vector, here named first vector. The words
shall be stored in the same order as they were read from the file.
3) Print how many words there are in first vector.
4) Sort the words in first vector in alphabetic order.
5) Print the words in first vector, with one space character after each word.
6) Copy the words in first vector to another std::vector, here named second vector. Each specific
word is to be copied just once, i.e. no duplicate words are to be found in second vector. The
content of first vector must not be modified.
7) Print how many unique words there are in second vector.
8) Reorder the words in second vector, so they are ordered primarily by word length, secondarily
by alphabetic order, i.e. the shortest words are to appear first in second vector, and words with
the same length shall be ordered alphabetically.
9) The words in second vector, shall be written to a text file named result7.txt, with one space
character after each word.
Output from the program shall be as follows:
Number of words read: 522
Words sorted in alphabetic order:
a a a a a a a a a a a a a a again again again ...
... way way way with with with with with with with with with
Number of unique words: 60
The ordered unique words are written to file ’result7.txt’.
The content of the file result7.txt could look as follows (as a single line):
a an do if is it me my of on to and ask bug ...
... reproduce supported documentation
❏
TDDD38 Advanced Programming in C++
8.
5 (6)
Copy the program8.cc from given_files. The file contains a program skeleton and comments.
In the given program there is a vector with the balance of six bank accounts. Now, yearly interest
is to be added to the accounts, by using algorithm std::for_each and a function object of type
Add_Interest, see below.
Construct a function object class Add_Interest with the following features.
• An object of type Add_Interest shall always be explicitly initialized with the interest rate in
question, for example 5.5, if 5.5 percent interest is to be added to the bank accounts.
• An object of type Add_Interest shall, repeatedly, be able to take the balance of one bank account
at the time, compute the interest for the account, and add the interest to the balance of the
account.
• An object of type Add_Interest shall also accumulate the total interest that have been added to
all accounts the object have operated upon since its initialization.
• Add_Interest shall be a template, so one can chose the data type for the interest rate and interest
sum. This monetary type is expected to be some floating point type, for example long double.
• Add_Interest objects shall be copyable, moveable, and assignable.
Add_Interest shall, besides what is required for a fully featured function object class and what is
said above, have the following public member functions:
• get_interest_rate() to return the interest rate the function object was initialized with, for
example 5.5 if the interest rate was 5.5 percent.
• get_interest_sum() to return that interest sum that have been accumulated after the object have
been used to deliver interest to accounts.
Also, overload operator<< to print an Add_Interest object to an output stream the following way:
Interest rate 5.0%, total interest delivered: 10000
The output from the program shall be as below, after 5 percent interest have been added.
Account balances, after interest 5% has been added:
10500
10500
21000
31500
52500
84000
Interest rate 5.0 %, total interest added to accounts: 10000
❏
TDDD38 Advanced Programming in C++
9.
6 (6)
Copy program9.cc from given_files. The file contains an incomplete definition of a class Date,
and a test program.
Big endian, yyyy-mm-dd (2015-04-09), Little endian, dd-mm-yyyy (09-04-2015), and Middle
endian, mm-dd-yyyy (04-09-2015), are different date forms. The delimiter used, ’-’, can also be,
for example, a slash, as in 09/04/2015.
The given constructor for Date supports Big endian, since the parameters first, second, and third
are used to initialize data members year, month and day, in that order. In the Little endian form,
first would represent day, and should then initialize member day, and third would represent year,
and should then initialize member year, while second correctly represents month. For Middle
endian, first should initialize month, second should initialize day, and third should initialize year.
To make Date adaptable to different date forms, make Date a template with two parameters:
• a parameter for a date form policy. Big endian shall be the default policy.
• a non-type parameter for the delimiter to be used in string representation. Dash (’-’) shall be
default.
Date objects can then be declared as the following examples shows:
Date<> d1{ 2015, 4, 9 };
// uses defaults; 2015-04-09
Date<Little_Endian, ’/’> d2{ 9, 4, 2015 }; // 09/04/2015
Besides the given constructor, Date shall have two member functions:
• set_date(), with parameters first, second, and third, to set the values of year, month, and day.
Which parameter to use for a specific member depends on the date form. For Big endian the
arguments given are supposed to correspond to year, month, and day. For Little endian
day, month, and year. For Middle endian month, day, and year. To do this, the data members,
year, month, and day, are first set to the values of the parameter first, second, and third,
respectively. Then a policy function swap_into_position(year, mont, day) is called to swap the
values of the data members, into their correct position (for Big endian no swapping is required).
• get_date() shall return the string representation. A policy operation to_string() shall be called
by get_date() to create the string, according to the date form and the delimiter in question.
Define three date form policy classes, Big_Endian, Little_Endian, and Middle_Endian. These
policy classes shall have two member functions, mentioned above:
• swap_into_position() have three parameters named year, month, and day. The arguments are
suppose to be the corresponding Date data members year, month, and day, after they have been
initialized by first, second, and third in the constructor, or after being assigned to first, second,
and third in set_date(). If the date form is Big endian the data members already have correct
values. If Little endian, data member year holds the day value, and data member day holds the
year value, so they have to be swapped to make the data members store values corresponding to
their names. For Middle endian none of year, month, and day have correct values, so all three
have to be swapped.
• to_string() shall return a string representation of a date, for example ”2015-04-09”. to_string()
have four parameters, year, month, day, and delimiter. In the returned string, year, month, day
shall be ordered according to the date policy and separated by the delimiter in question. Day and
month values shall always represented by two digits, for example 09/04/2015.
Complete class Date for the Big endian case and test. Then define the policy classes Big_Endian,
Little_Endian, and Middle_Endian, and modify Date to be a template, as specified above. Adapt
the test program successively.
Date shall have full set of special member functions, except the default construction.
Hint: Use an output string streams to generate the string representation to be returned by to_string().
❏
Download