我无法弄清楚有关“ void change(Square&obj){obj.side + = y;}”的部分,如何在不给其诸如整数或其他类型的情况下定义“ obj”。另外,Square&obj对于此代码有什么作用?谢谢。
#include<iostream>
using namespace std;
class Increment;
class Square
{
private:
int side;
public:
Square()
{
side=2;
}
void print()
{
cout << "side of square is: " << side<<endl;
}
friend class Increment;
};
class Increment
{
private:
int y;
public:
Increment()
{
cout<<"enter increment value: ";
cin>>y;
}
void change(Square& obj){obj.side+=y;}
};
int main()
{
Square s;
s.print();
Increment t;
t.change(s);
s.print();
}