Jump to content

.h file creation help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
I am currently stuck with the C++ coding, i don't know how to actually create a .h application interface. I usually just get a new project where it is saved as .cpp. BUT this time, I want to know how to create and save as a .h file in part of my work. For example - code below



// Lab 1: account.h

// Definition of Account Class


Class Account

{

public:

      Account (int);

      void credit(int);


      int getBalance();

private:

      int balance;

}




#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
I really have no clue what you are asking.

#3
broncoslb

broncoslb

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
The .h ,or header files, are used to declare your functions. Now when you write your definitions in the accounts.cpp file just remember to use the #include "accounts.h" file. Also include it in your main.cpp file

#4
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
Normally, the .h-extension is used for C, while the .hpp-extension is used for C++. It's not wrong to use the first for C++ programs, but I prefer to easily distinguish between C and C++, so personally, I use the second.

Sometimes if you include the header-file in lots of files, you'll may make sure that the header-file is only included once (or you'll end up with compiler-errors telling you that you've multiple declarations) You can handle this using preprocessors.
#ifndef MY_HEADER__HPP
#define MY_HEADER__HPP

// All your declarations goes here...

#endif


#5
Frenzy123

Frenzy123

    Newbie

  • Members
  • PipPip
  • 15 posts
Hi clarke, if i remember rightly your using vis studios to create your projects. On the left hand side (usually) in the solution explorer you can right click on headers and select add, new item. Then from that menu just select the .h file type.(you probably new that though).

.h files are usually used to declare variables and functions which are then defined in the .cpp files. Your code seems fine except you may want to add a semi-colon after the last brace but im not sure if these is needed or just good practice, you just need to use the include function shown below in the required .cpp files to be able to use the variables and funtions within the header file.

#include "account.h"

That is prejuming you named the .h file account.

#6
jclarke

jclarke

    Programmer

  • Members
  • PipPipPipPip
  • 106 posts
I figured it out, before you could say that, but thanks! The problem has been solved. Sorry for the incovience....This forum is where I can find it really useful :cool:



Frenzy123 said:

Hi clarke, if i remember rightly your using vis studios to create your projects. On the left hand side (usually) in the solution explorer you can right click on headers and select add, new item. Then from that menu just select the .h file type.(you probably new that though).

.h files are usually used to declare variables and functions which are then defined in the .cpp files. Your code seems fine except you may want to add a semi-colon after the last brace but im not sure if these is needed or just good practice, you just need to use the include function shown below in the required .cpp files to be able to use the variables and funtions within the header file.

#include "account.h"

That is prejuming you named the .h file account.