• Tidak ada hasil yang ditemukan

THE C++ PROGRAMMING LANGUAGE

N/A
N/A
Chip Swift

Academic year: 2023

Membagikan "THE C++ PROGRAMMING LANGUAGE "

Copied!
1366
0
0

Teks penuh

In addition to the people mentioned in the acknowledgments sections of previous issues, I would like to thank Pete Becker, Hans-J. Brian Kernighan, for hosting me for part of the sabbatical that gave me time to write this book. Andy Hopper, for hosting me for part of the sabbatical that gave me time to write this book.

About a third of the information in the second edition of this book came from the first. The primary purpose of this book is to help the reader understand how the features of C++ are provided. The main purpose of the language enhancements of the past six years has been to improve C++.

Implementations of C++ exist from some of the most modest microcomputers to the largest supercomputers and for almost all operating systems. This edition gives a greater emphasis on tutorial aspects than the first edition of this book.

Introductory Material

A Tour of C++: The Basics

A Tour of C++: Abstraction Mechanisms

You did nothing without first considering how it would affect Marcus Cocoza's happiness and reputation. From now on you must be more than one, many people, as many as you can think of. A pure reference can only be used by someone familiar with all fundamental concepts and techniques.

If you know most of the concepts and techniques, you can approach them on a chapter-by-chapter or even section-by-section basis. If not, you can start from the beginning, but try not to get bogged down in details. Making the parts of the book relatively self-contained means some repetition, but the repetition also serves as a review for people reading the book linearly.

This chapter, Chapter 1, provides an overview of this book, some pointers on how to use it, and some background information about C++ and its uses. We invite you to skim through it, read what you find interesting, and come back to it after reading other parts of the book.

A Tour of C++: Containers and Algorithms

A Tour of C++: Concurrency and Utilities

However, please do not assume that "the old ways" are better or simpler just because they are old and familiar. This has surprised some who - correctly - point out that C++ is not the smallest or cleanest language ever designed. Such code is also very frustrating to write, because every line of code and every compiler error message reminds the programmer that the language being used is different from "the old language". You can write in the style of Fortran, C, Lisp, Java, etc., in any language, but doing so is neither pleasant nor economical in a language with a different philosophy.

