Transcript Chapter 1
C++ for Java Programmers
Chapter 1
Basic Philosophical Differences
Similar Roots, different paths
Both C++ and Java (and C# and PHP
and many other languages) grew out of
one tradition of languages
So it is not surprizing they have
similarities
But also many differences that can trap
the unwary
Chapter 1
C++ for Java Programmers
2
Common Ancestor - C
Designed by Dennis Ritchie in 1970’s
Intended for System Programming
(specifically, unix)
Economy of expression
Easy for compiler to generate code
Both high level and low level features
Chapter 1
C++ for Java Programmers
3
Some Features of C
Explicit use of pointers
Close match between pointers and
arrays
Variety of bit-level operations
Fast execution time
Minimal memory requirements
Simple memory model
Portable assembly language
Chapter 1
C++ for Java Programmers
4
Criticisms of C
“Too concise for human understanding”
while (*p++ = *q++)
Lack of run-time checks makes programs
error prone (array index checking,
parameter matching, etc.)
Opens door or malicious use
gets(s)
a[i++] = i++;
func1(func2(), func3(),func4());
Chapter 1
C++ for Java Programmers
5
Development of C++
Inspired by earlier language Simula
Started as collection of macros and library
routines for C
Intended to be backward compatible with C
Inherited C mind-set (pointers, lax checking,
memory model)
Uncompromising emphasis on efficiency
Chapter 1
C++ for Java Programmers
6
Examples of Emphasis on
Efficiency
Few run-time checks (if you want
checks, you write them yourself)
Many operations ill-defined, so can
match machine dependency
Stack memory model instead of heap
Many decisions left to compiler writer
a[i++] = i++
Chapter 1
C++ for Java Programmers
7
Legacy Problem - old C code
The C++ programmer must know about
Old C libraries
Old techniques (use of preprocessor,
etc)
Old types (char * for strings, etc)
Old styles (use if integers for booleans)
Use of global variables, functions, other
features not found in Java
Chapter 1
C++ for Java Programmers
8
And new features
C++ added a number of new features
to C
Classes, inheritance
Virtual functions
Templates
Exception Handing
Chapter 1
C++ for Java Programmers
9
The Language Java
Designed by James Gosling
Add simplicity and security to syntax of C++
Trade off between simplicity and efficiency
Preserve consistent behavior on all platforms
Add run-time checks for safety
Heap based memory model, garbage
collection
Simpler OO model
Chapter 1
C++ for Java Programmers
10
Comparisons are dangerous
Can you say one is better than the
other?
No. Each has slightly different use,
different outlook
Given right situation, either can be
made to look better than the other
Chapter 1
C++ for Java Programmers
11