2

I usually code in Java, but I have started to learn c++ as I'm trying to get into some competitive programming. What is confusing for me right now is that in java you can declare a method within a class, make it public and thats that.

For example, in Java:

public class Main{
    public int javamethod(int myNum){
        return myNum;
    }
}

Whereas in C++, you split the function declaration and definition into different files (like cprogram.hpp and cprogram.cpp). Isn't this redundant and repetetive? What exactly is the point of this?

C++ example (header file)

int cppfunc(int myNum);

(cpp file)

int cppfunc(int myNum){
    return myNum;
}
  • 2
    Unrelated to your problem, but please learn C++ properly first, before going to any of those horrible so-called "competition" sites. Read [some good books](https://stackoverflow.com/a/388282/440558) or take clases. Anything is better than to learn all the bad habits used on such sites. – Some programmer dude Sep 02 '20 at 00:59
  • I agree with @Someprogrammerdude. Unless what you're doing is serious, competitive programming can make you adopt new habits that won't benefit you in the future, e.g. using namespace std, or #include . – BidoTeima Sep 02 '20 at 01:04
  • Also possible duplicate: https://stackoverflow.com/questions/333889/why-have-header-files-and-cpp-files – BidoTeima Sep 02 '20 at 01:05

1 Answers1

3

There are 3 reasons:

  1. Function must be declared before it can be called. So if you have fun1() calling fun2() and vice versa you cannot write implementation for each upon declaration.

  2. To hide dependencies. If implementation is dependent on some include and you don't want to require every user of the function to include it - then you have to split declaration and implementation.

  3. C++ follows crappy archaic build scheme. Headers are copy pasted into other headers and are rebuild each time. Compilation time will rise to ridiculous numbers as each header is recompiled again and again.

Issues 2 and 3 are addressed in C++20. Modules TS solves these two issues and with them you'll be able to write functions without splitting definition and implementation just like in Java without any issues.

Note: you don't have to split declaration / definition. You only need to make the function inline as otherwise linker might scream at you.

ALX23z
  • 4,456
  • 1
  • 11
  • 18
  • The first is really the third reason, aka c++ has a bad compilation model. – Passer By Sep 02 '20 at 01:20
  • @PasserBy the first one originated from the desire to be able to compile fast. If a `fun1()` is not declared prior but called in `fun2()` the compiler will have a much harder time figuring out what a hell is it. Classes's methods do not have such issues because class has a concrete amount of space for declarations. While 2 and 3 are simply fundamental language issues, the first one has a purpose. – ALX23z Sep 02 '20 at 08:27
  • You could stuff an entire program into classes, not really sure what you're point about member function is. It really is a legacy from C, wishing to avoid two passes. – Passer By Sep 02 '20 at 13:06
  • @PasserBy one could but shouldn't put the whole program in a class and nobody will. And yes, avoiding two passes speeds up build, compilation, and simplifies the compiler. Unlike the other two points this isn't a language flaw. – ALX23z Sep 02 '20 at 16:47