20-CS-122-001 Computer Science II Spring 2012
List class, Many Operators Overloaded, Big Integer Objects

Virtual functions, classes, inheritance, lists, queues, stacks, applications

Prev      Next      Lectures      list.0_1.h      list.0_1.cc      bigint.1.h      bigint.1.cc

#include <iostream>
#include "list.0_1.h"
#include "bigint.1.h"
using namespace std;
// Overloading the "*" operator to make the multiplication easier on
// the eyes.

class C {
   int number;

   BigInteger *fact (int n) {
      if (n == 1) return new BigInteger("1");
      return *(new BigInteger(n)) * fact(n-1);
   }

public:
   C (int number) { this->number = number;  }
   int getnumber () { return number; }
   BigInteger *factorial () { return fact(number); }
};

void display_C_object (void *obj) {  
   cout << ((C *)obj)->getnumber() << "! = " 
	<< ((C *)obj)->factorial();  
}

bool find_C_object (void *obj, void *what) {
   return ((C *)obj)->getnumber() == *(int *)what;
}

int main () {
   List l;
   l + new C(5) + new C(10) + new C(15) + new C(20) + new C(25);
   l += display_C_object;
   l += find_C_object;
   cout << l;
   cout << l - new int(15) - new int(10);
   l -= new int(25);
   cout << l;
}