Tranvick

Example7

Jan 8th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. //Пример 7. Классы:
  2. //константные поля и методы; использование this; указатели для полей и методов
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. class MyClass {
  9. private:
  10.     const static int field1 = 10;
  11. public:
  12.     int field2;
  13.     static void print() {
  14.         cout << "Abracadabra\n";
  15.     }
  16.     void print1() const;
  17.     void print2() const;
  18.     void setf(int);
  19.     const int * getptr() const {
  20.         return &field1;
  21.     }
  22. };
  23.  
  24. void MyClass :: print1() const {
  25.     cout << field1 << endl;
  26. }
  27.  
  28. void MyClass :: print2() const {
  29.     cout << this -> field2 << endl;
  30. }
  31.  
  32. void MyClass :: setf(int x) {
  33.     field2 = x;
  34. }
  35.  
  36. int main() {
  37.     MyClass g;
  38.     void (* ptr) () = MyClass :: print;
  39.     ptr();
  40.     void (MyClass :: *pt) () const = & MyClass :: print1;  
  41.     g.print1();
  42.     g.setf(100);
  43.     g.print2();
  44.     const MyClass t;
  45.     t.print1();
  46. //  t.setf(100);
  47.     cout << *t.getptr();
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment