Duplicating code

greenspun.com : LUSENET : Steve Heller's books : One Thread

In the Dated item functions, these two:

ostream& DatedStockItem::Write(ostream& os)
{
  os << m_Expires << endl;
  os << m_Name << endl;
  os << m_InStock << endl;
  os << m_Price << endl;
  os << m_MinimumStock << endl;
  os << m_MinimumReorder << endl;
  os << m_Distributor << endl;
  os << m_UPC << endl;

return os; }

void DatedStockItem::FormattedDisplay(ostream& os) { os << "Expiration date: "; os << m_Expires << endl; os << "Name: "; os << m_Name << endl; os << "Number in stock: "; os << m_InStock << endl; os << "Price: "; os << m_Price << endl; os << "Minimum stock: "; os << m_MinimumStock << endl; os << "Reorder quantity: "; os << m_MinimumReorder << endl; os << "Distributor: "; os << m_Distributor << endl; os << "UPC: "; os << m_UPC << endl; os << endl; }

have me wondering was it just personal choice that you decided to rewrite all the code for these functions in this class, or was there some reason you couldn't have just output the expiration date material and then called the same function in UndatedStockItem to handle the rest of the function? Isn't one of the ideas behind OO programming in the first place that you don't have to go rewriting lots of code?

-- Mike Mannakee (B3FEETBACK@AOL.com), May 09, 1999

Answers

Yes, it would be possible (and preferable) to write the FormattedDisplay function to call the UndatedStockItem function. Interestingly enough, however, that wouldn't work with the Write function. Take a look at the code for the UndatedStockItem::Write function and tell me why not.

-- Steve Heller (stheller@koyote.com), May 09, 1999.

Moderation questions? read the FAQ