This is an old revision of the document!
Όταν δηλώνεται ένα αντικείμενο ως const (όπως παρακάτω), τότε οι μεταβλητές του αντικειμένου μπορούν μόνο να διαβαστούν αλλά όχι να μεταβληθούν, πρόκειται δηλαδή για ένα αμετάβλητο αντικείμενο. Παρατηρήστε βέβαια, ότι ο κατασκευαστής του αντικειμένου αρχικοποιεί κανονικά το αντικείμενο και μεταβάλλει τις τιμές του.
#include <iostream> using namespace std; class Rectangle { private: int width, height; public: Rectangle(int width, int height); void setWidth(int width); void setHeight(int height); int getWidth(); int getHeight(); }; Rectangle::Rectangle(int width, int height) { this->width = width; this->height = height; } void Rectangle::setWidth(int width) { this->width = width; } void Rectangle::setHeight(int height) { this->height = height; } int Rectangle::getWidth() { return width; } int Rectangle::getHeight() { return height; } int main() { const Rectangle rect(10,5); }