C++, An Overview

C++, An Overview
There are two primary aspects to the programs we write
            1. A collection of algorithms (tha t is, the programmed instructions to
               solve a particular task)
            2. A collection of data against which the algorithms are run to
               provide each unique solution
These two primary program aspects, algorithms and data, have remained
invariant throughout the short history of computing. What has evolved is the
relationship between them. This relationship is spoken of as a programming
paradigm.
In the procedural programming paradigm, a problem is directly modeled by a
set of algorithms. A check-out/check-in system for loan materials of a public
library, such as books, videos, and so on, is represented as a series of procedures,
the two central procedures being the checking-out and checking-in of library
materials. The data is stored separately, accessed either at a global location or by
being passed into the procedures. Three prominent procedural languages are
FORTRAN, C, and Pascal. C++ also supports procedural programming.
Individual procedures, such as check_in(), check_out(), overdue(), fine(), and so
on, are referred to as functions. Part III, Procedural-Based Programming,
focuses on the support C++ provides for the procedural programming paradigm,
with an emphasis on functions, function templates, and generic algorithms.
In the 1970s, the focus of program design shifted from the procedural paradigm
to that of abstract data types (now generally referred to as object-based
programming). In this paradigm, a problem is modeled by a set of data
abstractions. In C++ we refer to these abstractions as classes. Our library
check-out system, for example, under this paradigm is represented as the
interaction between object instances of classes such as Book, Borrower, DueDate
(an aspect of Time), and the inevitable Fine (an aspect of Money), representing
the library abstractions. The algorithms associated with each class are referred
to as the class's public interface. The data is privately stored within each object;
access of the data is hidden from the general program. Three programming
languages that support the abstract data type paradigm are CLU, Ada, and
Modula-2. Part IV, Object-Based Programming, illustrates and discusses the
support C++ provides for the abstract data type programming paradigm.
Object-oriented programming extends abstract data types through the
mechanisms of inheritance (a "reuse" of an existing implementation) and
dynamic binding (a reuse of an existing public interface). Special type/subtype
relationships between previously independent types are now provided. A book,
                                         - 16 -
