Header files and C++ files


Some questions:

1.-What's the difference between a .h and a .cpp, when used for functions and classes?

2.-What exactly happens when I include a file in a C++ program? Does it count as if the code of that library/file would be put directly where the include is?

3.-Is it completely neccesary to include the libraries I need to use in my program on all .h or .cpp files, or is it better to only include those libraries in the main file (Before including the other files)?

4.-How to make a namespace? And what exactly is a namespace? A special class of a file/library?
Last edited on
closed account (o3hC5Di1)
Hi there,

1) a.h will contain the function prototypes and / or class declarations. so it will contain stuff like:
1
2
3
4
5
6
7
8
9
class someclass 
{
    private:
        int member_int;

    public:
        someclass();
        int add(int);
};


But no actual definitions, thos go in the a.cpp file:

1
2
3
someclass::someclass() : member_int(5) {}

int someclass::add(int i) {return i+memer_int; }


2. Yes - Which is why you need to use include guards, to prevent variables and classes being defined more than once accidentally

3. Best if someone more experienced than me answers that.

4. A namespace is a block in a file which isolates it from other code, but only for as far as naming goes. By that I mean that within a certain namespace you can for instance use the same variable name as you used in another namespace, without getting errors from the compiler:

1
2
3
4
5
6
7
8
9
10
11
12
13

namespace A
{
    int integer = 5;
}

namespace B
{
    int integer = 10;
}

std::cout << "Int A: " << A::integer;
std::cout << "\nInt B: " << B::integer;
Int A: 5
Int B: 10



Hope that helps.

All the best,
NwN
Last edited on
1) So header files are only for prototypes?

2) Ok, thanks, then I was right

4) Hm, then, can I put namespaces into header files too? And is there a way to use public functions from classes without needing objects or the scope resolution operator (::)?

One last question: When I include a header file to my program, does it automatically use a .cpp file of the same name for the prototypes made there or what? Because I remember seeing something working like that before...
You don't need to bother with namespaces for small projects. They are usually used in large projects to prevent name collisions.
Last edited on
closed account (o3hC5Di1)
samrux wrote:
One last question: When I include a header file to my program, does it automatically use a .cpp file of the same name for the prototypes made there or what? Because I remember seeing something working like that before...


Let's say you have these files:

- a.h
- a.cpp (has #include "a.h" )
- main.cpp (contains the main() of the program)

Then you compile it (using gcc as an example:

g++ -Wall --ansi -o program main.cpp a.cpp


If you're using an IDE like code::blocks this will probably be done for you because they usually make use of project-files.

All the best,
NwN
Is there a way to use public functions from classes without needing objects or the scope resolution operator (::)?
Last edited on
As far as I know, no you can't.
non-static member functions definitely need an object. No way around it. Even the scope resolution operator is not enough.

For example:

 
int bar = string::length();  // nonsense.  Which string are you trying to get the length of? 


static member functions can use the scope resolution operator, but I think you can also use the 'using' directive to sidestep that (note: I'm not 100% sure this works... try it and see):

1
2
3
4
5
6
7
class Foo
{
public:
  static void func();
};

using Foo::func;



But doing this kind of defeats the point of having the function be a static member. Why not just make the function global if that's what you want?
Last edited on
I want public class functions wich interact with the private functions of the same class but can be accesed without needing ClassName::VariabelName. But seems it's not possible.
closed account (o3hC5Di1)
Well it is possible - but you'll have to create an object:

1
2
3
ClassName obj = new ClassName();
obj.variablename;
obj.public_function();


All the best,
NwN
I mentioned objects before... I said that I would want to call a public function directly by its name (to say it in a shorter way)

Marked as solved
closed account (o3hC5Di1)
Ah sorry, must have missed that.
No, you will always have to relate the function to the class somehow otherwise the compiler doesn't know where it can find the function.

It doesn't make much sense either to define a function in a class, which you want to call as being not a part of the class.

What you can do is create friend functions that are allowed to access the private data of a class, but just using them "because you don't like typing the classname" would not be a valid enough reason I'm afraid. nevertheless, an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Test
{
    private:
        int number;

    public:
        Test() : number(42) {}
        friend void mega_integer_printer(Test t);
};

void mega_integer_printer(Test t)
{
    //friend function is allowed access to private data
    std::cout << "the integer is: " << t.number;  
}

int main()
{
    Test t;
    mega_integer_printer( t );
    return 0;
}
the integer is: 42


Hope that helps.

All the best,
NwN
Last edited on
lol, thanks NwN, I did'nt think about that, but i won't use it anyways :P

Just noticed this question wasn't answered:

3.-Is it completely neccesary to include the libraries I need to use in my program on all .h or .cpp files, or is it better to only include those libraries in the main file (Before including the other files)?
Last edited on
You should only include headers in files that need it.

IE: if main.cpp uses string, then main.cpp should #include <string>
if secondary.cpp does not use any strings, then it should not #include <string>
Topic archived. No new replies allowed.