User Tools

Site Tools


cpp:friend_methods

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
cpp:friend_methods [2017/04/20 15:51]
gthanos
cpp:friend_methods [2021/05/18 06:21]
Line 1: Line 1:
-====== Φιλικές Συναρτήσεις ====== 
  
-Μία φιλική συνάρτηση της κλάσης είναι μία συνάρτηση που δεν είναι μέλος της κλάσης, αλλά έχει πρόσβαση στα //private// και //protected// μέλη της κλάσης, όπως θα είχε και μία συνάρτηση μέλος. Για να είναι μία συνάρτηση φιλική (//friend//) θα πρέπει να την δηλώσετε ως τέτοια στη δήλωση της κλάσης. Δείτε το παράδειγμα της συνάρτησης //modifyDimensions// η οποία είναι φίλια συνάρτηση της κλάσης //Rectangle//. Παρατηρήστε ότι η συνάρτηση έχει απευθείας πρόσβαση στα //private// πεδία της κλάσης //Rectangle//. 
- 
-<code cpp Rectangle.cpp> 
-#include <iostream> 
-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 r, int dw, int dh); 
-}; 
- 
-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; } 
- 
-void modifyDimensions(Rectangle r, int dw, int dh) { 
-  r.width += dw; 
-  r.height += dh; 
-} 
-</code> 
- 
-<code cpp RectangleUsage.cpp> 
-#include "Rectangle.cpp" 
- 
-int main () { 
-  Rectangle rect(5,6);   
-  cout << "area: " << rect.getWidth() * rect.getHeight() << endl; 
-  modifyDimensions(rect, 1, 1); 
-  cout << "area: " << rect.getWidth() * rect.getHeight() << endl; 
-  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); 
-    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 r; 
-  int length; 
-public: 
-  void modifyDimensions(int dw, int dh, int dl) { 
-    r.width += dw; 
-    r.height += dh; 
-    length += dl; 
-  } 
-} 
-<code> 
cpp/friend_methods.txt · Last modified: 2021/05/18 06:21 (external edit)