sample_quiz1

sample_quiz1 - 1.124 Quiz 1 Thursday October 8, 1998 Time:...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1.124 Quiz 1 Thursday October 8, 1998 Time: 1 hour 15 minutes Answer all questions. All questions carry equal marks. Question 1. The following code is to be built and run as follows: Compile as g++ -c Point.C g++ -c myprog.C Link as g++ -o myprog myprog.o Point.o Run as myprog Would you expect to see (a) a compile-time error? (b) a link-time error? (c) a run-time error? (d) none of the above errors. Explain briefly. Point.h class Point { private: int x, y; public: Point() {} void set_coords(int x, int y); Answer: }; Point.C #include "Point.h" void Point::set_coords(int x, int y) { // Assume that this sets the private data. } myprog.C #include "Point.h" extern Point a; int main() { a.set_coords(2,3); return 0; } Question 2. Fill in the body of the member function, set_coords , so that it properly sets the private member data in class Point . } Answer: void Point::set_coords(int x, int y) { Question 3. Write a member function, access_x , that can be used either to set or to get the value of the private member, x . Your function should work with the following code: class Point { private: int x, y; public: Point() {} Answer: }; int main() { Point a; int i; a.access_x() = 5; i = a.access_x(); return 0; } Question 4. Is the following class declaration valid? Explain briefly. class Point { private: int x, y; Point a; public: Point() {} }; Answer: Question 5. Examine the following code carefully and explain the exact sequence of constructor calls. class Point { private: int x, y; public: Point() { x = y = 0; } // Constructor #1 Point(int ix, int iy) { x = ix; y = iy; } // Constructor #2 Point(const Point& p) { x = p.x; y = p.y; } // Constructor #3 ~Point() {} }; Point foo(Point p) { static Point c(p); return c; } int main() { Point a(2,3); Point b; b = foo(a); a = foo(b); return 0; } Answer: Constructor # Reason Question 6.Question 6....
View Full Document

This note was uploaded on 12/04/2011 for the course ESD 1.124 taught by Professor Kevinamaratunga during the Fall '00 term at MIT.

Page1 / 12

sample_quiz1 - 1.124 Quiz 1 Thursday October 8, 1998 Time:...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online