// Lab 1: account.h
// Definition of Account Class
Class Account
{
public:
Account (int);
void credit(int);
int getBalance();
private:
int balance;
}
.h file creation help
Started by jclarke, Feb 26 2008 08:37 PM
5 replies to this topic
#1
Posted 26 February 2008 - 08:37 PM
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
|
|
|
#2
Posted 26 February 2008 - 08:52 PM
#3
Posted 26 February 2008 - 09:24 PM
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
-Dustin
www.theCprogrammer.com
www.theCprogrammer.com
#4
Posted 26 February 2008 - 09:58 PM
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.
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
Posted 27 February 2008 - 02:08 AM
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.
.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
Posted 28 February 2008 - 07:11 PM
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.
.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.


Sign In
Create Account


Back to top









