Browse Source

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

master LW6
Kolan Sh 4 months ago
parent
commit
85d4a64c9c
  1. 9
      phoebook/CBookItems.cpp
  2. 2
      phoebook/CBookItems.h
  3. 8
      phoebook/CConsoleUI.cpp

9
phoebook/CBookItems.cpp

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

2
phoebook/CBookItems.h

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

8
phoebook/CConsoleUI.cpp

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

Loading…
Cancel
Save