Fibonacci Series Using Op. Overloading


//A program to generatea fibonacci series by overloading a postfix operator//
//generation of the series of fibonacci number
//using operator overloading of postfix incrementer

#include  <iostream.h>
#include<conio.h>
class fibonnaci
{
   private :
      unsigned long int f0,f1,fib;
   public :
      fibonnaci();
//    fibonnaci operator(); //constructor
      fibonnaci operator++(int );
      void display();
};
fibonnaci :: fibonnaci ()
{
   f0=0;
   f1=1;
   fib=f0+f1;
}
void fibonnaci:: display()
{
   cout << fib << '\t';
}
fibonnaci fibonnaci :: operator++ (int x)
{
   f0=f1;
   f1=fib;
   fib=f0+f1;
   return *this;
}

void main()
{
   clrscr();
   fibonnaci obj;
   int n;
   cout << " how many fibonnaci numbers are to be";
   cout << "displayed? \n";
   cin >> n;
   for (int i = 0; i <= n-1; ++i)
{
      obj.display();
      obj++;
}
getch();
}

No comments:

Post a Comment

Custom Search