This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
|
cpp:inheritance [2020/04/14 16:35] gthanos |
cpp:inheritance [2021/05/07 09:08] gthanos [Κλήση μιας επανα-ορισμένης μεθόδου της γονικής κλάσης από την υποκλάση] |
||
|---|---|---|---|
| Line 3: | Line 3: | ||
| Οι κλάσεις στη C++ μπορούν να επεκταθούν μέσω της κληρονομικότητας, | Οι κλάσεις στη C++ μπορούν να επεκταθούν μέσω της κληρονομικότητας, | ||
| - | Στο παρακάτω παράδειγμα, | + | Στο παρακάτω παράδειγμα, |
| - | + | ||
| - | <code cpp Shape.cpp> | + | |
| - | #include < | + | |
| - | #include < | + | |
| - | using namespace std; | + | |
| - | + | ||
| - | #ifndef __SHAPE2D__ | + | |
| - | #define __SHAPE2D__ | + | |
| + | <code cpp Shape.hpp> | ||
| class Shape { | class Shape { | ||
| unsigned int color; | unsigned int color; | ||
| Line 23: | 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(); | ||
| }; | }; | ||
| + | </ | ||
| + | |||
| + | <code cpp Shape.cpp> | ||
| + | #include < | ||
| + | using namespace std; | ||
| + | |||
| + | #include " | ||
| void Shape:: | void Shape:: | ||
| Line 41: | Line 39: | ||
| } | } | ||
| - | Shape:: | + | Shape:: |
| + | } | ||
| Shape:: | Shape:: | ||
| setColor(red, | setColor(red, | ||
| Line 49: | Line 49: | ||
| return 0; | return 0; | ||
| } | } | ||
| - | |||
| - | unsigned char Shape:: | ||
| - | void Shape:: | ||
| - | |||
| - | #endif | ||
| </ | </ | ||
| - | <code cpp Rectangle.cpp> | + | <code cpp Rectangle.hpp> |
| - | #include < | + | #include " |
| - | using namespace std; | + | |
| - | + | ||
| - | #include " | + | |
| class Rectangle : public Shape { | class Rectangle : public Shape { | ||
| Line 72: | Line 64: | ||
| unsigned int getHeight(); | unsigned int getHeight(); | ||
| unsigned int getArea(); | unsigned int getArea(); | ||
| + | | ||
| + | unsigned char getBorderWidth(); | ||
| + | void setBorderWidth(unsigned char bw); | ||
| }; | }; | ||
| + | </ | ||
| + | |||
| + | <code cpp Rectangle.cpp> | ||
| + | #include < | ||
| + | using namespace std; | ||
| + | |||
| + | #include " | ||
| Rectangle:: | Rectangle:: | ||
| Line 83: | Line 85: | ||
| unsigned int Rectangle:: | unsigned int Rectangle:: | ||
| + | /* this is how you call a function | ||
| + | * from the parent class. | ||
| + | */ | ||
| + | int area = Shape:: | ||
| 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 113: | Line 125: | ||
| ^ ^ μέλη της γονικής κλάσης | ^ ^ μέλη της γονικής κλάσης | ||
| ^ προσβασιμότητα | ^ προσβασιμότητα | ||
| - | | μέλη | + | | από |
| - | | μέλη | + | | από |
| - | | μη μέλη της | + | | από |
| + | ===== Κλήση μιας επανα-ορισμένης μεθόδου της γονικής κλάσης από την υποκλάση ===== | ||
| - | <WRAP center round info 80%> | + | Όταν η απόγονος κλάση επαναορίζει μία μέθοδο με το ίδιο |
| - | Όταν η απόγονος κλάση επαναορίζει μία μέθοδο με το ίδιο signature (ίδιο όνομα, ίδιος αριθμός και τύπος ορισμάτων) τότε συχνά θέλουμε να χρησιμοποιήσουμε τη γονική | + | |
| <code cpp> | <code cpp> | ||
| unsigned int Rectangle:: | unsigned int Rectangle:: | ||
| Line 125: | Line 137: | ||
| } | } | ||
| </ | </ | ||
| - | </ | ||