Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- class Test
- {
- static int common;
- int x;
- int* ptr;
- static const int size{42};
- public:
- Test() : Test(0) { std::cout << "Default constructor for Test\n"; };
- Test(int pX, int pSize = 433) : x{ pX }, ptr{ new int[10] }{++common; std::cout << "Test constructed for " << this << '\n' << "This is #" << common << " instance of Test\n"; }
- ~Test() { delete[] ptr; std::cout << "Test destructed for " << this << '\n' << "This was #" << common-- << " instance of Test\n"; };
- void setX(int x) { this->x = x; };
- int getX() { return x; };
- int getSize() { return size; };
- bool isEqual(Test* const obj) { return (obj == this); };
- Test& add(int num) { x += num; return *this; };
- Test& sub(int num) { x -= num; return *this; };
- Test& mul(int num) { x *= num; return *this; };
- Test& div(int num) { x /= num; return *this; };
- static int getObjCount(){ return common; };
- static void sayName() { std::cout << "I'm a Test class!\n"; };
- };
- //int getXTest(Test* const obj) { return obj->x; };
- int Test::common{0};
- int cnt()
- {
- static int cntNum{ 55 };
- return ++cntNum;
- }
- //Test* const this; //1
- //const Test* this; //2
- int main()
- {
- //for (int i{ 0 }; i < 5; ++i)
- //{
- // std::cout << cnt() << '\n';
- //};
- //std::cout << Test::common << '\n';
- //Test::common = 888;
- //std::cout << Test::common << '\n';
- //std::cout << "There are " << Test::getObjCount() << " objects of Test class \n";
- //std::cout << "\n\n";
- Test anotherTest{ 555, 55 };
- //std::cout << "\n\n";
- //std::cout << "There are " << anotherTest.getObjCount() << " objects of Test class \n";
- Test newTest{ 555 };
- //std::cout << "\n\n";
- //std::cout << "There are " << myTest.getObjCount() << " objects of Test class \n";
- //std::cout << "We got mytest up and running!\n";
- //myTest.setX(42);
- //std::cout << myTest.getX() << '\n';
- //std::cout << myTest.getX() << '\n';
- //std::cout << anotherTest.getX() << '\n';
- //std::cout << myTest.isEqual(&myTest) << '\n'; //1
- //std::cout << myTest.isEqual(&anotherTest) << '\n'; //0
- //std::cout << myTest.isEqual(&newTest) << '\n'; //0
- //std::cout << anotherTest.isEqual(&anotherTest) << '\n'; //1
- //std::cout << anotherTest.isEqual(&myTest) << '\n'; //0
- //std::cout << anotherTest.isEqual(&newTest) << '\n'; //0
- //std::cout << myTest.getSize() << '\n';
- //std::cout << anotherTest.getSize() << '\n';
- Test myTest{ 555 };
- std::cout << myTest.getX() << '\n';
- myTest.sub(555).add(10).add(2).mul(2); // x == 24;
- //myTest.sub(555); // x == 0;
- myTest.add(10); // x == 10;
- //myTest.add(2); // x == 12;
- //myTest.mul(2); // x == 24;
- std::cout << myTest.getX() << '\n'; //24
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement