#include<iostream>
using namespace std;
//所有函数都写在类的内部
template <typename T>
class Complex
{
friend ostream& operator<<(ostream& out, Complex& c3)
{
out << c3.a << c3.b << endl;
return out;
}
public:
Complex(T a, T b )
{
this->a = a;
this->b = b;
}
Complex operator +(Complex& c2)
{
Complex tmp(a + c2.a, b + c2.b);
return tmp;
}
void pritCom()
{
cout << "a" << a << "b" << b << endl;
}
private:
T a;
T b;
};
//ostream& operator<<(ostream& out, Complex& c3)
//{
// out << c3.a << c3.b << endl;
// return out;
//}
void main()
{
//需要把模板类具体化才能定义对象 c++编译器分配内存
Complex <int> c1(1, 2);
Complex <int> c2(3, 4);
Complex <int> c3 = c1 + c2;//进行函数重载
// Complex operator +(Complex & c2);
cout << c3 << endl;//重载座椅运算符
// ostream& operator<<(ostream & out, Complex & c3);
return;
}
写在类的内部