Skip to content

Instantly share code, notes, and snippets.

@qpfiffer
Created August 16, 2011 05:17
Show Gist options
  • Select an option

  • Save qpfiffer/1148481 to your computer and use it in GitHub Desktop.

Select an option

Save qpfiffer/1148481 to your computer and use it in GitHub Desktop.
lolNub
#pragma once
#include <ostream>
#include <set>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
typedef set<string> StringSet;
class Item
{
public:
Item(const string& title, const string& artist);
Item(const string& title, const string& author, const int nPages);
virtual ~Item();
//string getTitle() { return title; }
friend bool operator<(const Item& i1, const Item& i2);
virtual string toString() = 0;
void addContributor(string newContrib);
void addKeyword(string keyword);
protected:
string title;
string creator;
int length;
set<string> *contributors;
set<string> *keywords;
};
class Book: public Item {
public:
Book(const string& title, const string& author, const int nPages);
string toString();
};
class Movie: public Item {
public:
Movie(const string& title, const string& director, const int duration);
string toString();
};
class Album: public Item {
public:
Album(const string& title, const string& band, const int nSongs);
string toString();
};
// You can't store Item* in an ItemSet, because that would disable the automatic
// sorting that set does. Containers in the C++ STL are designed to store
// INSTANCES, not POINTERS to instances.
//
// Instead we store instances of ItemPtr in the ItemSet defined in Library.h.
// Each instance of ItemPtr contains a pointer to an instance of Item. In this way,
// the container can call operator< for ItemPtr (which can then call operator<
// for Item) to determine the order in which to store the items it's given.
class ItemPtr
{
private:
Item *ptr;
public:
ItemPtr(Item *ptr) : ptr(ptr) { }
Item* getPtr() const { return ptr; };
};
// compare two instances of Item
bool operator<(const Item& i1, const Item& i2);
// compare two instances of ItemPtr
bool operator<(const ItemPtr& ip1, const ItemPtr& ip2);
ostream& operator<<(ostream& out, const Item* const item);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment