Learning  C++
Home
Tutorials
C++  Programs
Contact  us
Sitemap
Your Ad Here
Your Ad Here
Creating  header  files

In  C++  we  have  many  header  files  to  perform  different  operations.  The  advantage  in C++ is  that  the  user  can  create  their  own  header  files  to  suit  the  requirements  of  the program. Let  us   consider  a  case  where  too  many  defined  constants  are  often  used. In  this  case, we  can  create  a  header  file  called  "constant.h"


The  file  will  have  definition  such  as  


              #  define  phi  3.1428
              #  define  g  9.8
              #  define  e  2.7182


This  file  can  be  created  using  any  editor  and  named  as  "constant.h".

For  example,  the  following  program  uses  the  above  said  definitions.  The  header  file "constant.h"  will  enable  to  substitute  for  the  value  of  phi, g  etc,  whereever  they  are encountered  with.




                                  # include <iostream.h>
                                  #include "constant.h"
                                  void  main ( )
                                  {
                                          float  m, h, r, pe, area ;
                                          cout  <<" Enter  mass  and  height " ;
                                          cin >> m>>h ;
                                          pe = m * g * h ;
                                          cout<<" potential  energy = "<<pe ;
                                          cout<<" Enter  radius : " ;
                                          cin  >> r ;
                                          area = 2 * phi * r * r ;
                                          cout <<" Area : "<<area ;
                                          cout  <<endl ;
                                  }
                                         



In  the  above  program  we  have  included  files  in  two  ways.


                                   # include "constant.h"
                                   # include <iostream.h>


The  only  difference  between  both  expressions  is  in  the  directories  in  which  the compiler is  going  to  look  for  the  file.  When  the  file  is  specified  between  quotes  the file  is looked for  from  the  same  directory  in  which  the  file  that  includes  the  directive is,  and only  in case  that  it  is  not  there  the  compiler  looks  for,  in  the  default  directories where it  is configured  to  look  for  the  standard  header  files.

And  when  the  file  name  included  is  enclosed  between  angle  brackets < >  the  file  is directly  looked  for  in  the  default  directories  where  the  compiler  is  configured  to  look  for the  standard  header  files.

Now  let  us  have  another  example  to  find  the  biggest  of  a  given  'n'  numbers  using header files.

         

                                 //  header  file  "big.h"

                                 int  max ( int  x [ ],  int  n )
                                 {
                                         int  i,  m ;
                                         m = x [ 0 ] ;
                                         for ( i = 1; i< n; i++ )
                                         {
                                                  if ( m < x [ i ] )
                                                  m = x [ i ] ;
                                         }
                                         return  m ;
                                 }



                                 //  program  using  big.h

                                 # include "big.h"
                                 #include <iostream.h>
                                 void  main ( )
                                 {
                                            int  a [ 100 ],  n,  i,  large ;
                                            cout <<"\n Enter how  many  numbers :" ;
                                            cin  >> n ;
                                            cout<<"\n Enter "<<n<<" numbers  " ;
                                            for ( i=0; i<n; i++ )
                                            cin >> a [ i ] ;
                                            large = max ( a , n ) ;
                                            cout  <<"\n Biggest  number  is  : " << large ;
                                            cout << endl ;
                                 }
         


                                       
Output
Enter  how  many  numbers : 5
Enter 5 numbers
12  45  22  90  67

Biggest  number  is  : 90