how to make C++ headers file Code::blocks

In this post we will see how to make C++ headers file in code::blocks .

We known that header file is necessary if we want to use a library functions. In other words, if you are making a library you also need a header file. Header file introduce what functions the library contain, the main code of the function is written in .cpp (for C++ and .c for C )file. Here we will learn a few ways to make .h and .cpp file.

Link :C++ difference between library and header’s file

C++ how to make headers file Code::blocks

Making library file in Code::blocks

If you are using Code::Blocks, the fastest way to make .h and .cpp is to navigate to the left-top side of the window and go to the directory “File->New->Class” and click on it. A new window will appear where you can enter the name of the class (say Class_name).

Clicking on create will create a Class_name.h file inside a newly created folder ‘include‘, and a file Class_name.cpp is created inside a new folder name ‘src‘ (to view these two newly created files you can go to the directory where your project is created). If you open the Class_name.h the file, it will appear like this.

#ifndef CLASS_NAME_H ///Your class name 

#define CLASS_NAME_H ///Your Class name
class Class_name
{
public:

/*Declare your variable,function name here,say I want to make a function that will add two integers*/

int add(int i1 , int i2 ) ;

Class_name( );
virtual ~Class_name( );

protected:
private:
//Declare your function name here

};

#endif // CLASS_NAME_H

The three statements #ifndef CLASSNAME_H, #define CLASSNAME_H and #endif are necessary. They prevent the compiler from multiple inclusion of the contents of the file.

What actually happens is that when the compiler come across the line include/Classname.h” for the first time, it includes the header file contents in the preprocessor. But when it come across the statement “include/Classname.h” again in another file, this time it prevents the inclusion of the content of the file as it has already been included. If the statements are not added you will get an error.

In your .cpp file add the code of add() function. The file will appear as,

#include “../include/Class_name.h”

Class_name::Class_name( )
{
//ctor
}

Class_name::~Class_name( )
{
//dtor
}

int Class_name::add(int i1 ,int i2)
{
return (i1+i2) ;
}

To include or make the add() function callable in your main.cpp file include the line “#include “include/Class_name.h” “. So, the main.cpp file will appear as.

#include <iostream>

#include “include/Class_name.h”

using namespace std ;

int main( )
{
Class_name cn ;

cout<< cn.add(1,999) << endl ;

cin.get() ;
return 0 ;
}

Try running the program, you will see that the function add() is called.

**Note:: The header file name is declared inside ” “(double quotation) and not under ‘< > ‘ (opening and closing angle bracket). This commands the compiler to search for the file in the directory where your main.cpp file is present. However, if we have declared it inside < > the compiler would search for the library file in the standard directory (the directory where all your MingGW library file is stored, most probably the directory “CodeBlocks\MinGW\include” ) and you will get an error as the file could not be found.


Making library file using notepad or notepad++

Using notepad or notepad++ you can also create the .h and .cpp file.

a)To make the header’s file, add the three lines given below.

#ifndef CLASSNAME_H ///CLASSNAME is the name of your class

#define CLASSNAME_H ///CLASSNAME is the name of your class

/* Declare your class or function name here */

#endif ///CLASSNAME

I have already explained the importance of these three statements. Now save the file in your main.cpp file directory.

b ) To make the .cpp file.

Open the notepad and include the header file name as given below and save it as classname.cpp in the directory of the main.cpp file.

#include “classname.h”

/* Methods or functions definition here */

Note that defining the function or method is allowed only inside .cpp file. However, you can define an inline or constexpr function inside .h file because inline functions does not behave like the usual function and are meant only for code substitution. More about inline function will be discussed in Chapter 4

A program is given below where a function is defined inside the classname.h file as an inline method.

//classname.h
#ifndef CLASSNAME_H ///CLASSNAME is the name of your class
#define CLASSNAME_H ///CLASSNAME is the name of your class
#include <iostream>

using namespace std;

class classname
{
public:
classname( );

inline void func( )
{
cout<<“func() is an inline method \n” ;

}

~classname( );

} ;

#endif ///CLASSNAME

 

The classname.cpp file is shown below.

#include “classname.h”

classname::classname( )
{
///Constructor
}

classname::~classname( )
{
///Destructor
}

The main.cpp file is given below.

//main.cpp
#include < iostream >
#include “classname.h”

using namespace std ;

int main( )
{
classname cn ;
cn.func( ) ;

cin.get() ;
return 0 ;
}
 

Run the progrm and see if the the func() function is called or not.




Leave a Reply

Your email address will not be published. Required fields are marked *