
声明一个dog类,包含体重合年龄两个成员变量及相应的成员函数,声明一个实例dog1,体重为5,年龄为10
(需C++完整程序 简单点的能完成就行 谢谢各位了) 展开
#include <iostream.h>
#include <fstream.h>
class dog
{
public:
int weight;
int age;
dog()
{
}
dog(int weight1,int age1)
{
weight=weight1;
age=age1;
}
~dog()
{
}
};
int main()
{
dog dog1(5,10);
dog dog2;
ofstream fout;
ifstream fint;
fout.open("out.txt");
fout<<dog1.weight<<"\n"<<dog1.age<<endl;
fint.open("out.txt");
fint>>dog2.weight>>dog2.age;
cout<<dog2.weight<<dog2.age<<endl;
fout.close();
//二进制
ofstream fdata("file.dat",ios::binary);
fdata.write((char*)(&dog1),sizeof(dog1));
ifstream fint1("file.dat",ios::binary);
fint1.read((char*)(&dog2),sizeof(dog2));
cout<<dog2.age<<dog2.weight<<endl;
return 0;
}
当然生成的二进制文件用普通的文本编辑器是打不开的,可以用Ultraedit等高级工具打开。