C++ Program to Generate Fibonacci Using Copy Constructor


C++ program source code for finding fibonacci series using copy constructor.

A program to generate a series of Fibonacci numbers using a copy constructor where the copy constructor is defined within the class declaration itself.

#include
class fibonacci
{
private:
unsigned long int f0,f1,fib,
public:
fibonacci()
{
f0=0;
f1=1;
fib=f0+f1;
}
fibonacci (fibonacci &ptr)
{
f0=ptr.f0;
f1=ptr.f1;
fib=ptr.fib;
}
void increment()
{
f0=f1;
f1=fib;
fib=f0+f1;
}
void display()
{
cout << fib << 't\';
}
}; //end of class construction
void main (void)
{
fibonacci number;
for (int i=0; i<=15;i++)
{
number.display();
number.increment();
}
}


Related Articles

Program Efficiency

The program efficiency is determined by the two resources - execution time and memory requirement.

Constructor Example in C++

Constructors are the special member functions which has same name as class name and can take arguments but can't return any value.

More articles: C++ Programs C++ Tokens

Comments

Guest Author: Sachin Sharma28 Apr 2015

What is the use of copy constructor here? No statement related to it has been specified in the main() function.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: