LW6: Abstract Class BaseBookItem with virtual to_string() method.

This commit is contained in:
Kolan Sh 2021-03-28 23:42:20 +03:00
parent e48b696ebd
commit 85d4a64c9c
3 changed files with 12 additions and 7 deletions

View File

@ -323,3 +323,12 @@ std::istream& operator>>(std::istream& s, CBookItems::BookItem& item)
s >> item._sFirstName >> item._sLastName >> item._sPhoneNumber1 >> item._sPhoneNumber2; s >> item._sFirstName >> item._sLastName >> item._sPhoneNumber1 >> item._sPhoneNumber2;
return s; return s;
} }
const string CBookItems::BookItem::to_string() const
{
string str = _sFirstName + " " + _sLastName + ": " + _sPhoneNumber1;
if (_sPhoneNumber2 != "")
str += ", " + _sPhoneNumber2;
return str;
}

View File

@ -35,6 +35,7 @@ public:
virtual ~BaseBookItem() virtual ~BaseBookItem()
{ {
} }
virtual const string to_string() const = 0;
}; };
class BookItem : BaseBookItem<string> class BookItem : BaseBookItem<string>
@ -54,6 +55,7 @@ public:
friend std::istream& operator>>(std::istream& s, BookItem& item); friend std::istream& operator>>(std::istream& s, BookItem& item);
friend CBookItems; friend CBookItems;
const string to_string() const;
}; };
vector<BookItem> _aItems; vector<BookItem> _aItems;

View File

@ -86,13 +86,7 @@ void CConsoleUI::FindItem(CBookItems& CPhoneBook)
for (int i = 0; i < nNumOfDesirvedItems.size(); i++) for (int i = 0; i < nNumOfDesirvedItems.size(); i++)
{ {
string sFName = " "; cout << 1 + i << ". " << CPhoneBook.GetItem(nNumOfDesirvedItems.at(i)).to_string() << endl;
string sLName = " ";
string sNum1 = " ";
string sNum2 = " ";
CPhoneBook.GetItem(nNumOfDesirvedItems.at(i), sFName, sLName, sNum1, sNum2);
cout << 1 + i << ". " << sFName << " " << sLName << ": " << sNum1 << ", " << sNum2 << endl;
} }
} }