Java程序员的C++教程

本文对比了C++与Java的语法及特性,包括编译原理、安全性、多线程支持等方面。并列举了C++相较于Java的独特优势,如模板、操作符重载等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写在前面的话

  本系列博客主要翻译自《C++ for Java Programmers》,本人是个对Java有些了解,但对C++几乎一无所知的人。我也是在边翻译边学习C++,不免会出现一些错误,希望大家不吝指正!

组织

  1. 0章,C++概览。
  2. 1章,C++中的基本表示式和语句,与Java中类似。
  3. 2章,函数、数组、字符串、参数传递
  4. 3章,介绍指针和引用变量,着重介绍C++程序员会遇到的指针陷阱。
  5. 4章,介绍基本的类。
  6. 5章,操作符重载(java中没有)
  7. 6章,继承
  8. 7章,模板(java中没有)
  9. 8章,异常
  10. 9章,I/O
  11. 10章,标准模板库
  12. 11章,基础数组和字符串
  13. 12章,C style C++
  14. 13章,JNI

前言

  从语法上看,C++和Java是相似的。Java中的很多基本结构例如基本表达式、条件、循环是完全相同的,除了一些细微的技术细节。因为每个新的保留字都会破坏现有的语言,所以C++社区宁愿过度使用现有的关键字如conststatic也不愿增加新的保留字。因此,在Java中有throwthrows,而在C++中抛出一个异常和列出一组异常都使用throw
  没有运行时边界检测,没有动态分配,没有GC。
  C++在不断地进化。第一个广泛使用的版本支持类和继承。之后加入了模板。在这以后才加入异常。

High Level Differences

  1. 编译型vs.解释性
    Java包含一个编译器和一个虚拟机。编译器生成Java字节码,然后字节码在运行时解释执行。因此,不仅Java源码是可移植的,理论上编译后的字节码也是可移植的并且可以在任何平台上运行。
    另一方面,C++编译器生成本地代码。C++语言规范并未对本地代码做出规定,所以编译后的代码不仅在不同类型的机器间不可移植,由同一台机器的不同编译器生成的程序相互间也是不兼容的。
  2. 安全性和鲁棒性
    Java非常关心代码编译,不允许不安全的代码通过编译,并且一遇到错误就会抛出异常。而C++在这方面表现欠缺。C++存在很多在纯Java中永远不会出现的问题。突出的四点如下:

    • 存在一个指向已回收堆内存的指针或引用。
    • 不进行数组索引检测。
    • C++的类型转换允许类型冲突
    • 在Java中,所有变量在使用前必须赋值。
  3. 多线程
    C++并没有把多线程作为语言的一部分去实现。相反,程序员必须使用平台特定例程库。

使用C++的10个理由
  1. C++ 仍被广泛使用
  2. 模板
  3. 操作符重载
  4. 标准模板库
    与Java Collections的API相比,C++的STL具有一些优越性。
  5. 资源的自动回收
    尽管Java提供GC,这是一个出色的特性,但是很难对其他的资源管理进行控制。例如Java程序员必须记住在及时关掉文件和数据库链接。在C++中,每个类需要提供一个特殊的方法,成为destructor,当对象不处于活跃态时被自动调用。
  6. 条件编译
    在C++中,程序员可以编写一些只有满足特定条件才会编译的程序。这可以通过预处理器来实现。
  7. Accessor 和 Mutator的区别
    在Java中,当把引用变量标记成final时,意味着该引用变量不能被修改。标记变量所引用的对象不能被修改并不容易。C++提供语法支持来区别accessors和mutators,当把对象标记成const时,仅能对该对象进行访问。
  8. 多继承实现
    Java不支持多继承,因为众所周知使用多继承很麻烦。但是,有时使用多继承非常有用。
  9. 空间效率
  10. 私有继承
    Java仅支持共有继承,这通过使用extends语句来实现。C++支持私有继承,这在改变接口可见性以及实现适配器模式时是有用的。
