cpp:inheritance
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| cpp:inheritance [2017/04/25 04:11] – gthanos | cpp:inheritance [2022/05/13 07:01] (current) – [Κληρονομικότητα] gthanos | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Κληρονομικότητα ====== | ====== Κληρονομικότητα ====== | ||
| - | Σε αναλογία με τη Java, οι κλάσεις στη C++ μπορούν να επεκταθούν μέσω της κληρονομικότητας, | + | Οι κλάσεις στη C++ μπορούν να επεκταθούν μέσω της κληρονομικότητας, |
| - | Στο παρακάτω παράδειγμα, | + | Στο παρακάτω παράδειγμα, |
| - | <code cpp Rectangle.cpp> | + | <code cpp Shape.hpp> |
| - | #include < | + | class Shape { |
| - | using namespace std; | + | |
| - | + | ||
| - | class Rectangle | + | |
| - | private: | + | |
| - | int width; | + | |
| protected: | protected: | ||
| - | | + | |
| public: | public: | ||
| - | | + | |
| - | | + | |
| - | void setWidth(int w); | + | void setColor(unsigned |
| - | void setHeight(int h); | + | void setColor(unsigned char red, unsigned char blue, unsigned char green); |
| - | int getWidth(); | + | |
| - | int getHeight(); | + | |
| }; | }; | ||
| - | |||
| - | Rectangle:: | ||
| - | width = w; height = h; | ||
| - | } | ||
| - | |||
| - | bool Rectangle:: | ||
| - | if(r.width == width && r.height == height) | ||
| - | return true; | ||
| - | return false; | ||
| - | } | ||
| - | |||
| - | void Rectangle:: | ||
| - | void Rectangle:: | ||
| - | int Rectangle:: | ||
| - | int Rectangle:: | ||
| </ | </ | ||
| - | <code cpp Square.cpp> | + | <code cpp Shape.cpp> |
| - | # | + | # |
| + | using namespace std; | ||
| - | class Square: public Rectangle { | + | #include " |
| - | int color; | + | |
| - | | + | |
| - | | + | |
| - | bool equals(Square &s); | + | |
| - | void setColor(int c); | + | |
| - | void setColor(unsigned char r, unsigned char b, unsigned char g); | + | |
| - | int getColor(); | + | |
| - | int getArea(); | + | |
| - | }; | + | |
| - | Square:: | + | void Shape::setColor(unsigned |
| - | void Square:: | + | void Shape:: |
| - | void Square:: | + | |
| color = red; | color = red; | ||
| color <<= 8; | color <<= 8; | ||
| Line 63: | Line 35: | ||
| } | } | ||
| - | int Square:: | + | unsigned |
| return color; | return color; | ||
| } | } | ||
| - | int Square::getArea() { | + | Shape::Shape(unsigned int c, unsigned char bw) : color(c), borderWidth(bw) { |
| - | // height is accessible from Square | + | |
| - | // since it is a protected member. | + | |
| - | return getWidth() * height; | + | |
| } | } | ||
| - | bool Square::equals(Square &s) { | + | Shape::Shape(unsigned char red, unsigned char blue, unsigned char green, unsigned char bw) : borderWidth(bw) { |
| - | if( Rectangle::equals(s) && s.color == color) | + | |
| - | | + | } |
| - | | + | |
| + | unsigned int Shape::getArea() { | ||
| + | return | ||
| } | } | ||
| </ | </ | ||
| - | <code cpp SquareUsage.cpp> | + | <code cpp Rectangle.hpp> |
| - | #include "Square.cpp" | + | #include " |
| + | |||
| + | class Rectangle : public Shape { | ||
| + | private: | ||
| + | unsigned int width, height; | ||
| + | public: | ||
| + | Rectangle(unsigned int c, unsigned char bw, unsigned int w, unsigned int h); | ||
| + | void setWidth(unsigned int w); | ||
| + | void setHeight(unsigned int h); | ||
| + | unsigned int getWidth(); | ||
| + | unsigned int getHeight(); | ||
| + | unsigned int getArea(); | ||
| + | |||
| + | unsigned char getBorderWidth(); | ||
| + | void setBorderWidth(unsigned char bw); | ||
| + | }; | ||
| + | </ | ||
| + | |||
| + | <code cpp Rectangle.cpp> | ||
| + | #include < | ||
| + | using namespace std; | ||
| + | |||
| + | #include "Rectangle.hpp" | ||
| + | |||
| + | Rectangle:: | ||
| + | Shape(c, bw) , width(w), height(h) { } | ||
| + | |||
| + | void Rectangle:: | ||
| + | void Rectangle:: | ||
| + | unsigned int Rectangle:: | ||
| + | unsigned int Rectangle:: | ||
| + | |||
| + | unsigned int Rectangle:: | ||
| + | /* this is how you call a function | ||
| + | * from the parent class. | ||
| + | */ | ||
| + | int area = Shape:: | ||
| + | return Shape:: | ||
| + | } | ||
| + | |||
| + | unsigned char Rectangle:: | ||
| + | void Rectangle:: | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | # | ||
| + | #include < | ||
| + | using namespace std; | ||
| int main() { | int main() { | ||
| - | | + | |
| - | cout << "Square dimensions: | + | rectangle.setBorderWidth(8); |
| - | cout << "Square | + | |
| + | cout << "[Rectangle Properties] | ||
| + | cout << | ||
| + | cout << " | ||
| + | cout << " area: " << | ||
| + | | ||
| return 0; | return 0; | ||
| } | } | ||
| Line 92: | Line 115: | ||
| Από τα παραπάνω παρατηρούμε τα εξής: | Από τα παραπάνω παρατηρούμε τα εξής: | ||
| - | - Η κληρονομικότητα δηλώνεται μέσω της δήλωσης '' | + | - Η κληρονομικότητα δηλώνεται μέσω της δήλωσης '' |
| - | - Η απόγονος κλάση μπορεί να ορίσει επιπλέον πεδία και επιπλέον μεθόδους. | + | - Η απόγονος κλάση μπορεί να ορίσει επιπλέον πεδία και επιπλέον μεθόδους. |
| - | - Τα μέλη της γονικής κλάσης //Rectangle// που είναι δηλωμένα ως // | + | - Η απόγονος κλάση |
| - | - Όπως και στη Java, τα μέλη της γονικής κλάσης //Rectangle// ή της απογόνου κλάσης | + | - Τα μέλη της γονικής κλάσης //Shape// που είναι δηλωμένα ως // |
| + | - Τα μέλη της γονικής κλάσης //Shape// που είναι δηλωμένα ως //private// δεν είναι προσβάσιμα από την απόγονο κλάση //Rectangle//. | ||
| + | - Όπως και στη Java, τα μέλη της γονικής κλάσης //Shape// ή της απογόνου κλάσης | ||
| Συνοπτικά ο πίνακας προσβασιμότητας παρατίθεται παρακάτω: | Συνοπτικά ο πίνακας προσβασιμότητας παρατίθεται παρακάτω: | ||
| ^ ^ μέλη της γονικής κλάσης | ^ ^ μέλη της γονικής κλάσης | ||
| ^ προσβασιμότητα | ^ προσβασιμότητα | ||
| - | | μέλη | + | | από |
| - | | μέλη | + | | από |
| - | | μη μέλη της | + | | από |
| - | <WRAP center round info 80%> | + | ===== Κλήση μιας επανα-ορισμένης μεθόδου της |
| - | Όταν μία κλάση έχει πρόσβαση σε ένα | + | |
| - | </ | + | |
| - | <WRAP center round info 80%> | + | Όταν η απόγονος κλάση επαναορίζει μία μέθοδο με το ίδιο |
| - | Όταν η απόγονος κλάση επαναορίζει μία μέθοδο με το ίδιο signature (ίδιο όνομα, ίδιος αριθμός και τύπος ορισμάτων) τότε συχνά θέλουμε να χρησιμοποιήσουμε τη γονική | + | |
| <code cpp> | <code cpp> | ||
| - | bool Square::equals(Square &s) { | + | unsigned int Rectangle::getArea() { |
| - | if( Rectangle::equals(s) && s.color == color) | + | |
| - | return true; | + | } |
| - | | + | |
| - | | + | |
| </ | </ | ||
| - | </ | ||
cpp/inheritance.1493093489.txt.gz · Last modified: 2017/04/25 03:11 (external edit)
