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.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)