编程辅导 CE221-5-AU

CE221-5-AU

Question 1

Copyright By PowCoder代写 加微信 powcoder

(i) Rewrite the following C++ class in two files x.h and x.cpp such that all of the [10%] 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. Appropriate header files should be included.

class Coords

{ private:

float x, y, z;

Coords(float xx, float yy, float zz)

{ x = xx; y = yy; z = zz;

void print(ostream &str) const

{ str<< '(' << x << ', ' << y << ', ' << z << ') '; (ii) Rewrite the constructor for the Coords class using an initialiser list. (iii) Modify the constructor so that both of the following declarations would be acceptable. (You must not introduce a second constructor.) Coords c1(5.5); Coords c2(3.5, -1.5); (b) (i) Explain the difference between uni-directional iterators, bi-directional iterators and random-access iterators, as provided by the Standard Template Library (STL). (ii) Briefly describe how the STL’s for_each algorithm differs from Java’s [6%] enhanced for loop (i.e. for (int i:L) …… ). (iii) 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 (s:ls) if (s.length()>5)

System.out.println(s);

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 = 5, j = 0; while (j<4) cout << ++j << ': ' << i-- << endl; (b) list ll; ll.push_back(4); ll.push_front(5); ll.push_back(6); list::reverse_iterator it;
for (it = ll.rbegin(); it != ll.rend(); it++) cout << *it << ' '; cout << endl; (c) int a[5] = { 1 }, b[] = { 4, 1, 2, 0, 3 }; int *p = a, *q = b; while (int *p = *q) // note: = not == { cout << *p++ << ' '; q++; cout << endl; priority_queue pq;

pq.push(3.4);

pq.push(4.5);

pq.push(5.6);

cout << pq.top() << endl; cout << pq.top() << endl; vector v;

v.push_back(5);

v.push_back(7);

v.push_back(3);

sort(v.begin()+1, v.end());

cout << v[0] << ' ' << v[1] << v[2] << 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). Explain what the default copy constructor and assignment operator generated by the compiler when a programmer does not provide them will do. 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*.) int *ip = new int[5]; for (int i = 0; i<5; i++) *(ip+i) = i; myfun(ip); delete [] ip; CE221-5-AU Question 4 The following is the contents of a header file for a class to store information about lecturers. #ifndef _LECT_H_ #define _LECT_H_ #include

class Lect

string name, department;

int payrollNum;

Lect(string name, string dept, int payroll); bool operator<(const Lect &) const; void print(ostream &str) const; Write a .cpp file for this class which should include complete definitions for the constructor and operator< and print functions. The < operator should compare lecturers using their payroll numbers; the print function should output all of the details of the lecturer on a single line. Assume that a function called inputLect has been written to input details of a Lecturer and create and return a Lect object. (You do not have to write this.) Write a main program that uses a for loop to make calls to inputLect to input details of about 20 lecturers and store the results of the calls in a vector, additionally maintaining a count of the number of lecturers in each department scheme in a map. After the input has been completed the program should the sort the list by payroll number and output the contents of the sorted list one lecturer per line using the print function. It should also output the name of the departments with least and most lecturers. (If there is more than one scheme with either the smallest or greatest number of lecturers it is sufficient to output just one of them.) The STL vector and map classes should be used for the list and map. END OF PAPER CE221-5-AU 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com