31.12.2014 Views

Paskaitų konspektai - Matematikos ir Informatikos fakultetas ...

Paskaitų konspektai - Matematikos ir Informatikos fakultetas ...

Paskaitų konspektai - Matematikos ir Informatikos fakultetas ...

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.

class Rectangle : public Shape<br />

{<br />

protected:<br />

int x;<br />

int y;<br />

int width;<br />

int height;<br />

public:<br />

Rectangle (int cx, int cy, int width, int height);<br />

...<br />

};<br />

class C<strong>ir</strong>cle : public Shape<br />

{<br />

protected:<br />

int cx;<br />

int cy;<br />

int radius;<br />

public:<br />

C<strong>ir</strong>cle (int cx, int cy, int radius);<br />

v<strong>ir</strong>tual int getX () const {return cx - radius;}<br />

v<strong>ir</strong>tual int getWidth () const {return radius*2 + 1;}<br />

...<br />

};<br />

Trečioji paveld÷jusi klas÷, Composite, yra sud÷tin÷ figūra:<br />

class Composite : public Shape<br />

{<br />

protected:<br />

vector shapes;<br />

public:<br />

...<br />

void addShape (Shape* shape);<br />

};<br />

Sud÷tin÷ figūra nesaugo savo matmenų, bet kiekvieną kartą skaičiuoja iš naujo:<br />

int Composite::getX() const<br />

{<br />

if (shapes.size() getX();<br />

for (unsigned i = 1; i < shapes.size(); i++)<br />

if (minX > shapes[i]->getX())<br />

minX = shapes[i]->getX();<br />

return minX;<br />

}<br />

Atkreipkime d÷mesį, jog sud÷tin÷ figūra taip pat yra figūra, t.y. sud÷tin÷ figūra gali<br />

susid÷ti <strong>ir</strong> iš kitų sud÷tinių figūrų. Sukurkime apskritimą, stačiakampį, medį,<br />

susidedantį iš apskritimo <strong>ir</strong> stačiakampio, bei šunį. Šuns galva <strong>ir</strong> uodega kiek<br />

d<strong>ir</strong>btinai sud÷sime į atsk<strong>ir</strong>ą sud÷tinę figūrą tam, kad pailiustruoti, jog sud÷tin÷ figūra<br />

gali susid÷ti iš bet kokių figūrų, tame tarpe <strong>ir</strong> kitų sud÷tinių:<br />

99

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

Saved successfully!

Ooh no, something went wrong!