new to concept of header files..

im new to the concept of using a header file. Here is my attempt:
my Main.cpp will prompt the user to input an integer. Then it will call the function "ChecknDivide.cpp" by using the header file "ChecknDivide.h".

ChecknDivide.cpp includes two function, one a boolean function - bool isitpositive(int n) that check if the input value is positive.
The other function Void divide(int n) - basically just divide n by 100.

Let's say that i have ChecknDivide.cpp function correctly running, is this the right way to write a ChecknDivide.h file and use it in the main.cpp?


main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "AddnDivide.h"
#include <iostream>
#include <cmath>


using namespace std;


int main()
{
    int x;
    cout << "Input an integer: " ;
    cin >> x;

    AddnDivde(x);

    return 0;
}


ChecknDivide.h
1
2
3
4
5
6
7
8
#ifndef ChecknDivide
#define ChecknDivide


{public:    
void Divide(int n);
bool isitpositive(int n);}
#endif 


The #include directive is equivalent to copy-paste

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{public:    
void Divide(int n);
bool isitpositive(int n);}

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int x;
    cout << "Input an integer: " ;
    cin >> x;

    AddnDivde(x);

    return 0;
}
¿does that make sense to you?
i get it,it's just declaring the function to let my main.cpp knows the existence of this function.
but what about my "CheckNdivide".h? is my script for the CheckNdivide.h right?
as a matter of course, always add this:
#pragma once
at the top of all header files, it will save you the pain of multiple declarations.

also,(you didn't do this, but its a hot tip :>) - never define a variable in a header file, every .cpp file that includes the header will get its own copy of the variable because of what ne555 said above.
is my script for the CheckNdivide.h right?


you dont need the public, or the braces. Just the function definitions will do.

And like i said, replace #ifndef #endif with #pragma once.
And like i said, replace #ifndef #endif with #pragma once.

There's absolutely nothing wrong with the #ifndef/#define/#endif way of preventing multiple inclusion. In fact, many would argue that it's a better way of doing it. While #pragma once is supported by most modern compilers, it's not part of the C++ standard, whereas using #ifndef/#define/#endif is guaranteed to work for all compilers.
Last edited on
> it will save you the pain of multiple declarations.
declare all you want, the problem is with multiple definitions

> never define a variable in a header file,
> every .cpp file that includes the header will get its own copy of the variable
if the variable is declared as static every translation unit would have its own copy
if the variable is declared as extern the variable is shared.
if there is no qualification, the linker complains for multiple definition.


> i get it,it's just declaring the function to let my main.cpp knows the existence of this function.
I wonder what you get it.
In main.cpp you call `AddnDivde(x)', but never say what that is.

Also, ¿what's is stopping you from simply testing your snips and read the error messages?
@MikeyBoy
Agreed there's nothing wrong with traditional include guards, I didnt realise #pragma once isn't part of the standard.
Topic archived. No new replies allowed.