D
d2003xx
cyberdude said:Hi,
Both C++ and Java are object oriented. What are the major differences
between them then? Thank you in advance.
C++ is just a language, Java is a complete platform.
cyberdude said:Hi,
Both C++ and Java are object oriented. What are the major differences
between them then? Thank you in advance.
Stack based objects tend to be confusing if you are used to the Java
model where objects are always allocated on the heap and managed by
references (similar to pointers.)
Think of it as a java primitive, but as an object. You don't use new
when using primitives in java. You wouldn't call java primitives useless?
No. Myclass o creates an instance of MyClass on the stack. Once o goes
out of scope, the destructor is automatically called. In C++ MyClass o =
new MyClass() is not even legal syntax. Instead, you must use a pointer,
MyClass* o = new MyClass(). This will allocate o on the heap. It will
not get automatically destroyed, it must explicitly be deleted.
Do you know how a stack works!??
The whole point of a stack is, there's nothing to manage (with regard to
memory), the program 'just' runs and puts variables on the stack or
retrieves them from it.
You could also say whether my room is clean, is also a matter of opinion.
But what use is this? Is "f()" a static method? If so, then why have
the line "MyClass o" at all?
To actually get an object MyClass, do you not need to "o = new
MyClass()"? And then, if you do not manage the "delete o", does not
the object become unreferenced and therefore a memory leak? And now
you must manage it yourself (as per your last sentence).
I worked beside people who were working on a rather large C++ project.
This project had memory leaks. It was actually easier to occasionally
bounce the application to recover lost memory than to locate and find
all the memory leaks.
And if you are thinking that the original authors of the project were
lazy and stupid, then maybe you are right. But EVERYONE makes mistakes
in programming. And memory leaks usually only show up in production.
Who called it useless? I was just not clear about it.
Ok, so for allocation purposes it is treated as a primitive? How do
you pass parameters to the constructor?
Josh Joyce said:Just to be clear, C and C++ are two different languages. The set of
all valid C programs is not a subset of the set of all valid C++
programs.
Here is an example of a C program that is not a C++ program:
int main(void)
{
int class = 0;
return class;
}
The C++ run-time system must put the object on the stack, call
constructors, check the scope of the given object, call destructors
and pop the object. That is the management. The programmer has
nothing to do, except defining an object.
d2003xx said:
You don't usually use the term "memory management" for a simple stack.
It refers to something more complicated where you have a heap, must
find a slot of suitable size, compact, shuffle etc.
Both C++ and Java use simple stack allocation for primitives. Both C++
and Java can allocate objects on a heap. The difference is Java gets
rid of the objects automatically, with C++ you must explicitly destroy
the objects. In addition C++ lets you explicitly allocate objects on
the stack, something you cannot do explicitly in Java, but which some
optimising compilers do when it is safe and optimal.
Roedy said:Jet sometimes allocated objects on the stack as an optimisation. You
are totally unaware of this. It is just another form of automatic
garbage collection, somewhat more efficient for special cases.
Is it any wonder teachers go nuts with this stuff?
easier on them and me, but there are some major concepts that drive them
wild with Java that don't occur in C++.<snip>
Sudsy said:Gary Labowitz wrote:
Interesting, since I believe that the optimum approach would be:
- C for the basic language syntax
- Java for the object-oriented approach
- C++ to combine the two
In this scenario you'd have to stay away from the more complex
Java issues (like classpaths and class loaders) and save them
for an "advanced course". Same with elements like templates in
C++. You have to grasp the advanced concepts to be a productive
commercial programmer but I think this ordering is better than
the typical C->C++->Java one.
YMMV.
Sudsy said:In this scenario you'd have to stay away from the more complex
Java issues (like classpaths and class loaders)
Harald Hein said:There is nothing complex in classpath and class loader. Sorry, but a
programmer who doesn't get the simple concept of a search path "First
look here, then here, then here ..." should refrain from touching a
keyboard and maybe try to make a career in used-care sales.
Gary Labowitz said:Ah, but Sudsy, what you say is exactly what's wrong. They do know that a
search path tells them to "look here." But classpath doesn't do that. It
says "Start here and first look for the directory/subdirectory(ies) that are
defined with the package name used and then look there, unless it's a jar in
which case expand the jar and find the directory/subdirectory(ies) structure
in the jar and then look there." That's what drives them nuts. And, of
course, "if the class file isn't in a package (naughty and hard to work
with) then the classpath doesn't have to find the directory structure ...
just look here."
This is NOT what they learned about PATH environment variable.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.