Fibonacci Series Program In C++ Using Constructor

 Fibonacci Series Program in C ++ Using Constructor




C ++ program to display Fibonacci series using constructor where member functions are defined out side of class definition.

Constructor is a special member function of class.

Member functions of class are defined out side of class definition by using scope resolution (::) operator.

What is Fibonacci series?

Fibonacci series is a series of integers where every number after first two number is sum of  two preceding numbers. For example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ..... So on.


// Display Fibonacci series using constructor where member functions are defined out //side of class definition

#include<iostream.h>

#include<conio.h>

class fibonacci

{

private:

int f0, f1, f2;

public:

fibonacci(); //constructor

     void display(); //member function

}; //end of class construction

fibonacci::fibonacci() //constructor

{

f0 = 0;

f1 = 1;

f2 = f0 + f1;

}

void  fibonacci::display()

{

f0 = f1;

f1 = f2 ;

f2 = f0+f1;

cout<<” Fibonacci Series is:”<<’\t’;

}


void main()

{

fibonacci obj;

// Fibonacci series number up to 50

for(int i=0 ; i<=50; i++)

{

obj.display();

}

}

 

टिप्पणी पोस्ट करा

0 टिप्पण्या