#include<iostream>
using namespace std;
class test
{
public:
int a;
int b;
public:
test(int a = 0, int b = 0)
{
this->a = a;
this->b = b;
cout << "a=" << a << ",b=" << b ;
cout << "I am test()"<< endl;
}
~test()
{
cout << "a=" << a << ",b=" << b;
cout << "I am ~test()"<< endl;
}
//成员函数 返回的是一个元素
test Add(test &obj)
{
test t(this->a + obj.a, this->b + obj.b);
return t;
}
//返回的是一个引用 并且用*this返回
//返回一个引用 相当于返回自身
//返回t1这个元素 this就是t1
test& Add1(test& obj1)
{
this->a = this->a + obj1.a;
this->b = this->b + obj1.b;
return *this;//把*(&t1)又回到了t1自身
}
void printfA()
{
cout << "a="<<a<<",b="<<b<< endl;
}
};
//成员函数转成全局函数 多了一个参数
void printfB(test *pt)
{
cout << "a:"<<pt->a<<"b:"<<pt->b<< endl;
}
//全局函数
//全局函数转成成员函数 少了一个参数
test Addt(test &t1, test& t2)
{
test t;
return t;
}
void main()
{
test t1(1, 2);
test t2(3, 4);
test t3 = Addt(t1, t2);//调用全局函数
printfB(&t3);
test t4 = t1.Add(t2);
test t5;
t5 = t1.Add(t2);
t4.printfA();
t1.Add1(t2);
t1.printfA();
system("pause");
}