29 The Power of Inheritance and Polymorphism
29 The Power of Inheritance and Polymorphism
29 The Power of Inheritance and Polymorphism
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Inheritance</strong> <strong>and</strong> <strong>Polymorphism</strong> 1036voidintintintprivate:intint};SetPt(const Pt& other);Equals(const Pt& other) const;Adjacent(const Pt& other) const;Distance(const Pt& other) const;fx;fy;Most <strong>of</strong> the member functions <strong>of</strong> Pt are sufficiently simple that they can be definedas inline functions.Classes WindowRep <strong>and</strong> Window<strong>The</strong> declaration for class WindowRep isclass WindowRep {public:static WindowRep *Instance();void CloseDown();void PutCharacter(char ch, int x, int y);void Clear();void Delay(int seconds) const;char GetChar();void MoveCursor(int x, int y);private:WindowRep();void Initialize();void PutCharacter(char ch);static WindowRep *sWindowRep;char fImage[CG_HEIGHT][CG_WIDTH];};<strong>The</strong> size <strong>of</strong> the image array is defined by constants CG_HEIGHT <strong>and</strong> CG_WIDTH (theirvalues are determined by the area <strong>of</strong> the cursor addressable screen, typically up to24 high, 80 wide).<strong>The</strong> static member function Instance(), <strong>and</strong> the static variable sWindowRepare used in the implementation <strong>of</strong> the "singleton" pattern as explained in theimplementation section. Another characteristic <strong>of</strong> the singleton nature is the factthat the constructor is private; a WindowRep object can only get created via thepublic Instance() function.Most <strong>of</strong> the members <strong>of</strong> class Window have already been explained. <strong>The</strong> actualclass declaration is:class Window {public:Window(int x, int y, int width, int height,char bkgd = ' ', int framed = 1);virtual ~Window();void Clear(int x, int y);void Set(int x, int y, char ch);void SetBkgd(int x, int y, char ch);