Skip to main content
GameDev.net gamedev.net
🔒 Locked

Going from java to c++

Started by Realityman May 12, 2003 at 3:30 AM 15 replies 2.6k views
Original Post
Realityman
Realityman
I''ve taken a few java courses at the university I go to. I''ve made a compiler, a simple compression program and taken some nasty algorithm courses, so my java isn''t half bad. I want to learn 3D programming however, so I figure C++ is the way to go. I just downloaded Bruce Eckels free online c++ book, but it''s so boring going through it trying to find something interesting. My question is: How is c++ different from java?(apart from some minor syntax differences) If someone could just direct me to the parts I have to read I''d be very grateful. Thanks!
sjelkjd
sjelkjd
In java everything is a reference type - in c++, the default is value types. C++ can have explicit references with &, or with pointer semantics. Value types copy by default, every type in c++ has a copy ctr and an assignment operator defined for it, as well as operator==.

in c++ methods are not virtual by default. You must explicitly state this(including destructors). You may also have functions with global scope. You can allocate types either on the stack, or on the heap.


  
MyType type; //allocates a MyType on the stack in c++, calls the default constructor

MyType* pType = new MyType(); //allocates a MyType on the heap in c++

delete pType; //needed to call pType''s destructor



Arguments are passed by value to functions by default - this makes a copy. The same is true for return values from functions.

C++ has more some features that java does not - templates are probably the biggest example you will run into, especially since they form the backbone of the STL(standard template library). Learn and love the stl.

Input and output use streams in c++.

  
MyType type;
cout << "hi there" << 5 << type << endl;
float f;
cin >> f;

cout is an output stream object. operator<< is overloaded so that is puts the rhs argument into the lhs argument stream. You can overload this operator for your own classes(like type in the above example). Similarly for cin.

ok, that was a bunch of rambling, hope it gives you some idea =)
Dark_Streak
Dark_Streak
Like you, I started programming with Java at university, and then learnt (and am still learning) c++ in my own time in order to create ''proper'' 3D graphics applications.

To be honest, if you are fairly comfortable with Java, it really shouldnt be a problem moving to C++. After all, C++ supports all the standard Object Orientation methodology that Java is so strong in.

