Factorial Class - Stored in List

Previous      Next      class.3d.cc      list.0_1.h      list.0_1.cc

#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include "list.0_1.h"
using namespace std;
// Overload all the operators for nice looking output

extern int debug;

class B {
   int number;

   long long fact (int n) {
      if (n == 1) return 1;
      return n * fact(n-1);
   }

public:
   B (int number) { this->number = number;  }
   int get_number () { return number; }
   long long factorial () { return fact(number); }
};

void display_B_object (void *obj) {  
   cout << "\t" << ((B *)obj)->get_number() 
	<< "! = " << ((B *)obj)->factorial();  
}

bool find_B_object (void *obj, void *what) {
   return ((B *)obj)->get_number() == *(int *)what;
}

List &operator+(B *obj, List &lst) {
   lst.add(obj);
   return lst;
}

int main (int argc, char **argv) {
   if (argc > 1) debug = 1; else debug = 0;

   List l1, l2;
   l1 + new B(6) + new B(7) + new B(8) + new B(9) + new B(10);
   l1 += new B(11);
   l1 += find_B_object;
   l1 += display_B_object;
   cout << "First:\nL1 = \n" << l1 << "Again: " << l1;
   cout << "Removed 8:\nL1 = \n" << l1 - new int(8);
   cout << "Removed 7, 10:\nL1 = \n" << l1 - new int(10) - new int(7);
   l1 -= new int(6);
   cout << "Removed 6:\nL1 = \n" << l1;
   l1 += new B(12);
   l1 += new B(13);
   cout << "Added 12 & 13:\nL1 = \n" << l1;
   l2 = l1;
   cout << "------- try L2 = L1 ----------\n";
   cout << "\nL2 = \n" << l2;
   cout << "Remove 11 from L1:\nL1 = \n" << l1 - new int(11);
   cout << "\nL2 = \n" << l2;
   cout << "Remove 9 from L2: \nL2 = \n" << l2 - new int(9);
   cout << "done\n";
}