/*#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>*/
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
template <typename T>
struct point
{
T x,y;
point(T x=0,T y=0):x(x),y(y){} //point(T x=0,T y=0){ this->x=x;this->y=y;}
}; //初始化变量x,y的值与参数相同;this是指向当前对象的指针
template <typename T>
point<T> operator+(const point<T>& A,const point<T> B) //给结构体定义加法;
{
return point<T>(A.x+B.x,A.y+B.y);
}
template <typename T>
ostream& operator<<(ostream &out,const point<T>& p) //定义结构体流输出
{
out<<"("<<p.x<<","<<p.y<<")";
return out;
}
int main(void)
{
point<int> a(1,2),b(3,4);
point<double> c(1.1,2.2),d(3.3,4.4);
cout<<a+b<<" "<<c+d<<"\n";
return 0;
}
c++结构体加减运算
最新推荐文章于 2025-06-07 21:23:03 发布