cpp:friend_methods
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
cpp:friend_methods [2017/04/20 15:17] – created gthanos | cpp:friend_methods [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Φιλικές | + | ====== Φιλικές |
- | Μία φιλική συνάρτηση της κλάσης είναι μία συνάρτηση που δεν είναι μέλος της κλάσης, | + | ===== Φιλικές |
- | <code cpp Rectangle.cpp> | + | Μία φιλική συνάρτηση της κλάσης είναι μία συνάρτηση που δεν είναι μέλος της κλάσης, |
+ | |||
+ | <code cpp Rectangle.hpp> | ||
#include < | #include < | ||
using namespace std; | using namespace std; | ||
Line 16: | Line 18: | ||
int getWidth(); | int getWidth(); | ||
int getHeight(); | int getHeight(); | ||
- | friend void modifyDimensions(int dw, int dh); | + | friend void modifyDimensions(Rectangle& |
}; | }; | ||
Line 28: | Line 30: | ||
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 35: | Line 37: | ||
<code cpp RectangleUsage.cpp> | <code cpp RectangleUsage.cpp> | ||
- | #include " | + | #include " |
int main () { | int main () { | ||
Line 44: | Line 46: | ||
return 0; | 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// υπολογίζει τον όγκο έχοντας απευθείας πρόσβαση στα πεδία της κλάσης // | ||
+ | </ |
cpp/friend_methods.1492701421.txt.gz · Last modified: 2017/04/20 14:17 (external edit)