I saw this code on internet today:
#include<iostream>
using namespace std;
class Test
{
private:
int x;
int y;
public:
Test (int x = 0, int y = 0) { this->x = x; this->y = y; }
Test setX(int a) { x = a; return *this; }
Test setY(int b) { y = b; return *this; }
void print() { cout << "x = " << x << " y = " << y << endl; }
};
int main()
{
Test obj1;
obj1.setX(10).setY(20);
obj1.print();
return 0;
}
Here see these lines:
Test setX(int a) { x = a; return *this; }
Test setY(int b) { y = b; return *this; }
Are setX and setY constructors? If not, what are they? Any help will be appreciated.