kapko

class_encapsulation

May 11th, 2026
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. // Klasa o nazwie ClassA (jest to nowy typ dostępny dla funkcji).
  7. class ClassA
  8. {
  9. public:
  10.     // Konstruktor domyślny obiektu ClassA.
  11.     ClassA(int prv = 0) {
  12.         mPrivate = prv;
  13.     }
  14.  
  15.     void print(void) const {
  16.         cout << "mPrivate = " << mPrivate << endl;
  17.     }
  18.    
  19.     void setPrivate(int prv) {
  20.         mPrivate = prv;
  21.     }
  22.    
  23.     int getPrivate(void) const {
  24.         return mPrivate;
  25.     }
  26.  
  27. private:
  28.     // Składowa prywatna.
  29.     int mPrivate;
  30.  
  31. };
  32.  
  33. int main()
  34. {
  35.     ClassA a(10);
  36.  
  37.     a.print();
  38.     a.setPrivate(12);
  39.     a.print();
  40.     a.setPrivate(14);
  41.    
  42.     cout << "a.getPrivate() = " << a.getPrivate() << endl;
  43.  
  44.     return 0;
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment