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/22 17:22] – [Πολλαπλή κληρονομικότητα] gthanos | cpp:inheritance [2022/05/13 07:01] (current) – [Κληρονομικότητα] gthanos | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Κληρονομικότητα ====== | ====== Κληρονομικότητα ====== | ||
- | ====== Κληρονομικότητα | + | Οι κλάσεις στη C++ μπορούν να επεκταθούν μέσω της κληρονομικότητας, δημιουργώντας νέες κλάσεις που διατηρούν τα χαρακτηριστικά των προγόνων τους. Η κληρονομικότητα προϋποθέτει ότι υπάρχει η βασική κλάση (πρόγονος) από την οποία προκύπτουν μία ή περισσότερες κλάσεις (απόγονοι). Οι απόγονοι διατηρούν τα χαρακτηριστικά του προγόνου, |
- | Σε αναλογία με τη Java, οι κλάσεις στη C++ μπορούν να επεκταθούν μέσω | + | Στο παρακάτω παράδειγμα, από |
- | Στο παρακάτω παράδειγμα, | + | <code cpp Shape.hpp> |
- | + | class Shape { | |
- | <code cpp Rectangle.cpp> | + | |
- | #include < | + | |
- | 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 65: | 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 94: | 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; | + | } |
- | | + | |
- | | + | |
</ | </ | ||
- | </ | ||
- | |||
- | ===== Άλλες μορφές κληρονομικότητας ===== | ||
- | |||
- | Εκτός από την κληρονομικότητα τύπου //public// ('' | ||
- | ^ | ||
- | ^ μέλη της γονικής κλάσης | ||
- | | public μέλη | ||
- | | protected μέλη | ||
- | | private μέλη | ||
- | |||
- | Δείτε το παρακάτω παράδειγμα που παραθέτει αναλυτικά τα επίπεδα της προσβασιμότητας | ||
- | |||
- | <code cpp> | ||
- | class Parent { | ||
- | public: | ||
- | int publicMember; | ||
- | protected: | ||
- | int protectedMember; | ||
- | private: | ||
- | int privateMember; | ||
- | }; | ||
- | |||
- | class ChildA : public Parent { | ||
- | // publicMember is public | ||
- | // protectedMember is protected | ||
- | // privateMember is not accessible from ChildA | ||
- | }; | ||
- | |||
- | class ChildB : protected Parent { | ||
- | // publicMember is protected | ||
- | // protectedMember is protected | ||
- | // privateMember is not accessible from ChildB | ||
- | }; | ||
- | |||
- | class ChildC : private Parent { // ' | ||
- | // publicMember is private (accessible) | ||
- | // protectedMember is private | ||
- | // privateMember is not accessible from ChildC | ||
- | }; | ||
- | </ | ||
- | |||
- | Στο παραπάνω παράδειγμα συμπερασματικά θα μπορούσαμε να πούμε τα εξής: | ||
- | * Για την κλάση //ChildA// (//public// κληρονομικότητα) οποιαδήποτε κλάση γνωρίζει ότι η //ChildA// είναι απόγονος της //Parent//. | ||
- | * Για την κλάση //ChildB// (// | ||
- | * Για την κλάση //ChildC// (// | ||
- | |||
- | <WRAP center round tip 80%> | ||
- | Η κληρονομικότητα τύπου //private// ή // | ||
- | </ | ||
cpp/inheritance.1492881741.txt.gz · Last modified: 2017/04/22 16:22 (external edit)