实验五
#ifndef PERSON_HPP #define PERSON_HPP #include <iostream> #include <string> using namespace std; class Person { public: Person(){}; Person(string name, string telephone, string email = "123@163.com") : name(name), telephone(telephone), email(email) { } friend ostream& operator<<(ostream &cout ,const Person &p); friend istream& operator>>(istream &cin , Person &p); friend bool operator==(const Person &p1 , const Person &p2); private: string name; string telephone; string email; }; ostream& operator<<(ostream &cout ,const Person &p) { cout << p.name << " " << p.telephone << " " << p.email; return cout; } istream& operator>>(istream &cin , Person &p) { cin >> p.name >> p.telephone >> p.email; return cin; } bool operator==(const Person &p1 , const Person &p2) { if(p1.name == p2.name && p1.telephone == p2.telephone&& p1.email == p2.email ) { return 1; } else return 0; } #endif
#include <iostream> #include <fstream> #include <vector> #include "Person.hpp" int main() { using namespace std; vector<Person> phone_book; Person p; while(cin>>p) phone_book.push_back(p); for(auto &i: phone_book) cout << i << endl; cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl; ofstream fout; fout.open("phone_book.txt"); if(!fout.is_open()) { cerr << "fail to open file phone_book.txt\n"; return 1; } for(auto &i: phone_book) fout << i << endl; fout.close(); system("pause"); }
实验三改完编译不出来,发修改截图了