文件流读写:
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
int main()
{
fstream in,out;
int a[10],i,n=0;
in.open("1.txt",ios::in);
while(!in.eof())
in>>a[n++];
in.close();
sort(a,a+n);
out.open("ans.txt",ios::out);
for (i=0;i<n;i++)
out<<a[i]<<" ";
out.close();
return 0;
}
结构体读写:
#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
using namespace std;
struct P
{
string name;int val;
P(){name="";val=0;}
P(string s,int x){name=s;val=x;}
};
int main()
{
fstream in,out;
P A("zjw",8),B;
out.open("person.txt",ios::out);
out.write((char*)&A,sizeof(A));
out.close();
in.open("person.txt",ios::in);
in.read((char*)&B,sizeof(B));
cout<<B.name<<" "<<B.val<<endl;
return 0;
}
c++类的继承与函数重载:
#include<iostream>
#include<string>
using namespace std;
class Rec
{
public:
int w,d;
Rec(){};
Rec(int a,int b){ w=a;d=b;}
Rec(Rec &A) { w=A.w;d=A.d;}
bool operator == (const Rec &A)
{ return (w==A.w && d==A.d); }
};
class RRec: public Rec
{
public:
int s;
RRec(int a,int b):Rec(a,b){}
void square(){ s=w*d; }
bool operator < (const RRec &A)const
{ return s<A.s; }
};
int main()
{
RRec a(3,4),c(2,5);
RRec b(a);
a.square();c.square();
cout<<(a==b)<<endl;
cout<<(c<a)<<endl;
}