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

What's inside.

Ta Email

Day Class Notes

Download Compiler

Evening Class Notes

Lab Notes

Linux For Windows

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
Polymorphism Problem - Item Class

Problem description.

Item class

% cat item.h
#ifndef ITEM_H
#define ITEM_H

class Item
{
  protected:
     string title;
     double price;
     int invent;
   public:
     Item();
     Item(string t, double p, int i);
     int getInvent() const;
     void setInvent(int i);
     virtual void invoice(ostream &out) const;
};

#endif

% 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() const
{
  return invent;
}

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

void Item::invoice(ostream & out) const
{
  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 << "Book: " << title
      << "\nAuthor: " << author
      << "\nPublished: " << year
      << "\nPrice = $" << price << endl;
}


Candle class

% cat candle.h
#ifndef CANDLE_H
#define CANDLE_H

class Candle : public Item
{
  protected:
    string color;
    int size;

  public:
    Candle();
    Candle(string t, double p, int i, string a, int y);
    void invoice( ostream &out ) const;
};

#endif

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

#include "item.h"
#include "candle.h"

Candle::Candle() : Item(), color("color"), size(1) {}

Candle:: Candle(string t, double p, int i, string c, int s) :
Item(t, p, i), color(c), size(s) {}

void Candle::invoice( ostream &out) const
{
  out.setf(ios::fixed | ios::showpoint);
  out.precision(2);
  out << "Candle: " << title
      << "\nColor: " << color
      << "\nDiameter: " << size << " inches"
      << "\nPrice = $" << price << endl;
}

Card class

% cat card.h
#ifndef CARD_H
#define CARD_H

class Card : public Item
{
  protected:
    string event;

  public:
    Card();
    Card(string t, double p, int i, string e);
    void invoice( ostream &out ) const;
};

#endif

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

#include "item.h"
#include "card.h"

Card::Card() : Item(), event("event") {}

Card:: Card(string t, double p, int i, string e) :
Item(t, p, i), event(e)  {}

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

Driver program

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

#include "item.h"
#include "book.h"
#include "candle.h"
#include "card.h"

int main()
{
  Book text;
  text.invoice( cout );
  cout << "\nNumber in inventory = " << text.getInvent() << endl;

  cout << endl;

  Book C_text("Advanced C++", 125.50, 42, "John B. Good", 2008);
  C_text.invoice( cout );
  cout << "\nNumber in inventory = " << C_text.getInvent() << endl
       << endl;


  // Candle class

   Candle cand;
   cand.invoice( cout );

   cout << endl;

   Candle holiday("Holiday", 12.95, 50, "red", 3);
   holiday.invoice(cout);

  cout << endl;

  // Card class

   Card crd;
   crd.invoice( cout );

   cout << endl;

   Card ghd("Box of 25", 18.50, 85, "Ground Hog Day");
   ghd.invoice(cout);

  cout << "\nPolymorphism\n\n";

  // polymorphism


  Item *base_pointer;

  base_pointer = &C_text; // point to a Book object
  (*base_pointer).invoice(cout);

  cout << endl;

  base_pointer = &holiday; // point to a Candle object
  (*base_pointer).invoice(cout);


  return 0;
}

Compile & execute

% g++ item_test.cpp item.cpp book.cpp candle.cpp card.cpp
% a.out

Book: item
Author: author
Published: 1900
Price = $0.00

Number in inventory = 0

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

Number in inventory = 42

Candle: item
Color: color
Diameter: 1 inches
Price = $0.00

Candle: Holiday
Color: red
Diameter: 3 inches
Price = $12.95

Card: item
Event: event
Price = $0.00

Card: Box of 25
Event: Ground Hog Day
Price = $18.50

Polymorphism

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

Candle: Holiday
Color: red
Diameter: 3 inches
Price = $12.95

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