CS 3723
 Programming Languages 
   Comparison: C++, C, Java  

Comparison: C, C++, Java
  • C is the precursor language. (Systems programming)

  • C++ is a mostly compatible extension of C. (A "better" C, O-O features, as efficient as C)

  • Java is an incompatible but similar extension of C and C++. (Modern language, similar to C and C++, for the Internet and other applications)


Overview:

  • C++ adds to C:
    • "Better C" features: more consistently strongly typed, and improved input/output.
    • Object-oriented features: very general classes, multiple inheritance, templates, user-defined operators.
    • Other features: exceptions.
    • Removes obsolete features.
    • Concurrency, multi-threading, and garbage collection supplied by library functions.

  • Java adapts from C and C++:
    • Simplified features of C.
    • Simplified O-O features of C++: no multiple inheritance, no user-defined operators.
    • Other features of C++: exceptions.
    • Other features: built-in concurrency, multi-threading, applets, and garbage collection.


Comparison Table of Features: Note that the column labeled "C++" below does not mention that a C feature can usually be employed in C++ in the same form, but only gives the separate C++ feature, if there is one. The simple examples below ignore details and extra possibilities, which are explained elsewhere.

Feature C C++ Java
Comments
/* This is a C-style
 comment */
/* C-style comment */
// C++ and Java
/* C-style comment */
// C++ and Java
Null pointer NULL (preferred)
(needs #include <stdio.h>)
0 (allowed)
0 (preferred)
NULL (with
   #include <stdio.h>)
null
Constants #define PI 3.141592653590 const double PI =
   3.14159265358979;

(also allowed in C)
final double PI =
   3.14159265358979;

   or Math.PI
Declarations At beginning of a block
(can be used thereafter)
Anywhere in a block
(can be used thereafter)
Anywhere in a block
(can be used thereafter)
Macros
#define ABS(x)
  ((x)>0 ? (x) : -(x))
inline abs(int x) {
 return(x>0 ? x : -x);}
No macros
Arrays
int x[10];
int x[10];
int[] x =
    new int[10];
Arrays
(dynamic)
#include <stdlib.h>
int* x = (int *)
  malloc(10*sizeof(int));
int* x =
    new int[10];
int[] x =
    new int[10];
Arrays
(references)
x[i] // subscript
*(x + i) // pointer
x[i] // subscript
*(x + i) // pointer
x[i] // subscript only
Deallocation of
dynamic storage
Must be explicitly done
with free (complicated)
Must be explicitly done
with delete (very complicated)
No explicit deallocation,
uses garbage collection
Output
#include <stdio.h>
printf("%i\n", x[i]);
#include <iostream>
using std::cout;
cout << x[i] << "/n";
System.out.print(
    x[i] + "\n");
Boolean 1 or any int != 0 is true
0 is false
bool a = true;
bool b = false;
boolean a = true;
boolean b = false;
while loop
do while loop
int i = 10;
while(i--)
   x[i] = 0;
int i = 10;
while(i--)
   x[i] = 0;
int i = 10;
while(i-- != 0)
   x[i] = 0;
for loop
int i;
for (i = 0; i < 10; i++)
   x[i] = 0;
for (int i = 0;
      i < 10; i++)
   x[i] = 0;
for (int i = 0;
      i < 10; i++)
   x[i] = 0;
main
function
int main(void) { }
int main(int argc,
   char* argv[]) { }
int main() { }
int main(int argc,
   char* argv[]) { }
public static void
  main(String[] args) {
}
function
prototypes
If no prototype or definition,
function returns int
Prototype or definition
required before use
Only in an interface.
No def required before use.


Revision date: 2013-02-16. (Please use ISO 8601, the International Standard Date and Time Notation.)