CE221-5-AU
Question 1
Copyright By PowCoder代写 加微信 powcoder
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.
int i = 5, j = 0;
while (j<3)
cout << ++j << ': ' << i-- << endl;
list
ll.push_front(3);
ll.push_back(2);
ll.push_front(1);
list
for (it = ll.begin(); it != ll.end(); it++)
cout << *it << ' ';
cout << endl;
(c) int x[] = { 1, 2, 3, 0, 1, 1, 2 }, y[7] = { 0 };
int *p = x, *q = y;
while (*q = *p)
// note: = not ==
{ cout << *q << ' '; p++;
cout << endl;
(d) priority_queue
cout << pq.top() << endl;
(e) vector
cout << v[0] << ' ' << v[1] << endl;
(f) map
cout << tel.begin()->second << endl;
CE221-5-AU
Question 2
Write a complete C++ program that will print output of the form “Hello Fred” to the [8%] 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”
(b) (i) Explain the difference between data stored on the stack and on the heap.
(ii) Explain how the stack and the heap would be affected by the following declaration. [8%] (You should assume that it occurs inside a function definition.)
int *ip = new int[10];
for (int i = 0; i<10; i++)
*(ip+i) = i;
myfun(ip);
// assume that myfun takes an argument of
· type int* and returns no result
delete [] ip;
CE221-5-AU
Question 3
The following Java classes store information about shapes and their positions on a screen [22%] 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 the Java version the variable sh should be a pointer; the C++ equivalent of Java’s Math.PI is M_PI, defined in 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 Square extends Shape
{ private int length;
public Square(int x, int y, int len)
{ super(x, y); length = len;
public float area()
{ return length * length;
class Circle extends Shape
{ private int radius ;
public Circle(int x, int y, int rad)
{ super(x, y) ; radius = rad ;
public float area()
{ return radius * radius * Math.PI;
public class Q3
{ public static void main(String args[]) { Square s = new Square(10, 10, 5); Circle c = new Circle(20, 20, 7);
Shape sh = s;
System.out.println(sh.area());
System.out.println(sh.area());
CE221-5-AU
Question 4
(a) (i) 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)
(ii) Explain what the default copy constructor and assignment operator generated by the compiler when a programmer does not provide them will do, and briefly describe circumstances where this is inappropriate.
(b) 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 [10%] 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.
CE221-5-AU
Question 5
(a) (i) Briefly describe how the STL’s for_each algorithm differs from Java’s enhanced for loop (i.e. for (int i:x) …… ).
(ii) Write a C++ program fragment that uses for_each to perform the same task as the following Java loop, where li is a list of integers.
for (int i:li)
System.out.println(i);
(b) (i) Assume that v is an object of type vector
(ii) Briefly explain why the STL sort algorithm cannot be used to sort the contents of containers of type list.
(iii) Write a single line of code that could be used to replace the call to the sort algorithm in part (i) if v was of type list
END OF EXAM PAPER CE221-5-AU
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com