As far as the differences between C and C++ go, most of you have got it right. But the origins and nature of C# tends to be somewhat murky in this discussion.
I'll try to clear that up a bit.
Before microsoft created C# they created J, which was their answer to Java (created by Sun Microsystems). Sufficed to say, that language just did not take off as a platform and fell flat on its face. It is rumored that Microsoft was so ashamed of its creation that at one point they denied its existence. In any case with the advent of the .Net platform they took another stab and created C#.
The syntax of C# is very close to Java, more so than it is to C++. Like Java it has
interfaces and a
single-inheritance hierarchy.
What does that mean? Let's start with the whole single-inheritance hierarchy thing. C++ allows for multiple inheritance, that means a class can have more than one super class. There are inherant problems with multiple inheritance, however. Let me give you the classic
Diamond Of Death scenario. You're given four classes. Classes B and C are subclasses of Class A, and Class D --using multiple inheritance-- is a subclass of both B and C. Class A sits at the top of the heirarchy, B and C sit on a level below and D is on the 3rd and last level(if you play connect the dots following the classes in the following order A-B-D-C-A you get a diamond).
Let say both classes B and C override the method 'foo()' of their super class A, but class D inherits from B and C without overriding any methods. How does D resolve calls to its inherited method 'foo()'.
It can't. Hence the fabled
'Diamond of Death' (
queue menacing sound).
The Java designers decided to implement a single-inheritance scheme to remedy this issue. That meant that a class can inherit-from/extend/subclass one and only one super class. To compensate for the loss of functionality that multiple inheritance provided, Java introduced interfaces. Think of them as classes whose methods have no body, that is, they have no implementation.
Interfaces look something like this:
CODE
interface Movable{
public void up();
public void down();
public void left();
public void right();
}
Now, in addition to their
extends keyword Java and C# also have the
implements keyword for interfaces. When the
implements keyword is followed by the name of an interface, a class is required to implement all the interface's methods as seen below:
CODE
class Car extends Vehicle implements Movable{
// ...other code
public void up() {
// implementation goes here
}
public void down() {
// implementation goes here
}
public void left() {
// implementation goes here
}
public void right() {
// implementation goes here
}
}
A class can implement as many interfaces as it likes and since every class that implements an interface has to provide its own implementation of that interface's methods the DOD (Diamond of Death) scenario never arises.
Java also supports garbage collection(i.e. freeing memory occupied by unused objects) and in the true spirit of mimicry so does C#.
And that, as far as I know, is the difference between C# and C/C++. As for C# and Java, this is what one of my former professors told me "Java uses 'String', C# uses 'string'". Nuff' said

.
Reply