This is an old revision of the document!
Οι προσδιοριστές πρόσβασης επιτρέπουν την απόκρυψη της πληροφορίας που είναι από τις βασικές αρχές του αντικειμενοστραφή προγραμματισμού. Με την έννοια “απόκρυψη πληροφορίας” εννοούμε την απόκρυψη της εσωτερικής υλοποίησης της κλάσης.
Σε μία κλάση μπορούν να οριστούν οι προσδιοριστές πρόσβασης public, protected και private. Οι προσδιοριστές πρόσβασης αφορούν τόσο τα πεδία (μεταβλητές) όσο και τις συναρτήσεις (μεθόδους) της κλάσης. Οι λειτουργικότητα τους συνοψίζεται στα εξής:
Δείτε το παρακάτω παράδειγμα χρήσης των προσδιοριστών πρόσβασης μέσω των κλάσεων Cuboid (κυβοειδές) και Cube (κύβος) που κληρονομεί την κλάση Cuboid.
#include <iostream> 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::Cuboid(int w, int h, int l) { width = w; height = h; length = l; color = 0xffffff; } void Cuboid::setWidth(int w) { width = w; } void Cuboid::setHeight(int h) { height = h; } void Cuboid::setLength(int l) { length = l; } int Cuboid::getWidth() { return width; } int Cuboid::getHeight() { return height; } int Cuboid::getLength() { return length; }
#include <iostream> using namespace std; #include "Cuboid.cpp" class Cube:Cuboid { // Cube extends Cuboid public: Cube(int s, int updatecolor); }; Cube::Cube(int s, int updatecolor) : Cuboid(s,s,s) { color = updatecolor; }
include "Cube.cpp" int main() { Cuboid cuboid(1,2,3); Cube cube(1, 0xcccccc); }