CE221-5-AU
Candidates must answer ALL questions
Copyright By Assignmentchef assignmentchef
Question 1
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 written in the cpp file. Use comments to indicate clearly which file is which. Appropriate header files should be included.
class Complex
{ private:
float real, imag;
Complex(float r, float i)
{ real = r;
Complex operator+(const Complex &c) const
{ Complex res(*this); res.real += c.real; res.imag += c.imag;
return res;
friend ostream &operator<<(ostream&, const Complex&);(b) Rewrite the constructor for the Complex class using an initialiser list.Provide an implementation of the operator<< function, which should generate [5%] output of the form 3.5+4.47i, where the first number is the value of the real memberand the second number is the value of the imag member.Briefly explain why the operator<< function could not be written as a member of [2%] the class.CE221-5-AUQuestion 2State 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;int i = 4;while (i!=0){ if (i%2==0) i += 3;else i = i>>1;
cout << i << endl;(c) int a[5] = { 1 }, b[] = { 3, -1, 2, 0, 4 };for (int i = 0; i<5; i++){ if (!(a[i] = b[i])) // note: = not == break;cout << a[i] << endl;(d) bool b1 = 0, b2 = -10;cout << b1 << ‘; ‘ << b2 << endl;(e) vector
cout << v[1] << ‘ ‘ << v[0] << endl;(f) list
for (it = ll.rbegin(); it != ll.rend(); it++) cout << *it << ‘ ‘;cout << endl;CE221-5-AUQuestion 3(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. (You should assume that it occurs inside a function definition.)int *ip = new int[10];(b) 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)(c) 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 invoked.// the following function declarations are assumed// a one-argument constructor X getX(int); void printX1(const X&); void printX2(X);X x(12); X y(x); X z = x;X x2 = getX(45); printX1(x); printX2(y);CE221-5-AUQuestion 4The following class stores information about students.class Student{ public: string name;string degreeScheme;{ this->name = ;
this->degreeScheme = ;
Student(string name, string deg)
{ this->name = name; this->degreeScheme = deg;
bool operator<(const Student &e) const{ return name < e.name;friend Student inputStudent(); returns an object whose member values have been input from the keyboard;The answers to the following should be written as part of a single main function. (You should assume that the inputStudent 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 inputStudent to input information 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. (The STL list and map classes should be used for the vector and map.)(b) Print the names of all degree schemes with more than 10 students, along with their numbers of students, one per line. Each output should have the formatComputer Science: 12Also output the name of the degree scheme with most students. (If there is more than one such scheme you may output any one of them.)CE221-5-AUQuestion 5The following Java classes store information about polygons.Write a complete single-fileC++ 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 poly should be a pointer.)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());System.out.println(poly.area());END OF EXAM PAPER CE221-5-AU CS: assignmentchef QQ: 1823890830 Email: [email protected]
Reviews
There are no reviews yet.