User Tools

Site Tools


cpp:friend_methods

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
cpp:friend_methods [2017/04/20 15:17] – created gthanoscpp:friend_methods [2017/04/20 16:11] – [Φιλικές Κλάσεις] gthanos
Line 1: Line 1:
 ====== Φιλικές Συναρτήσεις ====== ====== Φιλικές Συναρτήσεις ======
  
-Μία φιλική συνάρτηση της κλάσης είναι μία συνάρτηση που δεν είναι μέλος της κλάσης, αλλά έχει πρόσβαση στα //private// και //protected// μέλη της κλάσης, όπως θα είχε και μία συνάρτηση μέλος. Για να είναι μία συνάρτηση φιλική (//friend//) θα πρέπει να την δηλώσετε ως τέτοια στη δήλωση της κλάσης. Δείτε το παράδειγμα της συνάρτησης //modifyDimensions// η οποία είναι φίλια συνάρτηση της κλάσης //Rectangle//+Μία φιλική συνάρτηση της κλάσης είναι μία συνάρτηση που δεν είναι μέλος της κλάσης, αλλά έχει πρόσβαση στα //private// και //protected// μέλη της κλάσης, όπως θα είχε και μία συνάρτηση μέλος. Για να είναι μία συνάρτηση φιλική (//friend//) θα πρέπει να την δηλώσετε ως τέτοια στη δήλωση της κλάσης. Δείτε το παράδειγμα της συνάρτησης //modifyDimensions// η οποία είναι φίλια συνάρτηση της κλάσης //Rectangle//. Παρατηρήστε ότι η συνάρτηση έχει απευθείας πρόσβαση στα //private// πεδία της κλάσης //Rectangle//.
  
 <code cpp Rectangle.cpp> <code cpp Rectangle.cpp>
Line 16: Line 16:
     int getWidth();     int getWidth();
     int getHeight();     int getHeight();
-    friend void modifyDimensions(int dw, int dh);+    friend void modifyDimensions(Rectangle r, int dw, int dh);
 }; };
  
Line 44: Line 44:
   return 0;   return 0;
 } }
 +</code>
 +
 +====== Φιλικές Κλάσεις ======
 +
 +Σε αναλογία με τις φιλικές συναρτήσεις μπορείτε να έχετε και φιλικές κλάσεις που έχουν πρόσβαση στο σύνολο των πεδίων κλάσης. Στο παρακάτω παράδειγμα, η κλάση //Cuboid// είναι φιλική της κλάσης //Rectangle//. Ως φιλική κλάση έχει πρόσβαση στο σύνολο των πεδίων και των μεθόδων της κλάσης //Rectangle//
 +
 +<code cpp Rectangle.cpp>
 +#include <iostream>
 +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::Rectangle(int w, int h) {
 +  width = w; height = h;
 +}
 +
 +void Rectangle::setWidth(int w) { width = w; }
 +void Rectangle::setHeight(int h) { height = h; }
 +int Rectangle::getWidth() { return width; }
 +int Rectangle::getHeight() { return height; }
 +</code>
 +
 +<code cpp Cuboid.cpp>
 +#include <iostream>
 +using namespace std;
 +
 +#include "Rectangle.cpp"
 +
 +class Cuboid {
 +  Rectangle rect;
 +  int length;
 +public:
 +  Cuboid(Rectangle r, int l);
 +  int volume();
 +};
 +
 +Cuboid::Cuboid(Rectangle r, int l) {
 +  rect = r; length = l;
 +}
 +
 +int Cuboid::volume() {
 +  return rect.width * rect.height * length;
 +}
 +</code>
 +
 +<code cpp CuboidVolume.cpp>
 +#include <iostream>
 +using namespace std;
 +
 +#include "Cuboid.cpp"
 +
 +int main() {
 +  Rectangle rectangle(5,6);
 +  Cuboid cuboid(rectangle, 10);
 +  cout << "volume: " << cuboid.volume() << endl;
 +}
 +</code>
  
 +<WRAP center round tip 80%>
 +Παρατηρήστε πως η μέθοδος //volume// της κλάσης //Cuboid// υπολογίζει τον όγκο έχοντας απευθείας πρόσβαση στα πεδία της κλάσης //Rectangle//.
 +</WRAP>
cpp/friend_methods.txt · Last modified: 2021/05/18 06:21 (external edit)