例如对于类A;
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
class A{
public:
A(int a, int b, int c):x(a),y(b),z(c){}
int x;
int y;
int z;
};
void main()
{
A a(1,2,3);
string file;
cout<<"please input the file to be wrote:\n";
getline(cin,file);
ofstream out(file.c_str(),ios::out);
//output to screen:
cout<<a.x<<a.y<<a.z<<endl;
//output to file:
out<<a.x<<a.y<<a.z;
out.close();
return;
}