C++ Primer 3rd Edition – e类出版物 [http://www.epubcn.com] 制作出品
videotape, recording, and children's puppet are each a kind of library material,
although each has its own check-out and check-in policy. The shared public
interface and private data are placed in an abstract LibraryMaterial class. Each
specific library material class inherits the shared behavior from the
LibraryMaterial abstract class and need provide only the algorithms and data
that support its behavior. Three prominent languages supporting the
object-oriented paradigm are Simula, Smalltalk, and Java. Part V,
Object-Oriented Programming, focuses on the support C++ provides for the
object-oriented programming paradigm.
C++ is a multiparadigm language. Although we think of it primarily as an
object-oriented language, it also provides support for procedural and
object-based programming. The benefit is that we are able to provide a solution
best suited to the problem — in practice, no one paradigm represents a best
solution to every problem. The drawback is that it makes for a larger and more
complicated language.
In Part I, we present a quick tour of the entire C++ language. One reason for this
is to provide a first introduction to the language features so that we can more
freely reference aspects of the language before we fully treat them. For example,
we don't look at classes in detail until Chapter 13, but if we waited until then to
mention classes we would end up presenting a great many unrepresentative and
largely irrelevant program examples.
A second reason for providing a breadth-first tour of the language is aesthetic.
Unless you are exposed to the beauty and complexity of a Beethoven sonata or
the exhilaration of a Scott Joplin rag, it is easy to become alternately impatient
and bored with the apparent irrelevant detail of sharps, flats, octaves, and
chords; but until those details are mastere d, making music remains largely
beyond our means. Much the same holds true with programming. Stepping
through the maze of operator precedence or rules governing the standard
arithmetic conversions is a necessary but necessarily tedious foundation to
mastering programming in C++.
Chapter 1 provides a first introduction to the basic elements of the language: the
built-in data types, variables, expressions, statements, and functions. It looks at a
minimum legal C++ program, discusses the process of compiling our programs,
briefly walks through the preprocessor, and takes a first look at support for
input and output. It presents a number of simple but complete C++ programs
that the reader is encouraged to compile and execute.
In Chapter 2, we walk through a procedural program, an object-based program,
and then an object-oriented program implementation of an array — that is, a
numbered collection of elements of the same type. We then compare our array
abstraction with the C++ standard library vector class and take a first look at
                                           - 17 -
C++ Primer 3rd Edition – e类出版物 [http://www.epubcn.com] 制作出品
the standard library generic algorithms. Along the way, we motivate and take a
first peek at C++'s support for exception handling, templates, and namespaces.
In effect, the entire language is introduced, although many of the details are
deferred until later in the text.
Some readers may find portions of Chapter 2 rough going. Material is presented
without the full explanation normally expected of a primer (the explanation is
provided in subsequent chapters). If you should find yourself feeling
overwhelmed or impatient at the level of detail, we recommend that you skim
through or skip that portion, returning to it later when the material is more
familiar. In Chapter 3, we begin the more traditional narrative pace, and the
reader uncomfortable with Chapter 2 is recommended to start there.

                                                      C++概述
    我们编写的程序由两个主要方面组成
    1 算法的集合           就是将指令组织成程序来解决某个特定的问题
    2   数据的集合         算法在这些数据上操作 以提供问题的解决方案
    纵观短暂的计算机发展史                这两个主要方面 算法和数据               一直保持不变         发展演化的
                     就是所谓的程序设计方法 programming paradigm
是它们之间的关系
    在过程化程序设计方法 procedural programming 中 一个问题可直接由一组算法来建
                                            check out/check in
立模型      例如     公共图书馆的资料借阅/登记                                  系统是由一系列过程表现
         其中两个主要的过程是资料的借阅和登记                      这些数据被独立存储起来              我们既可以
出来的
                                 或者把数据传递给过程以便它能够访问这些数据 Fortran
在某个全局位置上访问这些数据
C 和 Pascal 是三种著名的过程语言 C++也支持过程化程序设计 单独的过程 如 check_in()
check_out()   over_due()  fine()等等                  第三篇将集中讨论 C++对过程化程序
                                     都被称为函数
设计方法的支持           尤其将重点讨论函数 函数模板和通用算法
    在 20 世纪 70 年代        程序设计的焦点从过程化程序设计方法转移到了抽象数据类型
  abstract data type  简写为 ADT                     现在通常称之为基于对象(object based
                                   的程序设计上
的程序设计         在基于对象的程序设计方法中 我们通过一组数据抽象来建立问题的模型                                 在
C++中我们把这些抽象称为类 class                 例如     在这种方法下        图书馆资料借阅          登记系统就
由类的对象实例           比如书     借阅者      还书时间 罚款等          之间的相互作用表现出来            以此表
                         与每个类相关的算法被称为该类的公有接口 public interface
示出图书馆的抽象概念                                                                     数
                                   对数据的访问应与一般的程序代码隔离开来 CLU                     Ada
据以私有形式被存储在每个对象中
和 Modula-2 是三种支持抽象数据类型的程序设计语言 第四篇将说明和讨论 C++对抽象数据
类型程序设计方法的支持
    面向对象的程序设计方法通过继承 inheritance 机制和动态绑定 dynamic binding 机
制扩展了抽象数据类型 继承机制是对现有实现代码的重用 动态绑定是指对现有的公有接
口的重用       以前独立的类型现在有了类型/子类型的特定关系                         一本书     一盒录像带      一段录
音   甚至孩子的宠物           尽管它们有各自的借阅/登记方式                 但都可以成为图书馆的收藏资料
                                                           LibraryMaterial 中
共享的公有接口和私有的数据都放在一个抽象类                          图书馆资料                          每个特
殊的图书馆资料类都从 LibraryMaterial 抽象类继承共享的行为 它们只需要提供与自身行为相
                  Simula  Smalltalk 和 Java 是三种支持面向对象程序设计方法的著名语言
关的算法和数据
第五篇将集中讨论 C++对面向对象程序设计方法的支持
    C++是一种支持多种程序设计方法的语言                      虽然我们主要把它当作面向对象的语言                 但
实际上它也提供对过程化的和基于对象的程序设计方法的支持                                这样做的好处是对每个问题
都能够提供最合适的解决方案                  事实上 没有一种程序设计方法能够对所有的问题都提供最
好的解决方案          这样做带来的缺点是使得语言过于庞大 复杂
 2                                              第一篇 C++概述
   第一篇将对整个 C++进行快速浏览    这样做的一个原因是       它可以提供对语言特性的介
                                                   直到第 13
绍  以便我们在完全面对这些特性之前      可以自由地引用语言的各个部分         例如
章我们才会详细介绍类     但如果到那时候才提起类 那么在此之前我们将不得不使用很多非
典型的    不恰当的程序例子
   提供快速浏览的第二个原因是从美学的角度出发 除非首先让你领略到贝多芬交响曲的
美丽与博大    否则 无关联的升半音 降半音        八度音符  和弦   等一定会让你厌烦      但
                                         精通 C++程序设计的基础
是 只有掌握了这些细节      才有可能创作音乐 程序设计也一样
是首先要穿过操作符优先级或标准算术转换规则的迷宫            这样做既是必要的     也是非常枯燥

   第 1 章将首先介绍 C++语言的基本元素     包括内置数据类型    变量  表达式   语句   函
                 并且是合法的 C++程序
数  它将通过一个最小的                     来讨论程序的编译过程      预处理  以及
C++对输入           这一章将给出多个简单但完整的 C++程序
         输出的支持                             鼓励读者亲自编译并执
行这些程序
   第 2 章我们将浏览一个过程化程序 一个基于对象的程序和一个面向对象的程序              它们
都实现了一个数组 一个由相同类型的元素组成的有限元素的集合              然后   我们将这些程
序中的数组抽象与 C++标准库中的向量     vector  类进行比较  同时也将首次介绍标准库中
                  我们还将介绍 C++对异常处理
的通用算法 沿着这条路线                          模板  名字空间的支持     实际
   这一章对整个 C++语言作了大致的介绍
上                            细节部分将在以后各章节中详细介绍
   部分读者可能会感觉第 2 章很难理解     给出的许多资料没有初学者所期望的完整说明            这
                   如果你对某些细节部分感到吃力或失去耐心 建议略读或跳
些细节在以后的章节中讨论
                     再回头重读这些内容 第 3 章将以传统的叙述方式进行
过它们    等到熟悉这些资料以后
建议对第 2 章不够适应的读者 从第 3 章开始


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值