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?
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;
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...
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.
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:
staticvoid 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?
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.
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:
class Test
{
private:
int number;
public:
Test() : number(42) {}
friendvoid 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;
}
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)?