Click to See Complete Forum and Search --> : migrating from java to c++


blackrax
03-06-2004, 10:49 AM
hello people,

i'm thinking of learning c++; being a java programmer, with aged and limited experience with c++, i don't really know where to begin - actually, what i'm looking for is a java->c++ migration guide of some sort? i know there's a book around, but atm i can't afford any such luxuries.

what are the main differences (from a programming perspective) - is it difficult? i've heard that the c++ OO is not very pleasant.

primarily i want to use c++ for tui:s/ncurses and replacing all those small java console apps (that really create too much overhead).

regards,
//blackrax

hammer123
03-10-2004, 05:37 AM
some differences I noticed:

inheritance is expressed in different symbols

rather than those annoying interfaces you can use multiple inheritance

#include rather than import

# indicates a precompiler command. people with a c background like to use it to substitute text.

* and & are pointers and addresses, you use them alot, otherwise you will be doing things like passing values instead of objects

jStupidThing myStupidJ = new jStupidThing();
myStupidJ.runSlowToWasteTime();

now often looks like
smart_thing * my_smart_thing = new smart_thing();
my_smart_thing->do_stuff();

blackrax
03-10-2004, 02:33 PM
thanks for the reply, but i was hoping for a more extensive guide of some sort, but i guess that i'll just have to look at the general tutorials.

on thing that bothers me though is the lack of java docs - is there anything similar around for c++? i suppose writing a script to generate something similar isn't that difficult, but it'd be nice if there already was such a thing available.

btw, does cpp have anything similar to java's package statement?

cheers,
//blackrax

gamblor01
03-10-2004, 06:54 PM
Blackrax, I'm with you on this one. I had a year in high school of C++, but it was a very basic intro and we didn't much past like loops, pointers, and functions (you know, like having functions besides main). Then for the past 3 years in college I've been programming pretty much entirely in Java. If you have enough experience in Java though, you will find the switch to C++ quite easy. I went and bought "Sams Teach Yourself C++ in 21 Days" by Jesse Liberty and it's VERY good once you start getting into the second and third week. It only took me about 2-3 to skim through the entire first week as I flipped from page to page saying "I already know this, maybe the syntax is slightly different but I already know it." I also remember having trouble with pointers in high school but when I read the section on them, it made total sense to me now. I can't believe most of us actually have a hard time understanding that a pointer is just a reference to a memory address and nothing more...though I think my two architecture classes more than cleared that up...as well as registers, pipelining, superscalar machines, etc.

Anyway, I think the book was $35, so it's not too terribly expensive.

I'm not sure what you were getting at with Java Doc, but you might look into downloading IBM's project called Eclipse (www.eclipse.org) which is a GREAT developing environment. The debugger is fantastic, and it will help generate Java Doc comments for you (placing comments before all of your methods).

The only disappointing thing is that there's no supreme reference for C++ like Sun's Java API. You kinda just have to hope to stumble upon the information you need, or have someone tell you. Good luck though.

And hammer, right tool for the right job man. I'm not going to get into any C++ vs. Java arguments because they're pointless, but personally I think Java is an extremely well written language, and is probably my preferred language . I can only see better JVMs and better garbage collectors coming in the future and allowing faster Java code, but it will never replace languages like C++ or Assembly. Like I said, right tool for the right job. God bless them all (except the LC-2 ASM language I had to use in my first architecture class...screw that language!). C++ is l33t too and probably my second favorite language.

bwkaz
03-10-2004, 08:35 PM
Originally posted by blackrax
btw, does cpp have anything similar to java's package statement? The cpp executable (the C preprocessor) doesn't, but C++ does.

They're called namespaces. Basically, you say:

namespace MyPackage {
class MyClass {
public:
MyClass() {
x = 1;
} /* inline constructor */

int MyFunction(); /* non-inline function */

private:
int x;
};
} /* (not sure if a semicolon is required here or not...) */

int MyPackage::MyClass::MyFunction()
{
return x;
}

