cpp:classes
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:classes [2017/04/12 08:14] – gthanos | cpp:classes [2020/04/08 07:35] – gthanos | ||
---|---|---|---|
Line 13: | Line 13: | ||
void setWidth(int w); | void setWidth(int w); | ||
void setHeight(int h); | void setHeight(int h); | ||
- | int getWidth(); | + | int getWidth() |
- | int getHeight(); | + | int getHeight() |
}; | }; | ||
void Rectangle:: | void Rectangle:: | ||
void Rectangle:: | void Rectangle:: | ||
- | int Rectangle:: | + | int Rectangle:: |
- | int Rectangle:: | + | int Rectangle:: |
int main () { | int main () { | ||
Line 31: | Line 31: | ||
</ | </ | ||
- | Η παραπάνω κλάση διαθέτει τα πεδία τύπου //int width, height// και τις μεθόδους //setWidth, setHeight, getWidth, getHeight// | + | Η παραπάνω κλάση διαθέτει τα πεδία τύπου //int width, height// και τις μεθόδους //setWidth, setHeight, getWidth, getHeight// |
===== Οι μέθοδοι της κλάσης ===== | ===== Οι μέθοδοι της κλάσης ===== | ||
- | A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. It operates on any object of the class of which it is a member, and has access to all the members of a class for that object. | + | Οι μέθοδοι της κλάσης είναι συναρτήσεις οι οποίες έχουν απευθείας πρόσβαση στα πεδία (μεταβλητές) της κλάσης. Οι μέθοδοι ορίζονται μέσα στην κλάση ή ορίζεται το πρότυπο τους μέσα στην κλάση και η υλοποίηση τους εκτός. Από το παρακάτω παράδειγμα παρατηρήστε τους δύο διαφορετικούς τρόπους ορισμού των μεθόδων // |
- | + | ||
- | Οι μέθοδοι της κλάσης είναι συναρτήσεις οι οποίες έχουν απευθείας πρόσβαση στα πεδία (μεταβλητές) της κλάσης. Οι μέθοδοι ορίζονται μέσα στην κλάση ή ορίζεται το πρότυπο τους μέσα στην κλάση και η υλοποίηση τους εκτός. Από το παραπάνω παράδειγμα παρατηρήστε τους δύο διαφορετικούς τρόπους ορισμού των μεθόδων // | + | |
<code c++ Rectangle.cpp> | <code c++ Rectangle.cpp> | ||
Line 47: | Line 45: | ||
int width, height; | int width, height; | ||
public: | public: | ||
+ | int getWidth() const; | ||
+ | int getHeight() const; | ||
void setWidth(int w); | void setWidth(int w); | ||
void setHeight(int h) { height = h; } | void setHeight(int h) { height = h; } | ||
}; | }; | ||
+ | int Rectangle:: | ||
+ | int Rectangle:: | ||
void Rectangle:: | void Rectangle:: | ||
</ | </ | ||
Line 56: | Line 58: | ||
Οι δύο παραπάνω τρόποι ορισμού μίας μεθόδου της κλάσης είναι ισοδύναμοι με την διαφορά ότι η μέθοδος // | Οι δύο παραπάνω τρόποι ορισμού μίας μεθόδου της κλάσης είναι ισοδύναμοι με την διαφορά ότι η μέθοδος // | ||
+ | <WRAP center round important 80%> | ||
+ | Οι μέθοδοι // | ||
+ | </ | ||
cpp/classes.txt · Last modified: 2021/05/06 23:01 (external edit)