120 likes | 182 Views
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
E N D
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 C++ for Java Programmers
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 C++ for Java Programmers
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 C++ for Java Programmers
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()); C++ for Java Programmers
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 C++ for Java Programmers
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++ C++ for Java Programmers
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 C++ for Java Programmers
And new features • C++ added a number of new features to C • Classes, inheritance • Virtual functions • Templates • Exception Handing C++ for Java Programmers
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 C++ for Java Programmers
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 C++ for Java Programmers