C++ Comparison
suggest changeIntroduction
Java and C++ are similar languages. This topic serves as a quick reference guide for Java and C++ Engineers.
Remarks
Classes Defined within Other Constructs
Defined within Another Class
C++
Nested class[ref]. C++ does not add an implicit enclosing instance. Store an Outer* yourself if you need one.
class Outer {
class Inner {
public:
Inner(Outer* o) :outer(o) {}
private:
Outer* outer;
};
};
Java
[non-static] Nested Class (aka Inner Class or Member Class)
class OuterClass {
...
class InnerClass {
...
}
}
Statically Defined within Another Class
C++
Static Nested Class
class Outer {
class Inner {
// no implicit Outer*; closer to a Java static nested class
};
};
Java
Static Nested Class (aka Static Member Class)[ref]
class OuterClass {
...
static class StaticNestedClass {
...
}
}
Defined within a Method
(e.g. event handling)
C++
Local Class[ref]
void fun() {
class Test {
/* members of Test class */
};
}
See also Lambda expressions
Java
Local Class[ref]
class Test {
void f() {
new Thread(new Runnable() {
public void run() {
doSomethingBackgroundish();
}
}).start();
}
}
Overriding vs Overloading
The following Overriding vs Overloading points apply to both C++ and Java:
- An overridden method has the same name and arguments as its base method.
- An overloaded method has the same name but different arguments and does not rely on inheritance.
- Two methods with the same name and arguments but different return type are illegal. See related Stackoverflow questions about “overloading with different return type in Java” - Question 1; Question 2
Polymorphism
Polymorphism is the ability for objects of different classes related by inheritance to respond differently to the same method call. Here’s an example:
- base class Shape with area as an abstract method
- two derived classes, Square and Circle, implement area methods
- Shape reference points to Square and area is invoked
In C++, polymorphism is enabled by virtual methods. In Java, methods are virtual by default.
Order of Construction/Destruction
Order of Construction/Destruction
Object Cleanup
In C++, it’s a good idea to declare a destructor as virtual to ensure that the subclass’ destructor will be called if the base-class pointer is deleted.
In Java, a finalize method is similar to a C++ destructor, but finalizers are unpredictable because they rely on garbage collection. Prefer an explicit close method. finalize is deprecated for removal; do not use it in new code.
protected void close() {
try {
// do subclass cleanup
}
finally {
isClosed = true;
super.close();
}
}
protected void finalize() {
try {
if(!isClosed) close();
}
finally {
super.finalize();
}
}
Abstract Methods & Classes
Concept | C++ | Java |
—— | —— | —— |Abstract Methoddeclared without an implementation | pure virtual methodvirtual void eat(void) = 0; | abstract methodabstract void draw(); |Abstract Classcannot be instantiated | cannot be instantiated; has at least 1 pure virtual methodclass AB {public: virtual void f() = 0;}; | cannot be instantiated; can have non-abstract methodsabstract class GraphicObject {} |Interfaceno instance fields | no “interface” keyword, but can mimic a Java interface with facilities of an abstract class | very similar to abstract class, but 1) supports multiple inheritance; 2) no instance fieldsinterface TestInterface {} |
Accessibility Modifiers
Modifier | C++ | Java |
—— | —— | —— |Public - accessible by all | no special notes | no special notes |Protected - accessible by subclasses | also accessible by friends | also accessible within same package |Private - accessible by members | also accessible by friends | no special notes |default | class default is private; struct default is public | accessible by all classes within the same package |other | Friend - a way to grant access to private & protected members without inheritance (see below) | |
C++ Friend Example
class Node {
private:
int key; Node *next;
// LinkedList::search() can access "key" & "next"
friend int LinkedList::search();
};
The Dreaded Diamond Problem
The diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C? (from Wikipedia)
The Dreaded Diamond Problem
C++ has always been susceptible to the diamond problem. Java was not, until Java 8. A class still cannot extend more than one class. Default interface methods let it inherit implementation from more than one interface, and that can create a diamond.
java.lang.Object Class
In Java all classes inherit, either implicitly or explicitly, from the Object class. Any Java reference can be cast to the Object type.
C++ doesn’t have a comparable “Object” class.
Java Collections & C++ Containers
Java collections correspond to C++ containers.
Java Collections Flowchart
C++ Containers Flowchart
Integer Types
| Bits | Min | Max | C++ type | Java type |
|---|---|---|---|---|
| 8 | -128 | 127 | signed char |
byte |
| 8 | 0 | 255 | unsigned char |
none |
| 16 | -32768 | 32767 | short |
short |
| 16 | 0 | 65535 | unsigned short |
char |
| 32 | -2147483648 | 2147483647 | int |
int |
| 32 | 0 | 4294967295 | unsigned int |
none |
| 64 | -2^63 | 2^63 − 1 | long long, or long on LP64 |
long |
| 64 | 0 | 2^64 − 1 | unsigned long long |
none |
C++ char may be signed or unsigned; it is not the same type as signed char. On LP64, long is 64 bits. On LLP64 (Windows), long is 32 bits. The old 64-bit row used 2^16 by mistake.