CE221-5-AU
Candidates must answer ALL questions
Copyright By PowCoder代写 加微信 powcoder
Question 1
Rewrite the following C++ class in two files Time.h and Time.cpp such that all of [11%] the members of the class are declared in the header file but the complete function definitions are written in the cpp file. Use comments to indicate clearly which file is which.
class Time
{ private:
unsigned int h, m, s; // stored using 24-hour clock public:
Time(unsigned int hr, unsigned int min, unsigned int sec)
{ h = hr<=24 ? hr : 0; m = min<=60 ? min : 0; s = sec<=60 ? sec : 0; Time& operator+=(unsigned int sec) { s += sec; if (s>=60) { m += s/60; s %= 60; } if (m>=60) { h += m/60; m %= 60; } h %= 24;
return *this;
friend ostream &operator<<(ostream&, const Time&); (b) Show how to modify the constructor so that all of the following declarations would be acceptable. (You must not introduce a second constructor.) // should initialise to Time t2(12) // should initialise to Time t3(15, 30); // should initialise to (c) Provide an implementation of the operator<< function, which should generate output of the form 9:05:25 and 14:35:05, with leading zeroes where needed for the minutes and seconds, but not for the hours. Briefly explain why the operator+= function needs to return a reference to the [2%] Time object to which it is applied. CE221-5-AU Question 2 State what would be printed by each of the following C++ program fragments; you should assume that each is part of a complete program and that all appropriate header files have been included. (a) int i = 0, j = 6; while (j>3)
cout << j-- << ': ' << ++i << endl;
list
ll.push_front(1);
ll.push_back(2);
ll.push_front(3);
list
for (it = rbegin(); it != rend(); it++)
cout << *it << ' ';
cout << endl;
(c) int a[5] = { 1 }, b[] = { 3, -1, 2, 0, 4 };
int *p = a, *q = b;
// note: = not ==
while (*p = *q)
{ cout << *p << ' ';
cout << *p << endl;
map
age[“Mike”] = 23;
age[“Ann”] = 35;
age[“Bart”] = 17;
cout << age.begin()->first << ':' << age.begin()->second<< endl; { if (!(i%2)) i+=3; else i = i>>1;
cout << i << endl; CE221-5-AU Question 3 Describe the circumstance in which a copy constructor and an assignment operator for a class would be invoked. Show how each of these would be declared for a class X. (It is not necessary to provide function bodies) If a programmer does not provide a copy constructor and/or an assignment operator for a class the compiler will generate default versions. Explain what these will do, and briefly describe circumstances where this is inappropriate. Explain the difference between data stored on the stack and on the heap. Explain how the stack and the heap would be affected by the following program fragment. (You should assume that myfun takes one argument of type int*.) new int[10]; for (int i = 0; i<10; i++) myfun(ip); CE221-5-AU Question 4 (a) Explain the difference between uni-directional iterators, bi-directional iterators and random-access iterators, as provided by the Standard Template Library (STL). (b) Briefly describe how the STL’s for_each algorithm differs from Java’s enhanced for loop (i.e. for (int i:L) … ). (c) Write a C++ program fragment that uses for_each to perform the same task as the following Java loop, where ls is a list of strings. for (String s:ls) if (s.length()>=10)
System.out.println(s);
CE221-5-AU
Question 5
The following class stores information about members of parliament (MPs). Each member represents a political party in a constituency. The attributes of a class object are an MP’s name, party and constituency.
string name;
string party;
string constituency;
MP(string name, string party, string constit)
{ this->name = name; this->party = party; constituency = constit;
bool operator<(const MP &mp) const { return name < mp.name; friend MP inputMP(); · returns an object whose member values have been · input from the keyboard; The answers to the following three sections should be written as part of a single main function. (You may assume that the inputMP function has been written in a separate file and all appropriate #include directives have been used.) (a) Use a for loop to make calls to inputMP to input information about 20 MPs and store the results of the calls in a vector, additionally maintaining a count of the number of MPs from each party in a map. (The STL vector and map classes should be used for the vector and map.) (b) Sort the vector by name and print its contents one item per line. Each output line should have the format : ; Ambridge (c) Use the map to determine the party with the most MPs in the collection and print this. END OF EXAM PAPER CE221-5-AU 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com