LW5: Inheritance. BaseBookItem -> BookItem.

This commit is contained in:
Kolan Sh 2021-03-28 23:18:08 +03:00
parent 581d21007a
commit e48b696ebd
2 changed files with 30 additions and 20 deletions

View File

@ -8,7 +8,7 @@ void CBookItems::CreateNewItem(string sFirstName, string sLastName, string sPhon
void CBookItems::CreateNewItem(string sFirstName, string sLastName, string sPhoneNumber1, string sPhoneNumber2)
{
BookItem<string>* spaceItem = new BookItem<string>;
BookItem* spaceItem = new BookItem;
spaceItem->_sFirstName = sFirstName;
spaceItem->_sLastName = sLastName;
spaceItem->_sPhoneNumber1 = sPhoneNumber1;
@ -33,7 +33,7 @@ int CBookItems::SetNewValueToItem(string sFirstName, string sLastName, string sP
{
if (nNumOfItem == i)
{
BookItem<string> spaceItem;
BookItem spaceItem;
spaceItem._sFirstName = sFirstName;
spaceItem._sLastName = sLastName;
spaceItem._sPhoneNumber1 = sPhoneNumber1;
@ -72,9 +72,9 @@ int CBookItems::GetItem(int nNumOfItem, string& sFirstName, string& sLastName, s
}
}
const CBookItems::BookItem<string>& CBookItems::GetItem(int nNumOfItem)
const CBookItems::BookItem& CBookItems::GetItem(int nNumOfItem)
{
if (nNumOfItem < 0 || nNumOfItem > _aItems.size()) return CBookItems::BookItem<string>();
if (nNumOfItem < 0 || nNumOfItem > _aItems.size()) return CBookItems::BookItem();
return _aItems[nNumOfItem];
}
@ -309,7 +309,7 @@ int CBookItems::LoadItems(string sNameOfFile)
}
std::ostream& operator<<(std::ostream& s, const CBookItems::BookItem<string>& item)
std::ostream& operator<<(std::ostream& s, const CBookItems::BookItem& item)
{
s << item._sFirstName << " " << item._sLastName << ": " << item._sPhoneNumber1;
if (item._sPhoneNumber2 != "")
@ -318,7 +318,7 @@ std::ostream& operator<<(std::ostream& s, const CBookItems::BookItem<string>& it
return s;
}
std::istream& operator>>(std::istream& s, CBookItems::BookItem<string>& item)
std::istream& operator>>(std::istream& s, CBookItems::BookItem& item)
{
s >> item._sFirstName >> item._sLastName >> item._sPhoneNumber1 >> item._sPhoneNumber2;
return s;

View File

@ -21,32 +21,42 @@ class CBookItems
{
public:
template<class T>
class BookItem
class BaseBookItem
{
private:
protected:
T _sFirstName;
T _sLastName;
T _sPhoneNumber1;
T _sPhoneNumber2;
public:
BookItem()
BaseBookItem()
{
}
virtual ~BookItem()
virtual ~BaseBookItem()
{
}
};
//===============================================================================
/**
*Ïåðåîïðåäåëÿåò îïåðàòîðû âûâîäà/âûâîäà
*/
friend std::ostream& operator<<(std::ostream& s, const BookItem<string>& item);
friend std::istream& operator>>(std::istream& s, BookItem<string>& item);
class BookItem : BaseBookItem<string>
{
public:
BookItem()
{
}
virtual ~BookItem()
{
}
//===============================================================================
/**
*Ďĺđĺîďđĺäĺë˙ĺň îďĺđŕňîđű âűâîäŕ/âűâîäŕ
*/
friend std::ostream& operator<<(std::ostream& s, const BookItem& item);
friend std::istream& operator>>(std::istream& s, BookItem& item);
friend CBookItems;
};
friend CBookItems;
};
vector<BookItem<string> > _aItems;
vector<BookItem> _aItems;
public:
@ -90,7 +100,7 @@ public:
/**
*Âîçâðàùàåò ýëåìåíò òåëåôîííîãî ñïðàâî÷íèêà
*/
const BookItem<string>& GetItem(int nNumOfItem);
const BookItem& GetItem(int nNumOfItem);
//===============================================================================
/**