tags:

views:

367

answers:

7

Respected Sir!

As i have not learnt java yet but most people say that C++ has more OOP features than Java, I would like to know that what are the features that c++ has and java doesn't. Please explain.

+4  A: 

C++ is a huge language and it is common for C++ developers to only use a small subset during development. These language features are often cited as being the most dangerous/difficult part of C++ to master and are often avoided.

mikelong
+1: Template Metaprogramming is the shiznit.
Narfanator
Template metaprogramming is sometimes very useful, but it's not really an OOP feature.
j_random_hacker
Of course you are right, Template metaprogramming is not an OOP feature. I included it because Java and C++ both have generics but the template implementation is very different. Thanks for keeping me honest :-)
mikelong
A: 

In C++ you can bypass the OO model and make up your own stuff, whereas in Java, the VM decides that you cannot. Very simplified, but you know... who has the time.

Tor Valamo
Your gravatar is *much* better in the animated form...
ergosys
I know. I thought it would animate, but I'll take what I can get.
Tor Valamo
+6  A: 

From java.sun.com

Java omits many rarely used, poorly understood, confusing features of C++ that in our experience bring more grief than benefit. These omitted features primarily consist of operator overloading (although the Java language does have method overloading), multiple inheritance, and extensive automatic coercions.

For a more detailed comparison check out this Wikipedia page.

Bruce
I agree. Some of the C++ features are considered bad practice... by some.
Josh Ribakoff
@Josh: Some C++ features are considered bad if abused by beginners drivers. Those of us that have our advanced driving license like all the features and don't abuse them (because we have probably learned the hard way why it is bad).
Martin York
i'd say the confusing thing about MI is not the MI itself but how you specify virtual or not inheritence on the base class rather than derived class to control it.
jk
A: 

I suppose some would consider operator overloading an object oriented feature(if you view binary operators not much different then class methods).

Roman A. Taycher
+1  A: 

Some links, that give some good answers:

Andreas_D
A: 

Be careful. There are multiple definitions of OOP out there. For example, the definitions in Wegner 87 and Booch et al 91 are different to what people say in Java is not pure a OOP language.

All this "my language is more OO than your language" stuff is a bit pointless, IMO.

Stephen C
+5  A: 

This might be controversial, but some authors say that using free functions might be more object oriented than writting methods for everything. So by those author's point of view, free functions in C++ make it more OO than Java (not having them).

The explanation is that there are some operations that are not really performed on an instance of an object, but rather externally, and that having externally defined operations for those cases improves the OO design. Some of the cases are operations on two objects that are not naturally an operation of either one. Incrementing a value is clearly an operation on the value, but creating a new value with the sum of two others (or concatenating) are not really operations on the instance. When you write:

String a = "Hello";
String b = " World";
String c = a.append( b );

The append operation is not performed on a: after the operation a is still "Hello". The operation is not performed on b either, it is an external operation that is performed on both a and b. In this particular example, the most OO way of implementing the operation would be providing a new constructor that takes two arguments (after all, the operation is performed on the new string), but another solution would be providing an external function append that takes two strings and returns a third one.

In this case, where both instances are of the same type, the operation can naturally be performed as a static method of the type, but when you mix different types the operation is not really part of either one, and in some cases it might end up being of a completely different type. In some cases free functions are faked in Java as in the Collections java class, it does not represent any OO element, but is rather simple glue to tie free functions are static methods because the language does not have support for the former. Note that all those algorithms are not performed on the collection nor an instance of the contained type.

David Rodríguez - dribeas
You can read an article in DDJ on "How non-member functions improve encapsulation" http://www.ddj.com/cpp/184401197
Didier Trosset
Thanks @dtrosset, I was trying to locate references myself.
David Rodríguez - dribeas
+1 since I think the Java "every function is in a class" *discourages* this useful way of thinking, but as you say you can actually "fake" free functions perfectly so I would stop short of saying the Java language *forbids* this.
j_random_hacker