CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2012
    Location
    Kathmandu
    Posts
    31

    [Begineer] Template Issue!

    Greeting CodeGuru World.
    I am C++ beginner programmer. I just learned most of the fundamentals of C++. This is my neither any college project nor any home assignment. This is what i decided to work out on what i have learned from c++ fundamentals book. The Chapter which i was most influenced by "Template" because it is widely used in STL. So, in this project i decided to implement template on fraction class. But, i got a bit confusion about "Class Template". Here i will post my entire source code of my personal small project of "Four Function Calculator"[Took 20-25 min to write source code]
    I decided to put template on class fraction because user may enter floating point or Integers. So, i decided to work on class fraction which will operate on both variable types.


    Code:
    /*A project on building four function calculator which operate on three types of number
    i.e. long integers, decimal/floating numbers & fractional numbers */
    
    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    
    
    class numbers
    {
        private:
            long fNumb;
            char oper;
            long sNumb;
            long result;
        public:
            numbers() : fNumb(0),sNumb(0) {}
            numbers(long f, long s) : fNumb(f),sNumb(s) {}
            void get_numb();
            void numb_display()const;
    };
    
    class decimal
    {
        private:
            double fNumb;
            char oper;
            double sNumb;
            double result;
        public:
            decimal(): fNumb(0.0),sNumb(0.0){}
            decimal(double f, double s) : fNumb(f),sNumb(s) {}
            void get_decimal();
            void dec_display()const;
    };
    
    template<class type>
    class fraction
    {
        private:
            type numenator;
            char c;
            type dinominator;
        public:
            fraction():numenator(1),dinominator(1){}
            fraction(type n, type d) : numenator(n),dinominator(d){}
            void get_frac();
            void frac_display()const;
            void low_term();
            fraction operator +(fraction&);
            fraction operator -(fraction&);
            fraction operator *(fraction&);
            fraction operator /(fraction&);
    };
    
    //-----------------------------------------------------------------------------
    void numbers::get_numb()
    {
        while(true)
        {
            cout << "\nEnter 1st Number: ";
            cin >> fNumb;
            if(cin.good())
            {
                cin.ignore(10,'\n');
                break;
            }
            cin.clear();
            cout << "Incorrect Input, Try Again!";
            cin.ignore(10,'\n');
        }
        while(true)
        {
            cout << "\nEnter 2nd Number: ";
            cin >> sNumb;
            if(cin.good())
            {
                cin.ignore(10,'\n');
                break;
            }
            cin.clear();
            cout << "Incorrect Input, Try Again!";
            cin.ignore(10,'\n');
        }
        cout    << "**Operation Menu, Make your choice**"
                << "\nEnter '+' for Addition:"
                << "\nEnter '-' for subtraction:"
                << "\nEnter '*' for multiplication:"
                << "\nEnter '/' for division:";
        cin >> oper;
        switch(oper)
        {
            case '+' : result = fNumb+sNumb; break;
            case '-' : result = fNumb-sNumb; break;
            case '*' : result = fNumb*sNumb; break;
            case '/' : result = fNumb/sNumb; break;
        }
    }
    
    void numbers::numb_display()const
        {cout << result;}
    
    
    //-----------------------------------------------------------------------------
    void decimal::get_decimal()
    {
    while(true)
        {
            cout << "\nEnter 1st Decimal Number: ";
            cin >> fNumb;
            if(cin.good())
            {
                cin.ignore(10,'\n');
                break;
            }
            cin.clear();
            cout << "Incorrect Input, Try Again!";
            cin.ignore(10,'\n');
        }
        while(true)
        {
            cout << "\nEnter 2nd Decimal Number: ";
            cin >> sNumb;
            if(cin.good())
            {
                cin.ignore(10,'\n');
                break;
            }
            cin.clear();
            cout << "Incorrect Input, Try Again!";
            cin.ignore(10,'\n');
        }
        cout    << "**Operation Menu, Make your choice**"
                << "\nEnter '+' for Addition:"
                << "\nEnter '-' for subtraction:"
                << "\nEnter '*' for multiplication:"
                << "\nEnter '/' for division:";
        cin >> oper;
        switch(oper)
        {
            case '+' : result = fNumb+sNumb; break;
            case '-' : result = fNumb-sNumb; break;
            case '*' : result = fNumb*sNumb; break;
            case '/' : result = fNumb/sNumb; break;
        }
    }
    
    void decimal::dec_display()const
        {cout << result;}
    
    //-----------------------------------------------------------------------------
    
    void fraction::get_frac()
    {
        cout << "Enter fraction in (N/D) format: ";
        cin >> numenator >> c >> dinominator;
    }
    
    void fraction::frac_display()const
        {cout << numenator << c << dinominator;}
    
    fraction fraction :: operator +(fraction& f1)
    {
        type nu, di;
        nu = numenator+f1.numenator;
        di = dinominator + f1.dinominator;
        return fraction(nu, di);
    }
    fraction fraction :: operator -(fraction& f1)
    {
        type nu, di;
        nu = numenator-f1.numenator;
        di = dinominator - f1.dinominator;
        return fraction(nu, di);
    }
    fraction fraction :: operator *(fraction& f1)
    {
        type nu, di;
        nu = numenator*f1.numenator;
        di = dinominator * f1.dinominator;
        return fraction(nu, di);
    }
    fraction fraction :: operator /(fraction& f1)
    {
        type nu, di;
        nu = numenator*f1.dinominator;
        di = dinominator * f1.numenator;
        return fraction(nu, di);
    }
    
    void fraction::low_term()
    {
        type tnum, tden, temp, gcd;
        tnum = labs(numenator);
        tden = labs(dinominator);
        if(tden==0 )
            {
                cout << "Illegal fraction: division by 0"; exit(1);
            }
        else if( tnum==0 )
            {
                numenator=0; dinominator = 1; return;
            }
    
        while(tnum != 0)
            {
            if(tnum < tden)
            {
                temp=tnum; tnum=tden; tden=temp;
            }
            tnum = tnum - tden;
            }
            gcd = tden;
            numenator = numenator/ gcd;
            dinominator = dinominator / gcd;
    }
    
    int main()
    {
        char choice, ch, oper;
        numbers n1;
        decimal d1;
        fraction f1,f2, result;
        do
        {
            cout    <<"\n******MAIN MENU******\n"
                    <<"**Choose Operation type**"
                    <<"\nPress'n' for calculating long numbers:"
                    <<"\nPress'd' for calculating decimal numbers:"
                    <<"\nPress'f' for calculating fractional numbers:"
                    <<"\nPress'x' for exit::";
            cin >> choice;
            switch(choice)
            {
                case 'n' : n1.get_numb();n1.numb_display(); break;
                case 'd' : d1.get_decimal(); d1.dec_display(); break;
                case 'f' : f1.get_frac(); f2.get_frac();
                            cout    << "**Operation Menu, Make your choice**"
                                    << "\nEnter '+' for Addition:"
                                    << "\nEnter '-' for subtraction:"
                                    << "\nEnter '*' for multiplication:"
                                    << "\nEnter '/' for division:";
                            cin >> oper;
                            switch(oper)
                            {
                                case '+' : result = f1+f2; break;
                                case '-' : result = f1-f2; break;
                                case '*' : result = f1*f2; break;
                                case '/' : result = f1/f2; break;
                            }
                            result.low_term();
                            result.frac_display();
                case 'x' : exit(1);
                default : cout <<"\nUnknown KeY!";
            }
            cout <<"\nDo another calculation? (y/n)"; cin >> ch;
        }while(ch!='n');
        return 0;
    }
    I already mentioned, I am just a beginner programmer. I haven't post any comment of each line because there is no any complex method used. I guess it might be a one of the simplest program of C++. I guess i messed a bit in class fraction because my compiler signal most of the error from there.

    Awaiting any suggestion:
    Regards
    Basanta

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [Begineer] Template Issue!

    Quote Originally Posted by basanta View Post
    Greeting CodeGuru World.
    I am C++ beginner programmer. I just learned most of the fundamentals of C++. This is my neither any college project nor any home assignment. This is what i decided to work out on what i have learned from c++ fundamentals book.
    You should re-read your book again. Your error is very basic.

    You don't need all of that code you wrote to learn about how to properly declare and define template classes and functions. If you started with a very simple example, the error should have been obvious.
    Code:
    template <class T>
    class foo
    {
        public:
            T func();  // declaration
    };
    
    // Implementation
    template <class T>
    T foo<T>::func()
    { }
    This is how you implement the func() function outside of the template declaration? That is the entire basis of the errors you're having now.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: [Begineer] Template Issue!

    There's also no point in making this a template that can handle floating points.

    A fraction class doesn't really make much sense with floating points, the whole point about fractions is that you have a numerator and a denominator both being integers.

    Now, it might make more sense if you allowed your fraction class to accept different types of integers. Although you probably want to restrict it to unsigned integers with a separate sign bool.


    If you're new to C++, you probably want to make your class as a regular non templated class first and just make it work with 1 specific type. Then once it works you can start asking yourself "would this make sense with other types", and then convert the class from a regular class into a templated class.
    Trying to learn c++ by directly trying to design your own templates is i.m.o. not the best approach.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured