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 revisionPrevious revision
Next revision
Previous revision
cpp:friend_methods [2017/04/20 15:58] – [Φιλικές Κλάσεις] gthanoscpp:friend_methods [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 1: Line 1:
-====== Φιλικές Συναρτήσεις ======+====== Φιλικές συναρτήσεις και φιλικές κλάσεις  ======
  
-Μία φιλική συνάρτηση της κλάσης είναι μία συνάρτηση που δεν είναι μέλος της κλάσης, αλλά έχει πρόσβαση στα //private// και //protected// μέλη της κλάσης, όπως θα είχε και μία συνάρτηση μέλος. Για να είναι μία συνάρτηση φιλική (//friend//) θα πρέπει να την δηλώσετε ως τέτοια στη δήλωση της κλάσης. Δείτε το παράδειγμα της συνάρτησης //modifyDimensions// η οποία είναι φίλια συνάρτηση της κλάσης //Rectangle//. Παρατηρήστε ότι η συνάρτηση έχει απευθείας πρόσβαση στα //private// πεδία της κλάσης //Rectangle//.+===== Φιλικές Συναρτήσεις =====
  
-<code cpp Rectangle.cpp>+Μία φιλική συνάρτηση της κλάσης είναι μία συνάρτηση που δεν είναι μέλος της κλάσης, αλλά έχει πρόσβαση στα //private// και //protected// μέλη της κλάσης, όπως θα είχε και μία συνάρτηση μέλος. Για να είναι μία συνάρτηση φιλική (//friend//) θα πρέπει να την δηλώσετε ως τέτοια στη δήλωση της κλάσης. Δείτε το παράδειγμα της συνάρτησης //modifyDimensions// η οποία είναι φιλική συνάρτηση της κλάσης //Rectangle//. Παρατηρήστε ότι η συνάρτηση έχει απευθείας πρόσβαση στα //private// πεδία της κλάσης //Rectangle//
 + 
 +<code cpp Rectangle.hpp>
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
Line 16: Line 18:
     int getWidth();     int getWidth();
     int getHeight();     int getHeight();
-    friend void modifyDimensions(Rectangle r, int dw, int dh);+    friend void modifyDimensions(Rectangler, int dw, int dh);
 }; };
  
Line 28: Line 30:
 int Rectangle::getHeight() { return height; } int Rectangle::getHeight() { return height; }
  
-void modifyDimensions(Rectangle r, int dw, int dh) {+void modifyDimensions(Rectangler, 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 "Rectangle.cpp"+#include "Rectangle.hpp"
  
 int main () { int main () {
Line 46: Line 48:
 </code> </code>
  
-====== Φιλικές Κλάσεις ======+===== Φιλικές Κλάσεις =====
  
-Σε αναλογία με τις φιλικές συναρτήσεις μπορείτε να έχετε και φιλικές κλάσεις που έχουν πρόσβαση στο σύνολο των πεδίων κλάσης. Στο παρακάτω παράδειγμα, η κλάση //Cuboid// είναι φιλική της κλάσης //Rectangle//. Ως φιλική κλάση έχει πρόσβαση στο σύνολο των πεδίων και των μεθόδων της κλάσης //Rectangle//+Σε αναλογία με τις φιλικές συναρτήσεις μπορείτε να έχετε και φιλικές κλάσεις που έχουν πρόσβαση στο σύνολο των πεδίων κλάσης. Στο παρακάτω παράδειγμα, η κλάση //Cuboid// είναι φιλική της κλάσης //Rectangle//. Ως φιλική κλάση έχει πρόσβαση στο σύνολο των πεδίων και των μεθόδων της κλάσης //Rectangle//
  
- +<code cpp Rectangle.hpp>
-<code cpp Rectangle.cpp>+
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
Line 61: Line 62:
   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 79:
 </code> </code>
  
-<code cpp Cuboid.cpp>+<code cpp Cuboid.hpp>
 #include <iostream> #include <iostream>
 using namespace std; using namespace std;
  
-#include "Rectangle.cpp"+#include "Rectangle.hpp"
  
 class Cuboid { class Cuboid {
Line 89: Line 91:
   Cuboid(Rectangle r, int l);   Cuboid(Rectangle r, int l);
   int volume();   int volume();
-}+};
  
 Cuboid::Cuboid(Rectangle r, int l) { Cuboid::Cuboid(Rectangle r, int l) {
Line 96: Line 98:
  
 int Cuboid::volume() { int Cuboid::volume() {
-  return r.width * r.height * length;+  return rect.width * rect.height * length;
 } }
 </code> </code>
Line 102: Line 104:
 <code cpp CuboidVolume.cpp> <code cpp CuboidVolume.cpp>
 #include <iostream> #include <iostream>
 +using namespace std;
 +
 +#include "Cuboid.hpp"
  
 int main() { int main() {
   Rectangle rectangle(5,6);   Rectangle rectangle(5,6);
-  Cuboid cuboid(r, 10);+  Cuboid cuboid(rectangle, 10);
   cout << "volume: " << cuboid.volume() << endl;   cout << "volume: " << cuboid.volume() << endl;
 } }
 </code> </code>
 +
 +<WRAP center round tip 80%>
 +Παρατηρήστε πως η μέθοδος //volume// της κλάσης //Cuboid// υπολογίζει τον όγκο έχοντας απευθείας πρόσβαση στα πεδία της κλάσης //Rectangle//.
 +</WRAP>
cpp/friend_methods.1492703898.txt.gz · Last modified: 2017/04/20 14:58 (external edit)