adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

C++ program to create your own header file using Class

Create your own header file and source file in C++


Step 1: Create Charater.h header file (Header file name Character.h save )
                Header File contains functions definitions and Functions declaration
    Note 1: We can also create header file with functions definitions and functions declaration .
    Note 2: Predefined header file that contains only functions declaration and macros definition.



#ifndef CHARACTER_H
#define CHARACTER_H
#include<iostream>
 using namespace std;
class Character
{
    public:
    //Functions Declaration
        Character();
        Character(const char ch1);
        ~Character();
        void input();
        int isUpper();
        int isLower();
        int isDigit();
        int isBlank();
        int isSpace();
        int isAlph();
        int isAlnum();
        int isCntrl();
        char toUpper();
        char toLower();
        int toASCII();
    private:
         char ch;
};

#endif // CHARACTER_H

   //Functions Difinition outside the class 
   
    Character::Character()
      {
      	ch=' ';
	  }
	Character::Character(const char ch1)
	   {
	   	ch=ch1;
	   }
	Character::~Character(){	}
	
	
	void Character::input()
	  {
	  	cout<<"\nEnter a character:-";
	  	cin>>ch;
	  }
    int Character::isUpper()
     {
    	if(ch>=65&&ch<=90)
    	 return 1;
    	else
    	 return 0;
	 }
    int Character::isLower()
     {
    	if(ch>=97&&ch<=122)
    	 return 1;
    	else
    	 return 0;
	 }

	int Character::isDigit()
     {
    	if(ch>=48&&ch<=57)
    	 return 1;
    	else
    	 return 0;
	 }
	
    int Character::isAlph()
     {
    	if(ch>=97&&ch<=122||ch>=65&&ch<=90)
    	 return 1;
    	else
    	 return 0;
	 }
	
	int Character::isAlnum()
     {
    	if(ch>=97&&ch<=122||ch>=65&&ch<=90||ch>=48&&ch<=57)
    	 return 1;
    	else
    	 return 0;
	 }
	int Character::isBlank()
     {
    	if(ch==' '||ch=='\t')
    	 return 1;
    	else
    	 return 0;
	 }
	char Character::toUpper()
     {
    	ch=ch-32;
    	return ch;
	 }
	char Character::toLower()
     {
    	ch=ch+32;
    	return ch;
	 }
	
	int Character::isSpace()
     {
    	if(ch==' '||ch=='\t')
    	 return 1;
    	else
    	 return 0;
	 }
	int Character::isCntrl()
     {
    	if(ch=='\n'||ch=='\t'||ch=='\r'||ch=='\a')
    	 return 1;
    	else
    	 return 0;
	 }
	int Character::toASCII()
	  {
	  	return ch;
	  }


Step 2:  Create Source File
           Source file can access all the functions which are  available in the header file. Ex.(Character.h)
            Source file include Character.h header file using following  syntax
                 #include"Character.h"
           Note: User define Header file always include  in the main program using the "" .

             
#include<iostream>
#include"Character.h"
 using namespace std;
   int main()
    {
    	Character c1('l');// Implicit declaration
    	if(c1.isDigit())
    	  cout<<"\n Character is digit.";
    	else
    	  cout<<"\n Character is not digit.";
    	  
    	Character c2=Character('R'); //Emplicit declaration
    	if(c1.isUpper())
    	  cout<<"\n Upper character.";
    	else
    	  cout<<"\n Lower character.";
    	  
    	Character c3;
    	c3.input();
    	cout<<" ASCII code = "<<c3.toASCII();
       return 0;
   }
Output:-





NOTE :Header file and main source file that should exist only one folder



Post a Comment

0 Comments