Book Description Written for the moderately experienced Java programmer, this book builds on readers¿ existing knowledge of object-oriented programming and covers all important aspects of Standard C++—emphasizing more lower-level C-style details later in the presentation. Chapter topics include philosophy of C++, simplest C++, pointers and reference variables, object-based programming: classes, operator overloading, object-oriented programming: inheritance, templates, abnormal control flow, input and output, collections: the standard template library, primitive arrays and strings, C-style C++, and using Java and C++: the JNI. For new C++ programmers converted from Java. For experienced Java programmers and students who require the skills of C++ programming, best-selling author Mark Allen Weiss bridges the gap. He efficiently presents the complex C++ language in this well-designed tutorial/reference that both students and seasoned programmers will appreciate. The book is ideal as a primary text for intermediate C++ courses, as a supplemental no-nonsense reference for other courses, or for independent learning by professionals. C++ for Java Programmers is a concise, well-written text that provides authoritative and up-to-date coverage of key features and details of C++, with a special focus on how C++ compares to Java. The book's approach shows knowledgeable students or professionals how to grasp the complexities of C++ and harness its power by mutually addressing the benefits and the pitfalls of the two languages. By highlighting the features and comparative elements of each language, and building on the reader's existing knowledge of object-oriented programming, C++ for Java Programmers enables users to master the essentials of C++ quickly and thoroughly. Key Features Includes insightful comparisons of the two programming languages throughout the text and points out the subtleties of C++ Succinctly covers the pertinent highlights of STL (Standard Template Library) and the most effective use of templates Explains the use of the powerful JNI (Java Native Interface) for combining Java and C++ Includes a summary of key C++ features and issues with each chapter Provides extensive treatment of C details the programmer is likely to encounter in C++ Companion Website for complete online source code at: http://www.prenhall.com/weiss Available Instructors Resource CD-ROM Product Details Paperback: 304 pages Publisher: Prentice Hall; 1 edition (November 7, 2003) Language: English ISBN-10: 013919424X ISBN-13: 978-0139194245 Product Dimensions: 9.5 x 6.8 x 0.6 inches
C++ for Programmers: Deitel Developer Series by Paul Deitel, Harvey Deitel release type: eBook (.chm) release size: 4.80 MB Publisher: Prentice Hall PTR Pub Date: February 2, 2009 Print ISBN-10: 0137001304 Print ISBN-13: 978-0137001309 PRACTICAL, EXAMPLE-RICH COVERAGE OF: Classes, Objects, Encapsulation, Inheritance, Polymorphism Integrated OOP Case Studies: Time, GradeBook, Employee Industrial-Strength, 95-Page OOD/UML® 2 ATM Case Study Standard Template Library (STL): Containers, Iterators and Algorithms I/O, Types, Control Statements, Functions Arrays, Vectors, Pointers, References String Class, C-Style Strings Operator Overloading, Templates Exception Handling, Files Bit and Character Manipulation Boost Libraries and the Future of C++ GNU™ and Visual C++® Debuggers And more… VISIT WWW.DEITEL.COM For information on Deitel® Dive-Into® Series corporate training courses offered at customer sites worldwide (or write to [email protected]) Download code examples Check out the growing list of programming, Web 2.0 and software-related Resource Centers To receive updates for this book, subscribe to the free DEITEL® BUZZ ONLINE e-mail newsletter at www.deitel.com/newsletter/subscribe.html Read archived issues of the DEITEL® BUZZ ONLINE The professional programmer’s DEITEL® guide to C++ and object-oriented application development Written for programmers with a background in high-level language programming, this book applies the Deitel signature live-code approach to teaching programming and explores the C++ language and C++ Standard Libraries in depth. The book presents the concepts in the context of fully tested programs, complete with syntax shading, code highlighting, code walkthroughs and program outputs. The book features 240 C++ applications with over 15,000 lines of proven C++ code, and hundreds of tips that will help you build robust applications. Start with an introduction to C++ using an early classes and objects approach, then rapidly move on to more advanced topics, including templates, exception handling, the Standard Template Library (STL) and selected features from the Boost libraries. You’ll enjoy the Deitels’ classic treatment of object-oriented programming and the OOD/UML® 2 ATM case study, including a complete C++ implementation. When you’re finished, you’ll have everything you need to build object-oriented C++ applications.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值