Can any one tell me how to put a part of program into a header file or how to work with multiple files? According to my book, I need to put the template and class part into a separate file and include the header as "counter.h". I have done same and saved the file as counter.h but it didn’t worked. Please help me out!

Thanks in advance daniweb communitiy

#include<iostream>

template <class Type>
class counter
{
      Type data;                                   // store the count  
  public:
    counter (Type N=0){data = N;}                // constructor, default initial is 0
    void increment(Type D=1){data += D;}         // increment is 1 by default
    Type getValue(){return data;}                // return the current count 
};

using namespace std;

int main()
{

    int N;                                      // input number
    counter<int> counterOfOdds;                      // count of odd numbers
    counter<short>counterMultiples;                   // count of multiple of 3

    do
    {
       cout <<"Enter number (-1 to finish): ";
       cin >>N;

       if (N != -1)                             // not the terminator
       {
           if (N%2 == 0) counterOfOdds.increment();       // odd number
           if (N%2 != 0) counterMultiples.increment();    // multiple of 3
       }
    } while(N != -1);

    cout<<"Number of odd numbers: " <<counterOfOdds.getValue()<<endl;
    cout<<"Number of even numbers: "<<counterMultiples.getValue()<<endl;




    system("PAUSE");
    return 0;

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

The book should show you examples, doesn't it?

Make a file called counter.h. Put the entire class definition in it. Erase the definition from the C++ code file. That's all there is to it.

As Waltp said , u gotto create a new file with .h extension and then transfer the complete class code into it .
In the main program call

#include "counter.h"

and then create the class object to use all the methods inside it.

thanks u all,
not its happening.
to things need to remember to include in header file:
1: both file should be in the same location, and
2: we have to include the header as #include"header.h" not as <header.h>
thanks
Jiten

This question has already been answered. Start a new discussion instead.