Answer to Question #238485 in C++ for Anil

Question #238485

WAP in C++ to find a Fibonacci scries using copy constructor.


1
Expert's answer
2021-09-17T11:29:50-0400
#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;
	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS