60 370
Assignments Done
98,1%
Successfully Done
In April 2018

Answer to Question #42045 in C++ for chan

Question #42045
I have to read contents from a data file to members of classes. What is the code to open and read data into the class members?
Expert's answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Person {
public:
Person() :
name(""),
surname(""),
age(0),
height(0),
weight(0),
phone("") {}
friend istream &operator>> (istream &in, Person &p);
friend ostream &operator<< ( ostream &out, Person &p );
protected:
string name;
string surname;
int age;
int height;
int weight;
string phone;
};
istream &operator>> (istream &in, Person &p) {
in >> p.name >> p.surname >> p.age >> p.height >> p.weight >> p.phone;
return in;
}
ostream &operator<< ( ostream &out, Person &p ) {
return out << p.name << "\n"<< p.surname << "\n"<< p.age << "\n"<< p.height << "\n"<< p.weight<< "\n"<< p.phone;
}
int main() {
Person p;
ifstream in("person.txt");
in >> p;
cout << p << endl;
in.close();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions