i have a class, for the purpose of this question it is a simple class called person which stores their age and height (in cm). Now say i wanted to write the objects of this class to a file, the name of the object would be the persons name, and the class contents would obviously be the relevant persons age and height. How would i go about writing all this information to a file (including the persons name), for recall without defining the persons name as a string within the class?
#include <iostream>
#include <fstream>
using namespace std;
class person
{
public:
int age;
int height;
}
int main() {
person bob;
ofstream myfile;
myfile.open("file.txt", ios::app);
bob.age = 18;
bob.height = 180;
myfile << /*write the object name somehow*/"" << bob.age << " " << bob.height << "\n";
myfile.close();
return 0;
}
What RedEyedKiller says works but it is just overcomplicated to be useful. It is so much easier to write "bob" than write NAME_OF(bob) when both do the exact same thing.