This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
|
cpp:friend_methods [2021/05/18 06:21] |
cpp:friend_methods [2021/05/18 07:21] gthanos [Φιλικές Συναρτήσεις] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Φιλικές συναρτήσεις και φιλικές κλάσεις | ||
| + | ===== Φιλικές Συναρτήσεις ===== | ||
| + | |||
| + | Μία φιλική συνάρτηση της κλάσης είναι μία συνάρτηση που δεν είναι μέλος της κλάσης, | ||
| + | |||
| + | <code cpp Rectangle.hpp> | ||
| + | #include < | ||
| + | using namespace std; | ||
| + | |||
| + | class Rectangle { | ||
| + | private: | ||
| + | int width, height; | ||
| + | public: | ||
| + | Rectangle(int w, int h); | ||
| + | void setWidth(int w); | ||
| + | void setHeight(int h); | ||
| + | int getWidth(); | ||
| + | int getHeight(); | ||
| + | friend void modifyDimensions(Rectangle& | ||
| + | }; | ||
| + | |||
| + | Rectangle:: | ||
| + | width = w; height = h; | ||
| + | } | ||
| + | |||
| + | void Rectangle:: | ||
| + | void Rectangle:: | ||
| + | int Rectangle:: | ||
| + | int Rectangle:: | ||
| + | |||
| + | void modifyDimensions(Rectangle& | ||
| + | r.width += dw; | ||
| + | r.height += dh; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code cpp RectangleUsage.cpp> | ||
| + | #include " | ||
| + | |||
| + | int main () { | ||
| + | Rectangle rect(5, | ||
| + | cout << "area: " << rect.getWidth() * rect.getHeight() << endl; | ||
| + | modifyDimensions(rect, | ||
| + | cout << "area: " << rect.getWidth() * rect.getHeight() << endl; | ||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Φιλικές Κλάσεις ===== | ||
| + | |||
| + | Σε αναλογία με τις φιλικές συναρτήσεις μπορείτε να έχετε και φιλικές κλάσεις που έχουν πρόσβαση στο σύνολο των πεδίων κλάσης. Στο παρακάτω παράδειγμα, | ||
| + | |||
| + | <code cpp Rectangle.hpp> | ||
| + | #include < | ||
| + | using namespace std; | ||
| + | |||
| + | class Rectangle { | ||
| + | friend class Cuboid; | ||
| + | private: | ||
| + | int width, height; | ||
| + | public: | ||
| + | Rectangle(int w, int h); | ||
| + | Rectangle() {} | ||
| + | void setWidth(int w); | ||
| + | void setHeight(int h); | ||
| + | int getWidth(); | ||
| + | int getHeight(); | ||
| + | }; | ||
| + | |||
| + | Rectangle:: | ||
| + | width = w; height = h; | ||
| + | } | ||
| + | |||
| + | void Rectangle:: | ||
| + | void Rectangle:: | ||
| + | int Rectangle:: | ||
| + | int Rectangle:: | ||
| + | </ | ||
| + | |||
| + | <code cpp Cuboid.hpp> | ||
| + | #include < | ||
| + | using namespace std; | ||
| + | |||
| + | #include " | ||
| + | |||
| + | class Cuboid { | ||
| + | Rectangle rect; | ||
| + | int length; | ||
| + | public: | ||
| + | Cuboid(Rectangle r, int l); | ||
| + | int volume(); | ||
| + | }; | ||
| + | |||
| + | Cuboid:: | ||
| + | rect = r; length = l; | ||
| + | } | ||
| + | |||
| + | int Cuboid:: | ||
| + | return rect.width * rect.height * length; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code cpp CuboidVolume.cpp> | ||
| + | #include < | ||
| + | using namespace std; | ||
| + | |||
| + | #include " | ||
| + | |||
| + | int main() { | ||
| + | Rectangle rectangle(5, | ||
| + | Cuboid cuboid(rectangle, | ||
| + | cout << " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <WRAP center round tip 80%> | ||
| + | Παρατηρήστε πως η μέθοδος //volume// της κλάσης //Cuboid// υπολογίζει τον όγκο έχοντας απευθείας πρόσβαση στα πεδία της κλάσης // | ||
| + | </ | ||