Paskaitų konspektai - Matematikos ir Informatikos fakultetas ...
Paskaitų konspektai - Matematikos ir Informatikos fakultetas ...
Paskaitų konspektai - Matematikos ir Informatikos fakultetas ...
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Paveld÷jimas<br />
Turime geometrinę figūrą su centro koordinat÷mis x <strong>ir</strong> y, kuri piešia save kaip vieną<br />
tašką:<br />
// shapes.h<br />
class Shape<br />
{<br />
protected:<br />
int x;<br />
int y;<br />
public:<br />
Shape (int cx, int cy);<br />
int getCenterX () const {return x;}<br />
int getCenterY () const {return y;}<br />
void setCenter (int cx, int cy) {x=cx; y=cy;}<br />
};<br />
void draw<br />
(QPainter* p);<br />
Apskritimas, kvadratas <strong>ir</strong> stačiakampis taip pat yra figūros turinčios centrą.<br />
Objektiškai orientuoto programavimo terminologijoje žodelis "yra" keičiamas<br />
žodeliu "paveldi". Taigi, apskritimas, kvadratas <strong>ir</strong> stačiakampis paveldi iš figūros<br />
visas jos savybes bei prideda papildomų duomenų <strong>ir</strong> metodų. Šis faktas C++ kalboje<br />
užrašomas tokiu būdu.<br />
class C<strong>ir</strong>cle : public Shape<br />
{<br />
protected:<br />
int radius;<br />
public:<br />
C<strong>ir</strong>cle (int cx, int cy, int radius);<br />
int getRadius () const {return radius;}<br />
void setRadius (int r) {radius = r;}<br />
void draw (QPainter* p);<br />
};<br />
class Square : public Shape<br />
{<br />
protected:<br />
int width;<br />
public:<br />
Square(int cx, int cy, int width);<br />
int getWidth () const {return width;}<br />
void setWidth (int w) {width=w;}<br />
void draw (QPainter* p);<br />
};<br />
85