-3

After some time in C# I've decided to delve into C++, as I enjoy programming video games. C++ is broadly used across the industry so I would like to give it a shot.

The first thing I notice is that C++ doesn't seem to use "objects" the way C# does. In C# ALL programming was done in objects( i.e classes), no methods could be declared outside one, not even an entry point.

C++ appears to use this, but not as often, and sometimes it seems to be more of an afterthought, or perhaps I completely misunderstand the purpose of "objects/classes" in C++.

Which brings me to another thing I'm confused about: headers. From what I can gather, THIS is where you define objects/methods to be used in your program. It's sort of a framework for the main program.

Basically I'm confused about project composition. I'm trying to use this like C#, but 'snot working. Ideally someone here is familiar with both languages and could help close the gap, but just brief explanation of common C++ program structure should suffice.

Thank you very much in advance!

P.S. I imagine that there is probably no such thing as "common" C++ structure, but the relationship between objects and methods or the lack their in is what I'm looking for

  • 1
    What specifically is your question? We'd be happy to help, but the SO format expects you to provide specific questions with definitive answers. You might want to split this into several smaller questions. – templatetypedef Apr 03 '13 at 22:52
  • 2
    It's not an afterthought. C++ is a multi-paradigm language. It allows you to use classes when you have some state and non-member functions when you don't. To me, that is what makes it a great language. You get to choose the right tool for the job. – Joseph Mansfield Apr 03 '13 at 22:53
  • 3
    You need one of these: [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242). Explaining all the basics of C++ programming is, unfortunately, far too broad for a Stack Overflow question. You'll instead need to ask more specific questions you come across as you're learning the language. – Cody Gray - on strike Apr 03 '13 at 22:54

2 Answers2

3

You can use classes about as much or as little as you want in C++, but it's definitely true that they're not mandatory for everything like they are in C# (though no, the entry point can't be in a class in C++, but you can make your own decision about most other things).

As to how you should write C++: views on that have changed over time. Back in (say) the early 1990's, most people wrote C++ about like they do Java or C# today, with essentially all code, types, etc., rolled up into classes of some sort or other. Over time, however, that has become much less common -- to the point that most C++ programmers now tend to prefer free functions (i.e., ones that aren't members of a class) when at all reasonable, and use member functions only when alternatives are clearly inferior.

Headers are one of those things that are theoretically really simple, but can be rather confusing in practice. A header is just a file that gets included in some other file. You can use #include to include the contents of essentially any file into another file as you see fit.

In practice, you normally want to put declarations of functions, types, constants, etc., into headers, so you can include them in more than one source file, so all those source files get matching declarations.

With templates, however, you generally need to put not just declarations, but the entire definition into a header, so it's visible to the compiler anywhere you instantiate that template. For better or worse, the support C++ provides for separate compilation of templates is right on the ragged edge between "minimal" and "noneistent".

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
-1

win32 is totaly different than visual c++ clr/cli or c#.... but onced experienced .. you´ll see simularities. in win32 you have the main message loop and have to relay everything on your own ... even tough object oriented .... in manneged code like c· or visual c++ ... you controls like buttons can have their own message loop and you don´t have to collect garbage yourself .... take in account it´s done right.

in win32 c++ you need to handle your data very wel, otherwise memory holes/overflow will occur. you would work like an architect, document wel your variables and don´t eave anything behind. that´s the main difference!

NaturalDemon
  • 934
  • 1
  • 9
  • 21
  • 1
    I've never seen a button control that has its own message loop. Generally, message loops are per-thread. Aside from that nitpick, there's a lot more to C++ than Win32 programming. – Cody Gray - on strike Apr 03 '13 at 23:00
  • ´virtual void WndProc( Message %m ) override {...}´ you can use this code in any control and spy on it. – NaturalDemon Apr 03 '13 at 23:02
  • (1) That is C++/CLI code, not C++ code. Meaning that it runs on the .NET Framework and thus has almost nothing to do with the question being asked here. (2) That's not a message loop, it's a window procedure. Message loops dispatch messages to window procedures. (3) This is a non-unique difference between native C++ and .NET languages. Both have message loops and window procedures. – Cody Gray - on strike Apr 03 '13 at 23:04
  • Cody, i know that ... but i´m trying to explain the main difference between c#, visual c++ cli/clr and win32 c++ ... and even pure win32 code is no garantee it will run on a apple o linux machine with adaption .. and i´m not academic and there can´t explain it in this way ... i also coding a directX project – NaturalDemon Apr 03 '13 at 23:09