This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
|
cpp:inheritance [2020/04/14 16:41] gthanos |
cpp:inheritance [2021/05/07 09:08] gthanos [Κληρονομικότητα] |
||
|---|---|---|---|
| Line 3: | Line 3: | ||
| Οι κλάσεις στη C++ μπορούν να επεκταθούν μέσω της κληρονομικότητας, | Οι κλάσεις στη C++ μπορούν να επεκταθούν μέσω της κληρονομικότητας, | ||
| - | Στο παρακάτω παράδειγμα, | + | Στο παρακάτω παράδειγμα, |
| <code cpp Shape.hpp> | <code cpp Shape.hpp> | ||
| - | |||
| class Shape { | class Shape { | ||
| unsigned int color; | unsigned int color; | ||
| Line 17: | Line 16: | ||
| void setColor(unsigned char red, unsigned char blue, unsigned char green); | void setColor(unsigned char red, unsigned char blue, unsigned char green); | ||
| unsigned int getColor(); | unsigned int getColor(); | ||
| - | unsigned char getBorderWidth(); | ||
| - | void setBorderWidth(unsigned char bw); | ||
| unsigned int getArea(); | unsigned int getArea(); | ||
| }; | }; | ||
| Line 25: | Line 22: | ||
| <code cpp Shape.cpp> | <code cpp Shape.cpp> | ||
| #include < | #include < | ||
| - | #include < | ||
| using namespace std; | using namespace std; | ||
| + | |||
| + | #include " | ||
| void Shape:: | void Shape:: | ||
| Line 51: | Line 49: | ||
| return 0; | return 0; | ||
| } | } | ||
| - | |||
| - | unsigned char Shape:: | ||
| - | void Shape:: | ||
| - | |||
| </ | </ | ||
| <code cpp Rectangle.hpp> | <code cpp Rectangle.hpp> | ||
| - | |||
| #include " | #include " | ||
| Line 71: | Line 64: | ||
| unsigned int getHeight(); | unsigned int getHeight(); | ||
| unsigned int getArea(); | unsigned int getArea(); | ||
| + | | ||
| + | unsigned char getBorderWidth(); | ||
| + | void setBorderWidth(unsigned char bw); | ||
| }; | }; | ||
| - | |||
| </ | </ | ||
| Line 78: | Line 73: | ||
| #include < | #include < | ||
| using namespace std; | using namespace std; | ||
| + | |||
| + | #include " | ||
| Rectangle:: | Rectangle:: | ||
| Line 94: | Line 91: | ||
| return Shape:: | return Shape:: | ||
| } | } | ||
| + | |||
| + | unsigned char Rectangle:: | ||
| + | void Rectangle:: | ||
| </ | </ | ||
| - | <code cpp ShapeUsage.cpp> | + | <code cpp RectangleUsage.cpp> |
| #include " | #include " | ||
| + | #include < | ||
| + | using namespace std; | ||
| int main() { | int main() { | ||
| Rectangle rectangle(0xffffff, | Rectangle rectangle(0xffffff, | ||
| + | rectangle.setBorderWidth(8); | ||
| | | ||
| cout << " | cout << " | ||
| - | cout << " color: 0x" << hex << rectangle.getColor() << dec; | + | cout << " color: 0x" << |
| cout << " borderWidth: | cout << " borderWidth: | ||
| cout << " area: " << rectangle.getArea() << endl; | cout << " area: " << rectangle.getArea() << endl; | ||
| Line 122: | Line 125: | ||
| ^ ^ μέλη της γονικής κλάσης | ^ ^ μέλη της γονικής κλάσης | ||
| ^ προσβασιμότητα | ^ προσβασιμότητα | ||
| - | | μέλη | + | | από |
| - | | μέλη | + | | από |
| - | | μη μέλη της | + | | από |
| + | ===== Κλήση μιας επανα-ορισμένης μεθόδου της γονικής κλάσης από την υποκλάση ===== | ||
| - | <WRAP center round info 80%> | + | Όταν η απόγονος κλάση επαναορίζει μία μέθοδο με το ίδιο |
| - | Όταν η απόγονος κλάση επαναορίζει μία μέθοδο με το ίδιο signature (ίδιο όνομα, ίδιος αριθμός και τύπος ορισμάτων) τότε συχνά θέλουμε να χρησιμοποιήσουμε τη γονική | + | |
| <code cpp> | <code cpp> | ||
| unsigned int Rectangle:: | unsigned int Rectangle:: | ||