13.07.2015 Views

29 The Power of Inheritance and Polymorphism

29 The Power of Inheritance and Polymorphism

29 The Power of Inheritance and Polymorphism

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Implementation: Windows classes 1045fWidth = width;fHeight = height;fFramed = framed;}fBkgd = new char* [height];fCurrentImg = new char* [height];for(int row = 0; row < height; row++) {fBkgd[row] = new char[width];fCurrentImg[row] = new char[width];for(int col = 0; col < width; col++)fBkgd[row][col] = bkgd;}Naturally, the main task <strong>of</strong> the destructor is to get rid <strong>of</strong> the image arrays:Window::~Window(){for(int row = 0; row < fHeight; row++) {delete [] fCurrentImg[row];delete [] fBkgd[row];}delete [] fCurrentImg;delete [] fBkgd;}Functions like Clear(), <strong>and</strong> Set() rely on auxiliary routines Valid() <strong>and</strong>Change() to organize the real work. Function Valid() makes certain that thecoordinates are within the window's bounds. Function Change() is given thecoordinates, <strong>and</strong> the new character. It looks after details like making certain thatthe window frame is not overwritten (if this is a framed window), arranging for arequest to the WindowRep object asking for the character to be displayed, <strong>and</strong> theupdating <strong>of</strong> the array.void Window::Clear(int x, int y){if(Valid(x,y))Change(x,y,Get(x,y,fBkgd));}void Window::Set(int x, int y, char ch){if(Valid(x,y))Change(x, y, ch);}(Function Change() has to adjust the x, y values from the 1-based scheme used forreferring to screen positions to a 0-based scheme for C array subscripting.)void Window::Change(int x, int y, char ch){if(fFramed) {if((x == 1) || (x == fWidth)) return;if((y == 1) || (y == fHeight)) return;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!