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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

WindowRep <strong>and</strong> Window classes 1027Frame+------------------+|Health 100|+------------------++------------------+|Direction |+------------------+LabelNumberoutput hereCursor positioned so thatinput characters appear hereFigure <strong>29</strong>.3NumberItem <strong>and</strong> EditText WindowsNumberItem<strong>The</strong> class declaration for NumberItem is:class NumberItem : public Window {public:NumberItem(int x, int y, int width, char *label,long initval = 0);void SetVal(long newVal);long GetVal() { return fVal; }private:void SetLabel(int s, char*);void ShowValue();long fVal;int fLabelWidth;};In addition to the character arrays <strong>and</strong> dimension data that a NumberItem objectalready has because it is a kind <strong>of</strong> Window, a NumberItem owns a long integerholding the value to be displayed (<strong>and</strong>, also, an integer to record the number <strong>of</strong>characters used by the label so that numeric outputs don't overwrite the label).<strong>The</strong> constructor for class NumberItem completes the normal initializationprocesses <strong>of</strong> class Window. <strong>The</strong> auxiliary private member function SetLabel() isused to copy the given label string into the background array. <strong>The</strong> inheritedPrepareContent() function loads the current array from the background <strong>and</strong> addsthe frame. Finally, using the auxiliary ShowValue() function, the initial number isconverted into characters that are added to the current image array.Once constructed, a NumberItem can be used by the program. Usually, usagewill be restricted to just three functions – GetVal() (return fVal;), SetVal()(changes fVal <strong>and</strong> uses ShowValue() to update the current image array), <strong>and</strong>ShowAll() (the function, inherited from class Window, that gets the windowdisplayed).What does aNumberItem own?What does aNumberItem do?

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

Saved successfully!

Ooh no, something went wrong!