5
nitwhiz
5y

How do you do java style oop with classes and inheritance and all that in c++?
Do I really have to make a header and a source file for each class?

And while we are on it: are there any sources of "famous" games written in cpp?

Comments
  • 1
    @nitwhiz
    If you want to do one class per file, then I think yes.
    Most games should be written in CPP, or at least their engine.
  • 2
    Yes, you should have a header for every c/cpp file. You can write code directly into .h files and it will work from a single file (and I do write some smaller helper classes or libs like that sometimes) but its cleaner to split it up, you can even have a single header and multiple .cpp files implementing different parts of it. The headers are mostly supposed to provide forward declarations for everything you write. Much like in for example python, you can't use functions or variables that weren't previously declared. The headers are ordered by priority and pasted to the resulting code at the top so all things are declared, then you can provide a definition anywhere else in the code and C will find it. This also means that you need to be careful about cyclic dependencies. If A needs B to be declared and B needs A, the compiler will no longer be able to place them correctly and compilation will fail.
  • 0
    @Hazarth I think headers in c(++) are like interfaces in java
  • 2
    @netikras no, headers are just declarations of your code. You cant provide two implementations for the same function in single header. That would be like writting this in java:
    public String hello(){
    return "hello";
    }

    public String hello(){
    return "goodbye";
    }

    It wouldnt compile because you have ambiguous method signature. Thats what a header provides, signatures for classes, variables, functions... You just should to write their concrete definitions in the .c file

    An interface gives your code a "model" of what *any* implementation has to provide. a header tells your compiler "function/class/variable with thisName(int) exists somewhere in this codebase"

    I can see why you'd think its similar, but it works in two very different context and towards two very different goals.
  • 0
    To answer your last question: every UE game, every game using the CryEngine, a lot of EA games (including battlefield and need for speed), and a whole lot of indie games
  • 0
    @Krokoklemme whaat where do I find the sources for EA games?o.o
  • 1
    @nitwhiz sorry, misread your question ^^'
  • 0
Add Comment