程序代做 CE221-5-AU

CE221-5-AU

Question 1

Copyright By PowCoder代写 加微信 powcoder

Write a complete C++ program that will print a message that is supplied as a command-

line argument to the standard output, one word per line, and on a separate line, output the

number of words in the command-line argument. For example, if the program was run

with a command of the form myprog Hello world the output would be

Explain what is meant by a copy constructor and an assignment operator for a class and

describe in what circumstances they are used. Show how each of these would be declared

for a class C. (It is not necessary to provide function bodies)

If the following program fragment was run the copy constructor for the class X would be

invoked three times and the assignment operator once. State precisely each of the four

situations in which they would be used, indicating clearly which is being invoked.

// the following function declarations are assumed

// a no-argument constructor

X getX(int);

void print1(const X&);

void print2(X);

X y(x); X z = x; print1(getX(45)); print2(z);

(i) Explain the difference between the stack and the heap for storage and how these [4%] relate to the lifetime of variables.

(ii) Explain how the stack and the heap would be affected by the following declaration. [3%] (You should assume that it occurs inside a function definition.)

float *fp = new float[10];

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, a[] = { 1, 2, 3, 4, 5, 6 }; while (i>2)
cout << a[i--] << '; ' << a[++j] << endl; (b) int i = 4; while (i!=0) { if (i%2==0) i += 3; else i = i>>1; cout << i << endl; bool b1 = 0, b2 = -10; cout << b1 << '; ' << b2 << endl; vector v;

v.push_back(5);

v.push_back(3);

v.push_back(9);

sort(v.rbegin(), v.rend());

cout << v[0] << ' ' << v[1] << ' ' << v[2] << endl; list ll;

ll.push_front(1);

ll.push_back(2);

ll.push_front(4);

list::const_reverse_iterator it;

for (it = ll.rbegin(); it != ll.rend(); it++)

cout << *it << ' '; cout << endl; map age;

age.insert(make_pair(“Mike”, 18));

age[“Bart”] = 15;

age[“Lisa”] = 12;

age[“Marge”] = 25;

map::const_iterator it;

for (it = age.begin(); it->second<20; it++) cout << it->first << ' ' << it->second << endl; CE221-5-AU Question 3 The following Java classes store information about polygons in a list. Write a complete single- [25%] file C++ program with classes and a main function with the same functionality. You should use a the vector class to implement the list; note that in order to achieve the same behaviour as in Java version the vector must store pointers to Polygon objects abstract class Poly { protected int width, height; public Poly(int w, int h) { width = w; height = h; public abstract int area(); class Square extends Poly { public Square(int s) { super(s, s); public int area() { return width * height; class Triangle extends Poly { public Triangle(int w, int h) { super(w, h); public int area() { return (width * height)/2; public class Q5 { public static void main(String args[]) { List l = new ArrayList;

l.add(new Triangle(4, 5)); // add adds to end l.add(new Square(7));

Iterator> = l.iterator(); while (l.hasNext())

System.out.println(l.next().area());

CE221-5-AU

Question 4

The following class stores information about lecturers.

class Lecturer

{ public: string name;

string department; unsigned int phone;

Lecturer(string name, string dep, unsigned int phone)

{ this->name = name; this->department = dep; this->phone = phone;

bool operator<(const Lecturer &e) const { return name < e.name; friend Student inputLect(); // returns an object whose member values have been input The answers to the following should be written as part of a single main function. (You should assume that the inputLect function has been written in a separate file and all appropriate #include directives have been used.) Use a loop to make calls to inputLect to input details of 100 lecturers and store the results of the calls in a list, additionally maintaining a count of the number of students on each department in a map. (The STL list and map classes should be used for the list Sort the list by name and print its contents one item per line. Each output line should have the format : Computer Science: 2377 Print the names of all departments with more than 5 lecturers, along with their numbers of students, one per line. Each output should have the format Computer Science: 6 Also output the name of the department with most lecturers. (If there is more than one such department you may output any one of them.) END OF PAPER CE221-5-AU 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com