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:friend_methods [2017/04/20 15:51] gthanos |
cpp:friend_methods [2019/04/19 10:08] gthanos [Φιλικές Κλάσεις] |
||
|---|---|---|---|
| Line 3: | Line 3: | ||
| Μία φιλική συνάρτηση της κλάσης είναι μία συνάρτηση που δεν είναι μέλος της κλάσης, | Μία φιλική συνάρτηση της κλάσης είναι μία συνάρτηση που δεν είναι μέλος της κλάσης, | ||
| - | <code cpp Rectangle.cpp> | + | <code cpp Rectangle.hpp> |
| #include < | #include < | ||
| using namespace std; | using namespace std; | ||
| Line 16: | Line 16: | ||
| int getWidth(); | int getWidth(); | ||
| int getHeight(); | int getHeight(); | ||
| - | friend void modifyDimensions(Rectangle r, int dw, int dh); | + | friend void modifyDimensions(Rectangle& r, int dw, int dh); |
| }; | }; | ||
| Line 28: | Line 28: | ||
| int Rectangle:: | int Rectangle:: | ||
| - | void modifyDimensions(Rectangle r, int dw, int dh) { | + | void modifyDimensions(Rectangle& r, int dw, int dh) { |
| r.width += dw; | r.width += dw; | ||
| r.height += dh; | r.height += dh; | ||
| Line 34: | Line 34: | ||
| </ | </ | ||
| - | <code cpp RectangleUsage.cpp> | + | <code cpp RectangleUsage.hpp> |
| - | #include " | + | #include " |
| int main () { | int main () { | ||
| Line 48: | Line 48: | ||
| ====== Φιλικές Κλάσεις ====== | ====== Φιλικές Κλάσεις ====== | ||
| - | Σε αναλογία με τις φιλικές συναρτήσεις μπορείτε να έχετε και φιλικές κλάσεις που έχουν πρόσβαση στο σύνολο των πεδίων κλάσης. Στο παρακάτω παράδειγμα, | + | Σε αναλογία με τις φιλικές συναρτήσεις μπορείτε να έχετε και φιλικές κλάσεις που έχουν πρόσβαση στο σύνολο των πεδίων κλάσης. Στο παρακάτω παράδειγμα, |
| - | + | <code cpp Rectangle.hpp> | |
| - | <code cpp Rectangle.cpp> | + | |
| #include < | #include < | ||
| using namespace std; | using namespace std; | ||
| Line 61: | Line 60: | ||
| public: | public: | ||
| Rectangle(int w, int h); | Rectangle(int w, int h); | ||
| + | Rectangle() {} | ||
| void setWidth(int w); | void setWidth(int w); | ||
| void setHeight(int h); | void setHeight(int h); | ||
| Line 77: | Line 77: | ||
| </ | </ | ||
| - | <code cpp Cuboid.cpp> | + | <code cpp Cuboid.hpp> |
| #include < | #include < | ||
| using namespace std; | using namespace std; | ||
| - | #include " | + | #include " |
| class Cuboid { | class Cuboid { | ||
| - | Rectangle | + | Rectangle |
| int length; | int length; | ||
| public: | public: | ||
| - | | + | |
| - | | + | |
| - | r.height += dh; | + | }; |
| - | | + | |
| - | } | + | Cuboid:: |
| + | rect = r; length = l; | ||
| } | } | ||
| - | < | + | |
| + | int Cuboid:: | ||
| + | return rect.width * rect.height * length; | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | <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// υπολογίζει τον όγκο έχοντας απευθείας πρόσβαση στα πεδία της κλάσης // | ||
| + | </WRAP> | ||