This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
cpp:acccess_modifiers [2017/04/20 14:11] gthanos |
cpp:acccess_modifiers [2021/05/18 06:37] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Προσδιοριστές Πρόσβασης ====== | ||
| - | |||
| - | Οι προσδιοριστές πρόσβασης επιτρέπουν την απόκρυψη της πληροφορίας που είναι από τις βασικές αρχές του αντικειμενοστραφή προγραμματισμού. Με την έννοια //" | ||
| - | |||
| - | Σε μία κλάση μπορούν να οριστούν οι προσδιοριστές πρόσβασης //public//, // | ||
| - | - **public:** Ένα μέλος της κλάσης που δηλώνεται ως //public// είναι προσβάσιμο από οποιαδήποτε άλλη κλάση ή συνάρτηση που δεν ανήκει σε κλάση. | ||
| - | - **protected: | ||
| - | - **private: | ||
| - | |||
| - | Δείτε το παρακάτω παράδειγμα χρήσης των προσδιοριστών πρόσβασης μέσω των κλάσεων //Cuboid// (κυβοειδές) και //Cube// (κύβος) που κληρονομεί την κλάση //Cuboid//. | ||
| - | |||
| - | <code cpp Cuboid.cpp> | ||
| - | #include < | ||
| - | using namespace std; | ||
| - | |||
| - | class Cuboid { | ||
| - | private: | ||
| - | int width, height, length; | ||
| - | protected: | ||
| - | int color; | ||
| - | public: | ||
| - | Cuboid(int w, int h, int l); | ||
| - | void setWidth(int w); | ||
| - | void setHeight(int h); | ||
| - | void setLength(int l); | ||
| - | int getWidth(); | ||
| - | int getHeight(); | ||
| - | int getLength(); | ||
| - | }; | ||
| - | |||
| - | Cuboid:: | ||
| - | width = w; height = h; length = l; | ||
| - | color = 0xffffff; | ||
| - | } | ||
| - | |||
| - | void Cuboid:: | ||
| - | void Cuboid:: | ||
| - | void Cuboid:: | ||
| - | int Cuboid:: | ||
| - | int Cuboid:: | ||
| - | int Cuboid:: | ||
| - | </ | ||
| - | |||
| - | <code cpp Cube.cpp> | ||
| - | #include < | ||
| - | using namespace std; | ||
| - | #include " | ||
| - | |||
| - | class Cube:Cuboid { // Cube extends Cuboid | ||
| - | public: | ||
| - | Cube(int s, int updatecolor); | ||
| - | }; | ||
| - | |||
| - | Cube:: | ||
| - | color = updatecolor; | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | |||
| - | <code cpp CubeUsage.cpp> | ||
| - | include " | ||
| - | |||
| - | int main() { | ||
| - | Cuboid cuboid(1, | ||
| - | Cube cube(1, 0xcccccc); | ||
| - | } | ||
| - | </ | ||