Fibonacci numbers (using class)

This is a discussion on Fibonacci numbers (using class) within the C++ Programming forums, part of the General Programming Boards category; i want to write a program to compute the Fibonacci numbers (max 20 numbers).but i want to do it using ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    5

    Fibonacci numbers (using class)

    i want to write a program to compute the Fibonacci numbers (max 20 numbers).but i want to do it using a class.

    i wrote this and i want your opinions.
    it computes the Fibonacci numbers for given num and then prints the result.
    should i change anything?

    Code:
    class fib
    {
        private:
            int fibar[20];
        public:
            fib();
            void create_nums(int);
            void show(int);
    };
    void fib::show(int n)
    {
            for(int i=0;i<n;i++)
        cout<<fibar[i]<<"\n";
    }
    fib::fib()
    {
        memset(fibar,0,sizeof(int)*20);
        fibar[1]=1;
    }
    void fib::create_nums(int n)
    {
            for(int i=2;i<n;i++)
        fibar[i]=fibar[i-2]+fibar[i-1];
    }
    main()
    {
        fib obj;
        int num;
    
        cout<<"give N:";
        do{
            cin>>num;
            }while(num>20 || num<0);
    
            obj.create_nums(num);
            obj.show(num);
    }

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    use a vector instead of a fixed length array

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    5
    thanks for your reply.
    I 'll change it.
    something else?
    about the implementation?

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Firstly, it's int main(). Standard C++ does not have default return type (int as in C).

    Secondly, this class relies too much on the user of the class getting things right. For example, the display function assumes that you have called the create function earlier with a value large enough. What if the user of the class makes a mistake and asks the class to output more values than he has created earlier?

    One possibility here would be to make a create function private and call it from show to generate more fibonacci numbers as needed. If the user can only call the show function, and that takes care that the instance stores a sequence as long as requested, there are less ways to misuse the class.

    Also the class relies on the user checking if the input to the methods is less than 20 - from the user's perspective it is a completely arbitrary value. They shouldn't know that the class has a private array of that size (though you might add a method that tells them that).

    Then there's also const correctness. fibo::show doesn't change anything in the instance, so it should be declared a const method:

    Code:
            void show(int) const;
    It is also not a very good idea to omit variable names from function declarations. Looking at the class declaration I can see that the methods take an int parameter but what's it supposed to mean? OK, in this case I can guess, but that is not always the case.

    If you switch to using a vector (dynamic size), you might also need to check for integer overflow. The fibonacci numbers grow very fast and you'll soon get a value that is larger than the int type can hold. Can you check whether the result of an addition would be larger than the type can hold?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  3. Help with programming assignment, please!
    By xMEGANx in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2008, 04:29 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21