Note the difference from the C standard library (eg = instead of strcpy() for copying and == instead of strcmp() for comparison). I think we (the committee) did the right thing with ``concepts'' for C++11, but this feature was really ``the one that got away.'' This is currently a field of active research and design [Sutton, 2011] [Stroustrup, 2012a]. What used to be called systems programming is widely found in embedded systems, so it is not surprising to find massive use of C++ in demanding embedded systems projects, including computed tomography (CAT scanners), flight control software (e.g. Lockheed-Martin) ), rocket control, marine engines (e.g. control of the world's largest marine diesel engines from MAN), automotive software (e.g. BMW) and wind turbine control (e.g. Vesta).

That is, the C++ standard library can be implemented in C++ itself (and is with very minor uses of machine code for things like thread context switching). To match the <>operator (‘‘comes from’’) is used for input; cin is the standard input stream. When we don't have an object to point to or if we need to represent the idea of ​​"no object available". e.g. for an end of a list), we give the pointer the valuenullptr(''the null pointer'').

Similarly, we have not provided a mechanism to "return" the set of doubles newly acquired; §3.2.1.2 shows you how to use a destructor to do that elegantly. If we had formally specified Vector's subscript operator, we would have said something like 'the index must be in the range [0:size())', which is essentially what we tested in our []() operator. The C standard library (with very minor changes to minimize type system violations); see chapter 43.

This reads Entries from the standard input in phone_book until either the end of input (eg the end of a file) is reached or the input operation encounters a format error. In the examples above, the algorithms have simply "built in" the action to perform for each element of a sequence. Concurrency – performing multiple tasks simultaneously – is widely used to improve throughput (by using multiple processors for a single computation) or to improve responsiveness (by allowing one part of a program to progress while another waits an answer ).

An important point about futures and promises is that they allow values ​​to be passed between two tasks without explicitly using a lock; The "system" performs the transfer efficiently. The standard library supports a family of complex number types along complex lines. Examples of distributions are uniform_int_distribution (where all integers produced are equally likely), normal_distribution ("bell curve"), and exponential_distribution (exponential growth).

The standard-library bind() function makes a function object that will call its first argument (here, one_to_six) passing its second argument (here, re) as an argument (§33.5.1).

Basic Facilities 133

  • Pointers, Arrays, and References
  • Structures, Unions, and Enumerations
  • Statements
  • Expressions
  • Select Operations
  • Functions
  • Exception Handling
  • Namespaces
  • Source Files and Programs

We need a notation for characters that do not have a convenient character representation (such as newline and ``character with value 17''). A common use of booleans as the type of the result of a function that tests a condition (a predicate). A literal character can be implicitly converted to its integer value in the character set of the machine on which the C++ program will run.

Some of the aspects of C++'s fundamental types, such as the size of an int, are implementation-defined (§6.1). People who claim they don't care about portability usually do so because they only use a single system and feel they can afford the attitude that "the language is what my compiler implements." This is a narrow and short-sighted view. Note that the language and the standard library use lowercase letters for types; this can be seen as a hint that they are part of the standard.

A declaration sets a name in a scope; that is, a name can only be used in a specific part of the program text. If you do - and it was unfortunately common - the situation is more complicated. The obvious answer is that the element type of the sum is the type of the sum of the elements.

Roughly, rvalue means ''a value that is not an lvalue,'' such as a temporary value (eg the value returned by a function). The other alternatives are prvalue(''pure rvalue''), glvalue(''generalized lvalue''), and xvalue(''x'' for ''extraordinary'' or ''expert only''; the suggestions for the meaning of this ''x'' was quite imaginative). In general, it is not safe to use a pointer that has been converted (''cast'') to a type that is different from the type of the object being referenced.

For type T, T [size] type is "an array of elements of size T." Elements are indexed from 0. This is the way C stores arrays, so a zero-terminated array is often called aC-style array. The result of applying the arithmetic operators +, −, ++, or −− to pointers depends on the type of object you are pointing to.

Abstraction Mechanisms 447

Construction, Cleanup, Copy, and Move

The standard library added several components, including threads, locks, and most of the components from the 2003 Technical Report. The technical reports to the committee [WG21] and my C++11 FAQ [Stroustrup,2010a] provide many of the names. In the context of the C++ standard library, an algorithm is a function template that operates on sequences of elements.

Most of the advantages of the built-in array and few of the disadvantages can be obtained by using the standard library container array.

Overloading

Special Operators

Derived Classes

Class Hierarchies

Run-Time Type Information

Templates

Generic Programming

Specialization

Instantiation

Templates and Hierarchies

Metaprogramming

A Matrix Design

The Standard Library 857

STL Containers

STL Algorithms

STL Iterators

Memory and Resources

Utilities

Strings

Regular Expressions

I/O Streams

Locales

Numerics

Concurrency

Threads and Tasks

The C Standard Library

Compatibility

Referensi

Dokumen terkait

Where those designations appear in this book, and Addison-Wesley was aware of a trademark claim, the designations have been printed in initial capital letters or all capital

The primary aim of the language extensions made in the last six years has been to enhance C++ as a language for data abstraction and object-oriented programming in general and

Classes provide data hiding, guaranteed initialization of data, implicit type conversion for user-defined types, dynamic typing, user-controlled memory management, and mechanisms

This framework, conventionally called the STL [Stepanov,1994], is extensible in the sense that users can easily pro- vide containers and algorithms in addition to the ones provided

Contents Chapter 0 Getting started 0.1 Comments 0.2 #include 0.3 The main function 0.4 Curly braces 0.5 Using the standard library for output 0.6 The return statement 0.7 A