具体程序如下:
#include<iostream>
using namespace std;
class Base
{
public:
Base(int a,int b)
{
x=a;
y=b;
}
void add(const Base& rh)
{
x+=rh.x;//访问私有成员变量
y+=rh.y;
}
void print()
{
cout<<"x = "<<x<<endl;
cout<<"y = "<<y<<endl;
}
private:
int x;
int y;
};
int main()
{
Base b1(1,2);
Base b2(3,4);
b1.add(b2);
b1.print();
return 0;
}