User Tools

Site Tools


cpp:const_member_functions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
cpp:const_member_functions [2017/05/15 06:42]
gthanos created
cpp:const_member_functions [2021/05/07 07:50]
Line 1: Line 1:
-====== Const μέθοδοι της κλάσης ====== 
- 
-Όταν δηλώνεται ένα αντικείμενο ως //const// (όπως παρακάτω), τότε οι μεταβλητές του αντικειμένου μπορούν μόνο να διαβαστούν αλλά όχι να μεταβληθούν, πρόκειται δηλαδή για ένα αμετάβλητο αντικείμενο. Παρατηρήστε βέβαια, ότι ο κατασκευαστής του αντικειμένου αρχικοποιεί κανονικά το αντικείμενο και μεταβάλλει τις τιμές του. 
- 
-<code cpp Rectangle.cpp> 
-#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);   
-} 
-</code> 
- 
- 
- 
  
cpp/const_member_functions.txt · Last modified: 2021/05/07 07:50 (external edit)