CE221-5-AU
Question 1
Copyright By PowCoder代写 加微信 powcoder
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 in the cpp file. Appropriate header files should be included.
class Complex
{ private:
float real, imag;
Complex(float r, float i)
{ real = r;
void print(ostream &str) const
{ str << real << '+ ' << imag << 'i ';
Explain how a header file may prevent itself from being included more than once in any [3%] program file.
(c) Rewrite the constructor for the Complex class using an initialiser list.
(d) Modify the constructor so that both of the following declarations would be acceptable. [3%] (You must not introduce a second constructor.)
Complex c1;
Complex c2(3.5, -1.5);
Briefly explain why, if the print function were to be replaced with an overloaded [2%] version of <<, it would not be possible to write the operator<< function as a member of the class.
Write an overloaded version of operator<< that generates output in the same format [5%] as the print function.
CE221-5-AU
Question 2
(a) (i) Explain the difference between the stack and the heap for storage and how these
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.)
int *ip = new int[10];
(b) State what would be printed by each of the following program fragments; you should assume that each is part of a complete program and that all appropriate header files have been included.
= 10, j = 0;
{ i = i--;
cout << j++ << ': ' << i << endl;
(ii) list
ll.push_front(1);
ll.push_back(2);
ll.push_front(3);
list
for (it = ll.rbegin(); it != ll.rend(); it++)
cout << *it << ' '; cout << endl; (iii) int i=4; while (i) { if (!(i%2)) i+=3; else i = i>>1;
cout << i << endl; (iv) bool b1 = 10, b2 = 0; cout << b1 << '; ' << b2 << endl; (v) int a[5] = {1}, b[5] = {0, 0, 1 ,-1, 2}; for (int i = 0; i<5; i++) { if (a[i] = b[i]) break; // note: not == cout << a[i] << endl; CE221-5-AU Question 3 (a) Explain what is meant by a copy constructor and a destructor for a class and when they are used. Show how each of these would be declared for a class C. (It is not necessary to provide function bodies) (b) If the following program fragment was run the copy constructor for the class X would be invoked three times. Briefly explain each of the three situations in which it would be invoked. · the following function declarations are assumed · X getX(int); · void printX1(const X&); · void printX2(X); X x(12); X y(x); X z = x; X &xr = x; X x2 = getX(45); printX1(y); printX2(xr); CE221-5-AU Question 4 The following class stores information about musicians. class Musician { public: string name; string instrument; Musician() { this->name = “”; this->instrument = “”;
Musician(string name, string inst)
{ this->name = name; this->instrument = inst;
bool operator<(const Musician &m) const { return name < m.name; friend Musician inputMusician(); · returns an object whose member values have been · input from the keyboard The following should be written as part of a single main function. (You should assume that the inputMusician 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 inputMusician to input information about 10 musicians and store these in a vector, additionally maintaining a count of the number of musicians who play each instrument in a map. (The STL vector and map classes should be used for the vector and map.) (b) Sort the list by name and print its contents one item per line. Each output line should have the format Ringo: drums (c) Use the map to determine the most-frequently-played instrument and print this. CE221-5-AU Question 5 The following Java classes store information about shapes. Write a complete single-file C++ program with classes and a main function with the same functionality. abstract class Polygon { protected int width, height; public Polygon(int w, int h) { width = w; height = h; public abstract int area(); class Rectangle extends Polygon { public Rectangle(int w, int h) { super(w, h); public int area() { return width * height; class Triangle extends Polygon { 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[]) { Rectangle r = new Rectangle(3, 4); Triangle t = new Triangle(4, 5); Polygon poly = r; System.out.println(poly.area()); poly = t; System.out.println(poly.area()); END OF PAPER CE221-5-AU 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com