Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <vector>
- class Person {
- protected:
- std::string m_Name, m_Surname;
- public:
- Person() {
- }
- Person(std::string &s, std::string &f) :
- m_Name(f), m_Surname(s) {
- }
- void set_name(std::string &f) {
- m_Name = f;
- }
- void set_surname(std::string &s) {
- m_Surname = s;
- }
- const std::string& get_name() const {
- return m_Name;
- }
- const std::string& get_surname() const {
- return m_Surname;
- }
- virtual void print(std::ostream &o) const {
- o << "<person" << " S " << m_Surname << " N " << m_Name << " >";
- }
- virtual ~Person() {}
- };
- class Person_with_telephone: public virtual Person {
- protected:
- std::string m_Telephone;
- protected:
- Person_with_telephone(const std::string &telephone) :
- m_Telephone(telephone) {
- }
- public:
- Person_with_telephone() {
- }
- Person_with_telephone(std::string &s, std::string &f, std::string &telephone) :
- Person(s, f), m_Telephone(telephone) {
- }
- void set_telephone(std::string &telephone) {
- m_Telephone = telephone;
- }
- const std::string& get_telephone() const {
- return m_Telephone;
- }
- virtual void print(std::ostream &o) const override {
- o << "<person" << " S " << m_Surname << " N " << m_Name
- << " T " << m_Telephone << " >";
- }
- };
- class Person_with_email: public virtual Person {
- protected:
- std::string m_Email;
- protected:
- Person_with_email(const std::string &email) :
- m_Email(email) {
- }
- public:
- Person_with_email() {
- }
- Person_with_email(std::string &s, std::string &f, std::string &email) :
- Person(s, f), m_Email(email) {
- }
- void set_email(std::string &email) {
- m_Email = email;
- }
- const std::string& get_email() const {
- return m_Email;
- }
- virtual void print(std::ostream &o) const override {
- o << "<person" << " S " << m_Surname << " N " << m_Name << " E " << m_Email << " >";
- }
- };
- class Person_with_telephone_and_email: public virtual Person_with_telephone,
- public virtual Person_with_email {
- public:
- Person_with_telephone_and_email() {
- }
- Person_with_telephone_and_email(std::string &s, std::string &f, std::string &telephone,
- std::string &email) :
- Person(s, f), Person_with_telephone(telephone), Person_with_email(email) {
- }
- virtual void print(std::ostream &o) const override {
- o << "<person" << " S " << m_Surname << " N " << m_Name
- << " T " << m_Telephone << " E " << m_Email << " >";
- }
- };
- Person* read_person(std::istream &in) {
- std::string tmp, fname, surname, email, telephone;
- in >> tmp;
- if (tmp == "<person") {
- in >> tmp;
- while (tmp != ">") {
- if (tmp == "S") {
- in >> surname;
- } else if (tmp == "N") {
- in >> fname;
- } else if (tmp == "T") {
- in >> telephone;
- } else if (tmp == "E") {
- in >> email;
- } else {
- std::cerr << "Unknown token: " << tmp << "\n";
- return nullptr;
- }
- in >> tmp;
- }
- } else {
- std::cerr << "Bad starting sequence: " << tmp << std::endl;
- return nullptr;
- }
- if (email != "" && telephone != "") {
- return new Person_with_telephone_and_email(surname, fname, telephone, email);
- } else if (email != "") {
- return new Person_with_email(surname, fname, email);
- } else if (telephone != "") {
- return new Person_with_telephone(surname, fname, telephone);
- }
- return new Person(surname, fname);
- }
- int main() {
- std::stringstream ss("<person S Smith N Tom >\n<person S Smith N Dick T +49.921.1434 >\n<person S Smith N Harry E hsmith@gmail.com >\n<person S Smith N Mary T +39.921.1434 E msmith@gmail.com >\n<person S John N John T +33.921.1434 E jjohn@gmail.com >");
- std::vector<Person*> people;
- std::string tmp;
- std::stringstream stream;
- while(std::getline(ss, tmp)) {
- stream.clear();
- stream.str(tmp);
- people.push_back(read_person(stream));
- }
- for (auto &p : people) {
- p->print(std::cout);
- std::cout << std::endl;
- }
- for (auto &p : people) {
- delete p;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement