December 1998
OP216: OBJECT ORIENTED PROGRAMMING

QUESTION 5

Total Marks: 20 Marks

Click here to access other questions

Click to access
SUGGESTED SOLUTIONS
for Question 5

 

In this question, you are to write a program to update a computer display.
(a) Explain, stating one advantage, the term inheritance in object-oriented programming.

 

[3]
(b) A geometric point consists of an x- and y-coordinate. Define a class Point that contains: (1) two private integers to record the coordinates; (2) a constructor that takes two integers and assigns the first to the x-coordinate and the second to the  y-coordinate; and (3) accessor functions for the two coordinates.

 

[5]
(c) A pixel is a point that has an associated colour, represented as an integer. Use inheritance to define a class Pixel that includes: (1) an accessor method for the colour; (2) a method set_colour that takes a colour c, and sets the pixel's colour to c.

 

[4]
(d) A computer screen is a 480 x 768 matrix of Pixels, defined by the class below.

class Screen {
private:
   const int vertical = 480;
   const int horizontal = 768;
   Pixel screen[vertical][horizontal];
};

 

(i) A pixel is on if its colour is not 0. Write a function pixel_on that takes a point, and returns 1 if the pixel at that point is set or 0 otherwise.

 

[3]
(ii) Suppose we have two points p1 = (x1, y1) and p2 = (x2,y2), where x1 < x2 and y1 < y2, that form the lower-left and upper-right corners of a rectangle on the screen.
Write a method
fill that takes two point p1 and p2 and a colour c, and sets the colour of every pixel in the rectangle described by p1 and p2 to c. State any assumptions you make.
[5]