Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Download Compiler

Final Project

Lab Notes

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
Inheritance Problem: BaseCall Item, Derived Class Book

Item Class

% cat item.h
class Item
{
  protected:
     string title;
     double price;
     int invent;
   public:
     Item();
     Item(string t, double p, int i);
     int getInvent();
     void setInvent(int i);
     void invoice(ostream &out);
};

 % cat item.cpp
#include<iostream>
using namespace std;
#include<fstream>

#include "item.h"

Item::Item() : title("item"), price(0.0), invent(0) {}

Item::Item(string t, double p, int i): title(t), price(p),
  invent(i) {}

int Item::getInvent()
{
  return invent;
}

void Item::setInvent(int i)
{
  invent = i;
}

void Item::invoice(ostream & out)
{
  out.setf(ios::fixed | ios::showpoint);
  out.precision(2);
  out << "Item = " << title << "\nPrice = $"
      << price << endl;
}


Book Class

 
% cat book.h
    #ifndef BOOK_H
    #define BOOK_H
   
    class Book : public Item
    {
      protected:
        string author;
        int year;
   
      public:
        Book();
        Book(string t, double p, int i, string a, int y);
        void invoice( ostream &out ) const;
    };
   
    #endif

 % cat book.cpp
    #include<iostream>
    using namespace std;
   
    #include "item.h"
    #include "book.h"
   
    Book::Book() : Item(), author("author"), year(1900) {}
   
    Book:: Book(string t, double p, int i, string a, int y) :
    Item(t, p, i), author(a), year(y) {}
   
    void Book::invoice( ostream &out) const
    {
      out.setf(ios::fixed | ios::showpoint);
      out.precision(2);
      out << "Item: " << title
          << "\nAuthor: " << author
          << "\nPublished: " << year
          << "\nPrice = $" << price << endl;
    }


Driver Program

% cat book_list.cpp
#include<iostream>
using namespace std;
#include<fstream>
#include<list>   
#include "item.h"
#include "book.h"

void reorder( list<Book> & BL );
   
int main()
{
  Book b1("Advanced C++", 125.50, 42, "John B. Good", 2008);
  Book b2("Computers and You", 1200.00, 1, "Ada Lovelace",1843);
  Book b3("All Who Wander", 28.95, 3, "Bilbo Baggins", 1937);
  Book b4("Learn to Love Exams", 9.95, 987, "P. L. Ato", 2008); 

  // populate a list
  list<Book> bookList;
  bookList.push_back( b1);
  bookList.push_back( b2);
  bookList.push_back( b3);
  bookList.push_back( b4);

  // display the list
  list<Book>::iterator bit;

  for(bit = bookList.begin(); bit != bookList.end(); bit++)
  {
     (*bit).invoice(cout);
     cout << endl;
  }

  // insert a new book

  cout << "INSERT A BOOK\n\n";

  Book b5("Money Isn't Everything", 150.00, 9, "D. Trump", 2006);
  bookList.insert(bit, b5);

  for(bit = bookList.begin(); bit != bookList.end(); bit++)
  {
     (*bit).invoice(cout);
     cout << endl;
  }

  // check inventory to reorder books
  reorder( bookList );

  return 0;
}

void reorder( list<Book> & BL )
{
  list<Book>::iterator bit;

  cout << "REORDER THESE BOOKS\n\n";

  for(bit = BL.begin(); bit != BL.end(); bit++)
  {  if( bit->getInvent() < 10)
     {
        (*bit).invoice(cout);
        cout << "Inventory = " << bit->getInvent() << endl;
        cout << endl;
     }
  }
}


Execution

% g++ book_list.cpp item.cpp book.cpp
% a.out
Item: Advanced C++
Author: John B. Good
Published: 2008
Price = $125.50

Item: Computers and You
Author: Ada Lovelace
Published: 1843
Price = $1200.00

Item: All Who Wander
Author: Bilbo Baggins
Published: 1937
Price = $28.95

Item: Learn to Love Exams
Author: P. L. Ato
Published: 2008
Price = $9.95

INSERT A BOOK

Item: Advanced C++
Author: John B. Good
Published: 2008
Price = $125.50

Item: Computers and You
Author: Ada Lovelace
Published: 1843
Price = $1200.00

Item: All Who Wander
Author: Bilbo Baggins
Published: 1937
Price = $28.95

Item: Learn to Love Exams
Author: P. L. Ato
Published: 2008
Price = $9.95

Item: Money Isn't Everything
Author: D. Trump
Published: 2006
Price = $150.00

REORDER THESE BOOKS

Item: Computers and You
Author: Ada Lovelace
Published: 1843
Price = $1200.00
Inventory = 1

Item: All Who Wander
Author: Bilbo Baggins
Published: 1937
Price = $28.95
Inventory = 3

Item: Money Isn't Everything
Author: D. Trump
Published: 2006
Price = $150.00
Inventory = 9

 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming