重定向文件读入写入

#include<iostream>
using namespace std;

int main(){
	freopen("input.txt","r",stdin);//打开input文件,表明为读入,所有scanf的内容都是从这里读的 
	freopen("out.txt","w",stdout);//打开out文件,表明为写入,所有printf之类的内容都写在里面了 
	char str[200];
	scanf("%s",str); 
	printf("%s\n",str);
	printf("__hello!");
	fclose(stdin); //关闭读入 
	fclose(stdout); //关闭写入 
	return 0;
}

函数模板

#include<iostream>
using namespace std;
template <class T>
T add(T x, T y){
	return x+y;
}
int main(){
	double a,b;
	int c,d;
	cin>>a>>b;
	cout<<add(a,b)<<endl;
	cin>>c>>d;
	cout<<add(c,d)<<endl;
	
	return 0;
} 

重载为成员函数

#include<iostream>
using namespace std;
class Point{
	int x,y;
	public:
		Point(){}
		Point(int i, int j){
			x = i;
			y = j;
		}
		void disp(){
			cout<<"("<<x<<","<<y<<")"<<endl;
		}
		Point operator + (Point &p){
			return Point(x+p.x, y+p.y);
		}
}; 
int main(){
	Point p1(3,4),p2(5,6),p3;
	p3=p1+p2;
	p3.disp();
	return 0;
}

重载为友元函数

#include<iostream>
using namespace std;
class Point{
	int x,y;
	public:
		Point(){}
		Point(int i, int j){
			x = i;
			y = j;
		}
		void disp(){
			cout<<"("<<x<<","<<y<<")"<<endl;
		}
		friend Point operator + (Point &p1, Point &p2){
			return Point(p1.x+p2.x, p1.y+p2.y);
		}
}; 
int main(){
	Point p1(3,4),p2(5,6),p3;
	p3=p1+p2;
	p3.disp();
	return 0;
}