CE221-5-AU
Question 1
Copyright By PowCoder代写 加微信 powcoder
Write a complete C++ program that will print output of the form “Hello Fred” to the
standard output, using the name that is supplied as a command-line argument. If the
program is invoked without a command-line argument the output should instead be
“Hello world”.
Explain the difference between the stack and the heap for storage and how these
relate to the lifetime of variables.
Explain how the stack and the heap would be affected by the following declaration.
(You should assume that it occurs inside a function definition.)
char *s = new char[20];
(c) (i) Describe the circumstances in which a copy constructor and a destructor for a class
would be invoked. Show how each of these would be declared for a class C. (It is
not necessary to provide function bodies.)
(ii) Explain what the default copy constructor and destructor generated by the compiler
when a programmer does not prevent them will do, and briefly describe
circumstances where this is inappropriate.
(d) If the following program fragment was run the copy constructor for the class X would
be invoked three times. Indicate clearly each of the three situations in which it would be
// the following function declarations are assumed
// a one-argument constructor
X getX(int);
void printX1(const X&);
void printX2(X);
X x(1); X y(x); X z = x;
X x2 = getX(4); printX1(x); printX2(y);
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 = 1; while (j<4) cout << j++ << ': ' << ++i << endl; (b) int i = 4; while (i!=0) { if (i%2==0) i += 3; else i = i >> 1;
cout << i << endl;
int a[5] = { 1 }, b[] = { 4, 1, 2, 0, 3 };
for (int i = 0; i<5; i++)
{ if (!(a[i] = b[i])) break;
// note: = not ==
cout << a[i];
cout << endl;
bool b1 = 0, b2 = -10;
cout << b1 << '; ' << b2 << endl;
priority_queue
pq.push(3.1);
pq.push(4.3);
pq.push(5.3);
cout << pq.top() << endl;
vector
v.push_back(2);
v.push_back(5);
v.push_back(3);
sort(v.rbegin(), v.rend());
cout << v[0] << ' ' << v[1] << ' ' << v[2] << endl;
CE221-5-AU
Question 3
The following Java classes store information about shapes and their positions on a screen [20%] window. Write a complete single-file C++ program with classes and a main function with the
same functionality. (Note that in order to achieve the same behaviour as in Java version the variable poly should be a pointer. The C++ equivalent of Java’s Math.PI is M_PI; the header file
abstract class Shape
{ protected int xpos, ypos;
public Shape(int x, int y)
{ xpos = x; ypos = y;
public abstract float area();
class Rectangle extends Shape
{ private int width, height;
public Rectangle(int w, int h, int x, int y)
{ super(x, y); width = w; height = h;
public float area()
{ return width * height;
class Circle extends Shape
{ private int diam;
public Circle(int d, int x, int y)
{ super(w, h); diam = d;
public int area()
{ float rad = diam/2.0; return rad * rad * Math.PI;
public class Q3
{ public static void main(String args[])
{ Rectangle r = new Rectangle(3, 4, 10, 20); Circle c = new Circle(5, 50, 50);
Shape s = r; System.out.println(s.area()); s = c;
System.out.println(s.area());
CE221-5-AU
Question 4
The following is the contents of a header file for a class to store information about students.
#ifndef _STUDENT_H_
#define _STUDENT_H_
#include
class Student
{ private:
string name, degreeScheme;
int regNumber;
Student(int regNo, string name, string scheme); bool operator<(const Student &s) 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 students using their registration numbers; the print function should output all of the details of the student on a single line. Assume that a function called inputStudent has been written to input details of a student and create and return a Student object. (You do not have to write this.) Write a main program that uses a for loop to make calls to inputStudent to input details of about 50 students and store the results of the calls in a list, additionally maintaining a count of the number of students on each degree scheme in a map. After the input has been completed the program should the sort the list by registration number and output the contents of the sorted list one student per line using the print function. It should also output the name of the degree schemes with least and most students. (If there is more than one scheme with either the smallest or greatest number of students you may output any one of them.) The STL list 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