/* To create an instance of MyClass, do one of these: */

{
MyPackage::MyClass thing;
MyPackage::MyClass *thingpointer = new MyPackage::MyClass();

std::cout << thing.MyFunction();
std::cout << thingpointer->MyFunction();

delete thingpointer; /* IMPORTANT, or you'll have a mem leak! */
} thing is an actual instance of a MyPackage::MyClass, and thingpointer is a pointer to one. With pointers, you have to set them equal to a new object before using them, and delete the pointer when you're done. With non-pointers, the C++ runtime deletes the object for you when its lifetime is over.

mwinterberg
03-10-2004, 11:54 PM
Originally posted by hammer123
#include rather than import

That's not entirely true, as you can write complete Java programs without one import, but writing a non-trivial C++ program would take some effort without a #include.

uses/using are the more appropriate analogs (uses "namespaces" instead of "packages", of course, as mentioned in bwkaz's post).

Doxygen (http://www.stack.nl/~dimitri/doxygen/) is also another documentation-from-code generator, however, I don't have any first-hand experience with it.

-Michael

blackrax
03-11-2004, 07:40 PM
Originally posted by gamblor01
Blackrax, I'm with you on this one. I had a year in high school of C++, but it was a very basic intro and we didn't much past like loops, pointers, and functions


yeah, i took a similar course during my high school days; the original intent was to introduce us to OO programming, but the didactic process was brought to a complete stall once we hit the chapter concerning pointers.

Originally posted by gamblor01
I'm not sure what you were getting at with Java Doc, but you might look into downloading IBM's project called Eclipse (www.eclipse.org) which is a GREAT developing environment. The debugger is fantastic, and it will help generate Java Doc comments for you (placing comments before all of your methods).

The only disappointing thing is that there's no supreme reference for C++ like Sun's Java API. You kinda just have to hope to stumble upon the information you need, or have someone tell you. Good luck though.


hehe, i realize now i wasn't very clear about it; basically, i was (am) looking for a "C++ docs"; in my opinion, 'java docs' transcends any other type of api reference available - being a standardized (ie; for java) api reference documentation:(god!) i wish it were around for other programming languages aswell.

as for ide:s, i tend not to use them - i was hooked on vim (i am _not_ an addict, i swear) the very moment i was introduced to linux - although, a graphical debugger would surely come in handy.

Originally posted by bwkaz
thing is an actual instance of a MyPackage::MyClass, and thingpointer is a pointer to one. With pointers, you have to set them equal to a new object before using them, and delete the pointer when you're done. With non-pointers, the C++ runtime deletes the object for you when its lifetime is over.


ahh - this brings me to my next major concern; the absence of automated garbage collection (spoiled by java); how difficult is it to implement? are we talking about an additional 10% code or what - i realize it depends on the utilization of actual OO code, but generally - if there is such a thing?

... and last but not least, thanks for all the replies,
//blackrax

bwkaz
03-11-2004, 09:13 PM
Automatic garbage collection would need to be done by an external library. If it's possible at all, that is. Try googling for "boehm garbage collector", as that seems to be a fairly popular option (the Mono project uses it for their .Net runtime's GC, and Mozilla has a config option to make it use one of them internally too).

If you mean manual garbage collection, it's not extremely hard. You just have to remember to delete everything that you "new". Arrays are a bit harder, but you can get around that by using std::vector (or whatever) instead.

blackrax
03-11-2004, 10:53 PM
ah - well; i was just worried that one had to create (or rather, implement) some nice library to take care of garbage collection - but if all i have to do is to destroy the object, then i have nothing much to worry about.

bwkaz
03-11-2004, 11:10 PM
Oh -- and if you have a pointer that's currently pointing at one object, but needs to point at another object, you have to make sure you either:

(1) delete the pointer before making it point at the other object, or

(2) create another temporary (or not) pointer, so you don't lose track of the first object.

Once you reassign a pointer to another address, you lose the ability to delete the first object (unless you store its address somewhere, like in a second pointer).