重定向文件读入写入
#include<iostream>
using namespace std;
int main(){
freopen("input.txt","r",stdin);
freopen("out.txt","w",stdout);
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;
}