I recommend getting a good compiler (MS Visual Studio C++) and starting from the beginning by going through all the basic tutorials on C++ programming you can find. I never bought a C++ book, and I will never need to, because everything you need to know can be found online (this is my personal viewpoint, you may be different). I recommend the tutorials over at gametutorials (http://www.gametutorials.com/Tutorials/C++/Cpp_Pg1.htm) as a good starting point.

A few things that stood out when I converted to c++:
- Java is OS independant, don''t forget that C++ is not. You will need to code, compile and build apps using OS specific compilers. Oh, and you ''Build'' applications in C++ to create a runnable executable, but in Java you just had to compile (because of the Virtual Machine).
- Like sjelkjd says, watch out for those pointers. It took me a while to get used to them, be aware of them from the start.
- Garbage Collection in C++ is not automatic like in Java. You need to ''delete'' variables to free up system resources.
- Dealing with Strings in C++ was very frustrating for me. Java Strings are so easy to work with, but I didnt find this the case in C++.

Anyway, good luck. Perserverance is the key.
Qw3r7yU10p!
Qw3r7yU10p!
Buy a good book. The money you''ve invested in it will make you take it seriously:

Accelerated C++ - to get started and productive without getting bogged down in detail
The C++ Programming Language - to get to the nitty gritty
Exceptional C++ - to fine tune your design skills and understanding

You can use a free compiler such as GCC (which comes with dev-C++). Microsoft don''t need your money and you don''t need their compiler.
Arch@on
Arch@on
By the way, you can get OpenGL access from Java too and things aren''t that much slower in there. I however moved to C++ from different reasons. The effort it takes to write OpenGL with Java is totally in wrong balance, besides C++ is little bit more flexible. To make an easy switch from C++ to Java, you need to have excellent Java skills, otherwise you can just start it from the beginning.

If I were to teach a new programmer I would go C++ -> Java -> C++. Java gives you so different persepcitve to OOP that I don''t think anyone who has done Java wants to take the multi inheritance route. Besides it is basically unlogical A humming bird is a Bird and a Car produses completely wierd statements. Or this way: A reptile is a carnivore and a herbivore. Not that the statement is invalid by definition, it is also paradoxical.
Mage2k
Mage2k
quote:
Original post by Captain Goatse
If I were to teach a new programmer I would go C++ -> Java -> C++. Java gives you so different persepcitve to OOP that I don't think anyone who has done Java wants to take the multi inheritance route. Besides it is basically unlogical A humming bird is a Bird and a Car produses completely wierd statements. Or this way: A reptile is a carnivore and a herbivore. Not that the statement is invalid by definition, it is also paradoxical.
Well, seeing as how the c++ standard doesn't have interfaces like in java, it was actually after learning jave (i learned c++ first) that I saw the use for multiple inheritance in c++. Or, at least how to use it correctly and safely. While it is true that multiple inheritance in c++ allows you to do some pretty stupid things, it is the only way to have a class implement specific interfaces and be able to reference those objects with references (or pointers) to those "interfaces" the way you would in Java. It is not that multiple inheritance is bad, it's just that it is too often mis-used. Also, while Java also uses interfaces for context related constants, c++ generally uses macros although there is no reason you couldn't put a bunch of public constant values in a class and inherit from it to get the same effect you do in java. However, macros are much more powerful than aliasing constant values. Make sure you understand the preprocessor as Java does not do any preprocessing vs. in c/c++ where the use of the preprocessor is a very important part of the language.



peace and (trance) out

Mage

[edited by - Mage2k on May 12, 2003 2:58:15 PM]
---------------------------------------------------There are 10 kinds of people in the world:Those that understand binary, and those that dont...Mage
alnite
alnite
quote:
Original post by Dark_Streak
- Dealing with Strings in C++ was very frustrating for me. Java Strings are so easy to work with, but I didnt find this the case in C++.
I agree. C++ string may be harder to use than Java, but it''s not as frustrating as you said This is because there is no native string support in C/C++. String in C/C++ are pointers (char*), and most functions (mostly C functions) accept string argument whose type is char*, instead of std::string. So, I tend to think std::string as "just another class", instead of a data type.


Programmers are lazy people, they keep trying to find a way of doing so many things without moving.


Current project: A puzzle game. Homework.
% completed: ~10%. ~50%
Status: Active. Active.
stilltjack
stilltjack
Howdy,

I really enjoyed C++ for java programmers, it helped me switch over very quickly.

-stilltjack
alnite
alnite
quote:
Original post by Captain Goatse
If I were to teach a new programmer I would go C++ -> Java -> C++. Java gives you so different persepcitve to OOP that I don''t think anyone who has done Java wants to take the multi inheritance route. Besides it is basically unlogical A humming bird is a Bird and a Car produses completely wierd statements. Or this way: A reptile is a carnivore and a herbivore. Not that the statement is invalid by definition, it is also paradoxical.
Then you did the multiple inheritance in the wrong way. Consider this example:
A Command Center is a Unit and a Building.

Some may think, however, that a Building is also a Unit. So, instead of:
CommandCenter : Unit
CommandCenter : Building

They have
Building : Unit
CommandCenter : Building

But this is an issue in design pattern, not the language.
Ishan
Ishan
You may be surprised when you encounter this new feature in C++, it''s affectionaly known in C++ programmer circles as "performance." I think you''ll like it.
nonnus29
nonnus29
I started making the switch from java to c++ almost a year ago. The hardest things for me were: 1. pointers and 2. organizing your project files with #ifndef and #defines so two classes can contain references to one another. I''ve recently moved to ''relearning'' c++ because I found myself using c++ the same old way I did java; my programming skills hadn''t advanced, I was just doing the same stuff in a different language and there were alot of little details I was hazy on. Funny how attempting a project mored advanced than asteroids can expose your weak areas.
sjelkjd
sjelkjd
quote:
Original post by Anonymous Poster
Java 1.5 will have generics. They''re harder to misuse than C++ templates, meaning no template meta-programming.


That''s misleading. Java generics are MUCH less powerful than c++ templates. Sun''s marketing machine is trying to spin this as "harder to misuse." There are many valid c++ idioms that do not misuse templates that are impossible to implement in java. For example, try to write next_permutation with java generics. It''s impossible.


SabreMan
SabreMan
quote:
Original post by Captain Goatse
A humming bird is a Bird and a Car produses completely wierd statements. Or this way: A reptile is a carnivore and a herbivore. Not that the statement is invalid by definition, it is also paradoxical.

What on earth are you on about?
Qw3r7yU10p!
Qw3r7yU10p!
If you only want to do 3d programming I''d say that''s not a big enough reason to learn c++.

If you want to learn OpenGL it can be called from many languages as it uses a simple C interface. There are plenty of bindings for Python, Ruby, Java, FORTRAN, lisp, VB, whatever.

If you''re starting doing OpenGL I''d recommend either staying with java, which you know, or using something like Python and PyOpenGL to get you going. Also VPython is really nice to give you inspiration (as you seem to get bored quickly... you need a bit of a perk up)
Ishan
Ishan
quote:
Original post by SabreMan
What on earth are you on about?



He''s using a nonsensical analogy to misrepresent multiple inheritance. A real example is that a sword might inherit from weapon and item, so you can pick it up, and give Hillary Rosen some sweet justice.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.