ex8-6.cpp - Example 8-6 Overloading the Insertion Operator

// File: ex8-6.cpp

#include <iostream>
using namespace std;

const char* suit_name[4] = {"clubs","diamonds","hearts","spades"};
const char* pips_name[13] = {"two","three","four","five","six","seven","eight",
	"nine","ten","jack","queen","king","ace"};

class card
{
  private:
	 int suit;
	 int pips;
  public:
	 card(int=0);
  friend ostream& operator<<(ostream&,const card&);
};

card::card(int x)
{
  suit = x / 13;
  pips = x % 13;
}

ostream& operator<<(ostream& s,const card& c)
{
  s << pips_name[c.pips] << " of " << suit_name[c.suit] << endl;
  return s;
}

int main (void)
{
  card c1(47);
  card c2;
  cout << c1;
  cout << c2;
  cout << card(3) << card(4);

  return 0;
}



CIS27: Programming in C++    Instructor: Joe Bentley