Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Пример 7. Классы:
- //константные поля и методы; использование this; указатели для полей и методов
- #include <iostream>
- using namespace std;
- class MyClass {
- private:
- const static int field1 = 10;
- public:
- int field2;
- static void print() {
- cout << "Abracadabra\n";
- }
- void print1() const;
- void print2() const;
- void setf(int);
- const int * getptr() const {
- return &field1;
- }
- };
- void MyClass :: print1() const {
- cout << field1 << endl;
- }
- void MyClass :: print2() const {
- cout << this -> field2 << endl;
- }
- void MyClass :: setf(int x) {
- field2 = x;
- }
- int main() {
- MyClass g;
- void (* ptr) () = MyClass :: print;
- ptr();
- void (MyClass :: *pt) () const = & MyClass :: print1;
- g.print1();
- g.setf(100);
- g.print2();
- const MyClass t;
- t.print1();
- // t.setf(100);
- cout << *t.getptr();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment