CE221-5-AU
Question 1
Copyright By PowCoder代写 加微信 powcoder
Rewrite the following C++ class in two files Coords.h and Coords.cpp such that [12%] all of the 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:
int xc, yc;
Coords(int x, int y)
void print(ostream &str) const
{ str << '(' << xc << ', ' << yc << ') ';
(b) Rewrite the constructor for the Coords class using an initialiser list.
(c) Members of the Coords class represent coordinates of points on a GUI window. A user of the class wishes to be able to calculate the length of a line drawn between two points on the window. You decide to overload the % operator so that the value of the expression p%q will be the length of a straight line between p and q, when both p and q are expressions of type Coords.
Write, as a member of the Coords class, an operator% function to overload the
· operator as described above. Note that the length of the line is not necessarily an integer. You should show the changes needed to both the header file and the .cpp file.
(Using Pythagoras’s theorem, the length of a straight line between coordinates (x,y) and (v,w) will be the square root of (x-v)2+(y-w)2; the header file
The user says that she would prefer to use p–q instead of p%q to obtain the length [2%] of the line. Briefly explain why it would not be possible to overload the –operator
to allow this to be done.
CE221-5-AU
Question 2
State what would be output by each of the following C++ program fragments; you should assume that each fragment is inside a function body within a complete program and that all appropriate header files have been included.
(a) int i = 0, j =
while (++i>5)
cout << i <<
': ' << j-- << endl;
int a[] = { 3, 2, 1, 0, 4 }, b[5]; for (int i=0; i<5; i++)
{ if (!(b[i] = a[i]) break; // note: =, not == cout << b[i] << ' ';
cout << endl;
(c) map
marks[“Lisa”] = 99.9;
map
for (it = marks.begin(); it->second>20; it++) cout << it->first << ' ' << it->second << endl;
vector
v.push_back(7);
v.push_back(3);
v.push_back(4);
v.push_back(5);
sort(v.begin(), –v.end());
cout << v[0] << ' ' << v[1] << ' ' << v[2] << ' ' << v[3]
list
ll.push_back(5);
ll.push_front(2);
ll.push_back(3);
list
for (it = ll.rbegin(); it != ll.rend(); it++)
cout << *it << ' ';
cout << endl;
priority_queue
pq.push(3.1);
pq.push(5.3);
cout << pq.top() << ' ';
pq.push(4.2)
cout << pq.top() << end;
CE221-5-AU
Question 3
(a) (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 when the following program fragment is run. (You should assume that myfun takes one argument of type float*.)
float *p = new float[10];
for (int i = 0; i<10; i++)
p[i] = 0.5*i;
delete [] p;
(b) (i) Describe the circumstance in which a copy constructor and a destructor for a class would be invoked. Show how each of these functions should be declared for a class X. (Function bodies are not required.)
(ai) If the programmer does not provide a copy constructor or a destructor for a class, the compiler will generate a default version. Explain what both of these default versions would do if invoked at runtime.
(c) Assume that a Student class has a < operator that compares students by registration number and a public member function called name that return the name of a student as a string object. Write a function takes as an argument a reference to a vector of objects of type Student and uses the sort algorithm from the C++ Standard Template Library to sort the students in the vector by name. (You may, if you wish, use additional functions.)
CE221-5-AU
Question 4
The following is the contents of a header file for a class to store information about employees.
#ifndef _EMP_H_
#define _EMP_H_
using namespace std;
#include
#include
class Employee
{ private:
string name, jobTitle;
int payrollNumber;
Employee(int prNo, string name, string title);
bool operator<(const Employee &e) const;
void print(ostream &str) const;
(a) Write a .cpp file for this class which should include complete definitions for the
constructor and operator< and print functions. The < operator should compare
employees using their payroll numbers; the print function should output all of the details
of an individual employee in fixed-width fields on a single line (terminated by a newline
character).
(b) Write a main function that will create four objects of type Employee, add them to a list
(of type list
then use an iterator to output the contents of the sorted list.
END OF PAPER CE221-5-AU
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com