cpp:friend_methods
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
cpp:friend_methods [2017/04/20 15:57] – [Φιλικές Κλάσεις] gthanos | cpp:friend_methods [2019/04/19 10:04] – [Φιλικές Συναρτήσεις] gthanos | ||
---|---|---|---|
Line 3: | Line 3: | ||
Μία φιλική συνάρτηση της κλάσης είναι μία συνάρτηση που δεν είναι μέλος της κλάσης, | Μία φιλική συνάρτηση της κλάσης είναι μία συνάρτηση που δεν είναι μέλος της κλάσης, | ||
- | <code cpp Rectangle.cpp> | + | <code cpp Rectangle.hpp> |
#include < | #include < | ||
using namespace std; | using namespace std; | ||
Line 34: | Line 34: | ||
</ | </ | ||
- | <code cpp RectangleUsage.cpp> | + | <code cpp RectangleUsage.hpp> |
#include " | #include " | ||
Line 48: | Line 48: | ||
====== Φιλικές Κλάσεις ====== | ====== Φιλικές Κλάσεις ====== | ||
- | Σε αναλογία με τις φιλικές συναρτήσεις μπορείτε να έχετε και φιλικές κλάσεις που έχουν πρόσβαση στο σύνολο των πεδίων κλάσης. Στο παρακάτω παράδειγμα, | + | Σε αναλογία με τις φιλικές συναρτήσεις μπορείτε να έχετε και φιλικές κλάσεις που έχουν πρόσβαση στο σύνολο των πεδίων κλάσης. Στο παρακάτω παράδειγμα, |
<code cpp Rectangle.cpp> | <code cpp Rectangle.cpp> | ||
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 89: | Line 89: | ||
Cuboid(Rectangle r, int l); | Cuboid(Rectangle r, int l); | ||
int volume(); | int volume(); | ||
- | } | + | }; |
Cuboid:: | Cuboid:: | ||
Line 96: | Line 96: | ||
int Cuboid:: | int Cuboid:: | ||
- | return | + | return |
} | } | ||
</ | </ | ||
- | <code cpp CuboidUsage.cpp> | + | <code cpp CuboidVolume.cpp> |
#include < | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | #include " | ||
int main() { | int main() { | ||
Rectangle rectangle(5, | Rectangle rectangle(5, | ||
- | Cuboid cuboid(r, 10); | + | Cuboid cuboid(rectangle, 10); |
cout << " | cout << " | ||
} | } | ||
</ | </ | ||
+ | |||
+ | <WRAP center round tip 80%> | ||
+ | Παρατηρήστε πως η μέθοδος //volume// της κλάσης //Cuboid// υπολογίζει τον όγκο έχοντας απευθείας πρόσβαση στα πεδία της κλάσης // | ||
+ | </ |
cpp/friend_methods.txt · Last modified: 2021/05/18 06:21 (external edit)