WAP in C++ to find a Fibonacci scries using copy constructor.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Fibonacci
{
private:
unsigned long int fib0,fib1,fib;
public:
Fibonacci()
{
this->fib0=0;
this->fib1=1;
this->fib=fib0+fib1;
}
// copy constructor.
Fibonacci (Fibonacci &fibonacciPtr)
{
this->fib0=fibonacciPtr.fib0;
this->fib1=fibonacciPtr.fib1;
this->fib=fibonacciPtr.fib;
}
void increment()
{
this->fib0=fib1;
this->fib1=fib;
this->fib=fib0+fib1;
}
void display()
{
cout << this->fib << " ";
}
};
void main ()
{
int n;
cout << "Enter the number of terms: ";
cin >> n;
cout << "\nFibonacci Series:\n";
Fibonacci FibonacciSeries;
for (int i=1; i<n;i++)
{
FibonacciSeries.display();
FibonacciSeries.increment();
}
int i;
cin>>i;
}
Comments
Leave a comment