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
Next revisionBoth sides next revision
cpp:friend_methods [2017/04/20 15:58] – [Φιλικές Κλάσεις] gthanoscpp:friend_methods [2017/04/20 16:11] – [Φιλικές Κλάσεις] gthanos
Line 48: Line 48:
 ====== Φιλικές Κλάσεις ====== ====== Φιλικές Κλάσεις ======
  
-Σε αναλογία με τις φιλικές συναρτήσεις μπορείτε να έχετε και φιλικές κλάσεις που έχουν πρόσβαση στο σύνολο των πεδίων κλάσης. Στο παρακάτω παράδειγμα, η κλάση //Cuboid// είναι φιλική της κλάσης //Rectangle//. Ως φιλική κλάση έχει πρόσβαση στο σύνολο των πεδίων και των μεθόδων της κλάσης //Rectangle// +Σε αναλογία με τις φιλικές συναρτήσεις μπορείτε να έχετε και φιλικές κλάσεις που έχουν πρόσβαση στο σύνολο των πεδίων κλάσης. Στο παρακάτω παράδειγμα, η κλάση //Cuboid// είναι φιλική της κλάσης //Rectangle//. Ως φιλική κλάση έχει πρόσβαση στο σύνολο των πεδίων και των μεθόδων της κλάσης //Rectangle//
  
 <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(Rectangle r, int l) { Cuboid::Cuboid(Rectangle r, int l) {
Line 96: Line 96:
  
 int Cuboid::volume() { int Cuboid::volume() {
-  return r.width * r.height * length;+  return rect.width * rect.height * length;
 } }
 </code> </code>
Line 102: Line 102:
 <code cpp CuboidVolume.cpp> <code cpp CuboidVolume.cpp>
 #include <iostream> #include <iostream>
 +using namespace std;
 +
 +#include "Cuboid.cpp"
  
 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.txt · Last modified: 2021/05/18 06:21 (external edit)