1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
class Person
{
public:
Person(); // set birth_day, birth_month and birth_year to 1
// constructor to values to the parameter’s value
Person(string fn, string ln, int m, int d, int y);
string get_name() const; // return the person’s first_name and last_name
int get_birth_month() const; // return person’s birth_month
int get_birth_day() const; // return person’s birth_day
void set_name(string fn, string ln);
void set_birthday_day(int m, int d, int y);
void print() const; // print out all the person’s info
private:
string first_name;
string last_name;
int birth_day;
int birth_month;
int birth_year;
};
int main( )
{
string fname, lname;
int month, day, year;
string text;
vector<Person> friends;
ifstream myfile ("presidents.txt");
if (myfile.is_open())
{
while ( getline (myfile, text) )
{
istringstream ss (text);
ss >> fname >> lname >> month >> day >> year;
Person myFriend = Person(fname, lname, month, day, year);
friends.push_back(myFriend);
}
myfile.close();
}
else cout << "Unable to open file";
for (int i = 0; i < friends.size(); i++) {
friends[i].print();
}
return 0;
}
void set_name(fname, lname) {
first_name = fname;
last_name = lname;
};
void set_birthday_day(fmo, fday, fyear) {
birth_month = fmo;
birth_day = fday;
birth_year = fyear;
};
string get_name() const {
return (first_name + last_name);
}
int get_birth_month() const {
return birth_month;
}
int get_birth_day() const {
return birth_day;
}
void print() const {
cout << fname << "\t" << lname<<"\t"<< month <<"\t"<< day << "\t" << year << endl;
}
|