. 1 #include <the iostream> 2 #include < String > . 3 the using namespace STD; . 4 / * . 5 // member variables privatization advantages: 6 read and write access control members 1. 7 2. Test validity reader . 8 . 9 * / 10 class Point . 11 { 12 is public : // control read and write access member 13 is void setX ( int X) 14 { 15 m_x = X; 16 } . 17 int getX () 18 is { . 19 return m_x; 20 is } 21 is void setY ( int Y) 22 is { 23 is IF (Y < . 5 ) // detect validity reader 24 { 25 COUT << " the Y value of less than. 5 " << endl; 26 is m_Y = 0 ; 27 return ; 28 } 29 m_Y = Y; 30 } 31 is int getY () 32 { 33 is return m_Y; 34 is } 35 Private : // member variables privatization 36 int m_x; 37 [ int m_Y; 38 is }; 39 40 int main () 41 is { 42 is Point Pt; 43 is int X, Y; 44 is COUT << " Enter X: \ n- " ; 45 CIN >> X; 46 is COUT << " enter y (y is greater than 5, is less than 5 is set to 0): \ n- " ; 47 CIN >> Y; 48 49 pt.setX(x); 50 pt.setY(y); 51 52 cout << "X值:" << pt.getX() << endl; 53 cout << "Y值:" << pt.getY() << endl; 54 55 system("pause"); 56 return 0; 57 }
Class member variables privatization