ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Creating User Defined Library Header File in C++ Programming Language:

Updated on November 6, 2014

Built-In Library Header File in C++ Programming Language:

We commonly call the Libraries of C++ Programming Language as Header files. When we install any IDE for C++ programming, so there is a folder named “lib”, in this folder all library header files are saved. Whenever we use any library we write it at the beginning of the any C++ program. Because if we don’t use the library header file at the top of the program, so the program will not execute and show errors which are called bugs in programming.

As some times we need to use mathematics library header files to use the mathematics methods/functions to solve a problem by the help of C++ programming. i.e. “math.h” library and there are many other library header files as (string, cstring.h, stdio.h, iostream.h etc.).


C++ Built-in Functions:

As we mentioned that if we are going to use any library header file so we should know what type of functions or methods are available for us to use in our programs. For example we are going to use math.h library header file, by the help of this library header file we can use these functions abs(), acos(), asin(), atan(),atan2(), ceil(), cos(), cosh(), div(), exp(), fabs(), floor(), fmod(), frexp(), labs(), ldexp(), ldiv(), log(), log10(), modf(), pow(), sin(), sinh(), sqrt(), tan(), tanh() to solve the problem of mathematics.

So these are the methods which are built-in and we can use them by the help of built-in library header file as that is “math.h”. But we can define our own library header files in C++ programming language, and we can define them according to our requirements.

Programmers should know:

If you are not using the any version or Visual Studio so you should save your library header file at the directory of your main program. As I am using C-Free Compiler for this program, so I will use the same directory when I will save the main function file and library header file.

Defining Library Header File:

Let’s we define a library header file, we will define a library header file for a simple input about the student information. In which there will be a class and two methods commonly called member functions named “void GetData()” and “void DisplayData()”. By which we will take input from the user at the run time and display data on the console during the execution of the program. Working with functions in programming is a good experience because we avoid to write every code each time. If we define a function so we just call that function when we feel to need it in our program at any time. Like this if we define our personal Library Header Files according to our need so we can use them at any time in our programs when we feel to use them.

Listing 1: Member Function GetData()

void GetData()
		{
			cout << "\t\nStudents Information Entry Window\n";
			cout << "Enter Student ID Number: ";
			cin >> IDNumber;
			cout << "\nEnter Student First Name: ";
			cin >> FirstName;
			cout << "\nEnter Student Last Name: ";
			cin >> LastName;
			cout << "\nEnter Student Mobile Number: ";
			cin >> MobileNumber;
			cout << "\nEnter Student Class Section: ";
			cin >> ClassSection;	
		}

Description of void GetData() member function:

In this member function which is GetData(), by the help of this user-defined function we are getting input from the user at the run time or execution of the program. When the program execute successfully then this member function will call from the main function using the object of the class. As we can see that there is a word “void”, its mean that this program will just take input from the user at run time do not return or get any kind of argument.

Listing 2: Member Function void DisplayData():

void DisplayData()
		{
			cout << "\t\nStudents Information Window\n";
			cout << "ID Number\tFirst Name\tLast Name\tMobile Number\tClass Section\t" << endl;
			cout << IDNumber << "\t\t" << FirstName << "\t\t" << LastName << "\t" << MobileNumber << "\t\t" << ClassSection << endl; 
		}

Description of void DisplayData() member function:

In this member function which is DisplayData(), by the help of this user-defined member function the program will display all those data which will be input from the user at the run time of the program. As we can see that there is a word “void”, its mean that this member function will call and just display data not return or get any kind of argument.

Defining User-Defined Library Header File:

We are going to use these two user defined member functions in library header file, by using of a class. And we will save this library header file in “.h” extension. There is advantage if we define Library Header Files by our own logic. Because sometimes we need something during our programming, but we can’t get it from built-in Library Header Files. If we feel need so we should define it as a Library Header File. Then we can use it in future when need it. A good programmer can analyze the problems and he and she should create some alternative solutions for their analyzed problems.

Listing 3: UserInput.h

#include <iostream.h>
#include <string>
using namespace std;
class UserInput
{
	private:
	string FirstName;
	string LastName;
    unsigned long int MobileNumber;
	int IDNumber;
	char ClassSection;
	public:
	UserInput(int INumber, string FName, string LName, int MNumber, char CSection)
		{
				IDNumber = INumber;
				FirstName = FName;
				LastName = LName;
				MobileNumber = MNumber;
				ClassSection = CSection;
		}
		UserInput ()
		{
				FirstName;
				LastName;
				MobileNumber;
				IDNumber;
				ClassSection;
		}
		void GetData()
		{
			cout << "\t\nStudents Information Entry Window\n";
			cout << "Enter Student ID Number: ";
			cin >> IDNumber;
			cout << "\nEnter Student First Name: ";
			cin >> FirstName;
			cout << "\nEnter Student Last Name: ";
			cin >> LastName;
			cout << "\nEnter Student Mobile Number: ";
			cin >> MobileNumber;
			cout << "\nEnter Student Class Section: ";
			cin >> ClassSection;	
		}
		void DisplayData()
		{
			cout << "\t\nStudents Information Window\n";
			cout << "ID Number\tFirst Name\tLast Name\tMobile Number\tClass Section\t" << endl;
			cout << IDNumber << "\t\t" << FirstName << "\t\t" << LastName << "\t" << MobileNumber << "\t\t" << ClassSection << endl; 
		}
};

Description of UserInput.h Library Header File:

As we can see the class name and Library Header File name are both same. You can use different names for these kind of programs when you are defining a user-define library header file. I used the same name to show you that what I am doing in this program and how I used codding in this programs. There will not any problem if we save the file in a different name than the class name. In the class we used two keywords as public and private we commonly use these two terms for the sack of our data. Because everything is not private and also everything is not public. So the variables which contains data they should be private. That’s why the variables which are declared in the class are private. And the other member functions and constructors of the class are public, that’s why they are being access from the main.cpp file’s main function. As you can check them in Listing 4.

Listing 4: main.cpp

#include <iostream.h>
#include "UserInput.h"
using namespace std;
int main()
{
	Usernput UI(106,"Naseeb","Qasab",23561623,'A');  //Sending data as argument to Argument Constructor
	UI.DisplayData();  //Displaying data using Member Function (DisplayData() method)
	
	UI.GetData();
	UI.DisplayData();
}

Description of main.cpp:

Calling the member functions of the class UserInput from the Library Header file and creating an object of the class UserInput which is UI. In UserInput class there are two constructors 1st one an argument constructor and 2nd one is no argument constructor. That’s why in first object we send data to the class UserInput. And then using the UI object we called the DisplayData() member function of UserInput class for output on console. And we also used the GetData() member function to get input from the user during the run time of the program. And then again we used DisplayData() member function to output the data on console.

Figure 1     Execution and Output of the C++ Program on console
Figure 1 Execution and Output of the C++ Program on console

Conclusion:

In this article we discussed the Library Header File, and defined a header file. We also talked about the built-in and user-defined Library Header Files. I hope enjoy it, and learnt something new from this article. If do you have any query so please comment me on my articles.

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article