没有合适的资源?快使用搜索试试~ 我知道了~
首页快速掌握C++精华:实战导向的编程入门指南
快速掌握C++精华:实战导向的编程入门指南
需积分: 9 0 下载量 22 浏览量
更新于2024-07-17
收藏 1.49MB PDF 举报
《Essential C++》是一本由Stanley B. Lippman所著的实用教程,专为那些时间紧张但又希望迅速掌握C++编程的程序员设计。该书出版于2002年9月,由Addison Wesley出版社发行,ISBN号为0-201-48518-4,共416页。对于初学者来说,这本书提供了一个快速熟悉C++的途径,作者以简洁明快的方式提炼了C++编程的核心要素,使读者能在短时间内具备编写实际程序的能力。 斯坦利·利普曼将复杂的C++主题提炼到最核心的部分,重点关注程序员在日常工作中最可能遇到的元素。《Essential C++》的内容分为四个部分:过程风格、泛型风格、基于对象风格和面向对象风格。这种结构确保了学习者能够在逐步解决复杂问题的过程中逐渐掌握语言的不同范式。 书中采用了递进式的学习方式,通过一系列越来越复杂的编程问题来展示C++的功能和结构。作者将语言特性作为解决问题的关键工具,帮助读者理解这些特性的目的和基本理念。这样做的目的是让学习者不仅能够掌握语法和编程技巧,还能实际应用到解决现实世界中的编程挑战。 对于那些时间有限的专业程序员来说,《Essential C++》是一条高效的“速成”之路,旨在提升他们在工作中的C++技能。通过本书,读者可以在有限的时间内掌握C++编程的基础,从而提高工作效率并更好地应对实际项目中的需求。 《Essential C++》是一本适合初级到中级程序员的C++入门指南,它的目标是帮助读者快速掌握必备的C++知识,以便在日常工作中高效地运用这门强大的编程语言。无论是为了初次接触C++还是希望提升现有技能,这本书都提供了实用且结构清晰的学习路径。
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/11181730/bg10.jpg)
7
A class is a programmer-defined data type. C++ also provides a set of built-in data types: Boolean, integer,
floating point, and character. A keyword is associated with each one to allow us to specify the data type.
For example, to store the value entered by the user, we define an integer data object:
int usr_val;
int
is a language keyword identifying usr_val as a data object of integer type. Both the number of
guesses a user makes and the number of correct guesses are also integer objects. The difference here is that
we wish to set both of them to an initial value of 0. We can define each on a separate line:
int num_tries = 0;
int num_right = 0;
Or we can define them in a single comma-separated declaration statement:
int num_tries = 0, num_right = 0;
In general, it is a good idea to initialize data objects even if the value simply indicates that the object has
no useful value as yet. I didn't initialize
usr_val because its value is set directly from the user's input
before the program makes any use of the object.
An alternative initialization syntax, called a constructor syntax, is
int num_tries(0);
I know. Why are there two initialization syntaxes? And, worse, why am I telling you this now? Well, let's
see whether the following explanation satisfies either or both questions.
The use of the assignment operator (
=) for initialization is inherited from the C language. It works well
with the data objects of the built-in types and for class objects that can be initialized with a single value,
such as the string class:
string sequence_name = "Fibonacci";
It does not work well with class objects that require multiple initial values, such as the standard library
complex number class, which can take two initial values: one for its real component and one for its
imaginary component. The alternative constructor initialization syntax was introduced to handle multiple
value initialization:
#include <complex>
complex<double> purei(0, 7);
The strange bracket notation following complex indicates that the complex class is a template class. We'll
see a great deal more of template classes throughout the book. A template class allows us to define a class
without having to specify the data type of one or all of its members.
The complex number class, for example, contains two member data objects. One member represents the
real component of the number. The second member represents the imaginary component of the number.
These members need to be floating point data types, but which ones? C++ generously supports three
floating point size types: single precision, represented by the keyword
float; double precision,
represented by the keyword
double; and extended precision, represented by the two keywords long
double.
5.15
![](https://csdnimg.cn/release/download_crawler_static/11181730/bg11.jpg)
8
The template class mechanism allows the programmer to defer deciding on the data type to use for a
template class. It allows the programmer to insert a placeholder that is later bound to an actual data type. In
the preceding example, the user chose to bind the data type of the complex class members to
double.
I know, this probably raises scads more questions than it answers. However, it is because of templates that
C++ supports two initialization syntaxes for the built-in data types. When the built-in data types and
programmer-defined class types had separate initialization syntaxes, it was not possible to write a template
that supported both built-in and class data types. Making the syntax uniform simplified template design.
Unfortunately, explaining the syntax seems to have become more complicated!
The user's running score must to be a floating point value because it may be some percentage. We'll define
it to be of type
double:
double usr_score = 0.0;
We also need to keep track of the user's yes/no responses: Make another try? Try another sequence?
We can store the user's response in a character data object:
char usr_more;
cout << "Try another sequence? Y/N? ";
cin >> usr_more;
The char keyword represents a character type. A character marked by a pair of single quotations
represents a character literal:
'a', '7', ';', and so on. Some special built-in character literals are the
following (they are sometimes called escape sequences):
'\n' newline
'\t' tab
'\0' null
'\'' single quote
'\"' double quote
'\\' backslash
For example, to generate a newline and then tab before printing the user's name, we might write
cout << '\n' << '\t' << user_name;
Alternatively, we can concatentate single characters into a string:
cout << "\n\t" << user_name;
Typically, we use these special characters within string literals. For example, to represent a literal file path
under Windows, we need to escape the backslash:
"F:\\essential\\programs\\chapter1\\ch1_main.cpp";
C++ supports a built-in Boolean data type to represent true/false values. In our program, for example, we
can define a Boolean object to control whether to display the next numeric sequence:
bool go_for_it = true;
A Boolean object is specified with the bool keyword. It can hold one of two literal values, either true or
false.
![](https://csdnimg.cn/release/download_crawler_static/11181730/bg12.jpg)
9
All the data objects defined so far modified during the course of our program. go_for_it, for example,
eventually gets set to
false. usr_score is potentially updated with each user guess.
Sometimes, however, we need an object to represent a constant value: the maximum number of guesses to
allow a user, for example, or the value of pi. The objects holding these values should not be modified
during the course of our program. How can we prevent the accidental modification of such objects? We
can enlist the aid of the language by declaring these objects as
const:
const int max_tries = 3;
const double pi = 3.14159;
A const object cannot be modified from its initial value. Any attempt to assign a value to a const object
results in a compile-time error. For example:
max_tries = 42; // error: const object
1.3 Writing Expressions
The built-in data types are supported by a collection of arithmetic, relational, logical, and compound
assignment operators. The arithmetic operators are unsurprising except for integer division and the
remainder operator:
// Arithmetic Operators
+ addition a + b
- subtraction a - b
* multiplication a * b
/ division a / b
% remainder a % b
The division of two integer values yields a whole number. Any remainder is truncated; there is no
rounding. The remainder is accessed using the
% operator:
5 / 3 evaluates to 1 while 5 % 3 evaluates to 2
5 / 4 evaluates to 1 while 5 % 4 evaluates to 1
5 / 5 evaluates to 1 while 5 % 5 evaluates to 0
When might we actually use the remainder operator? Imagine that we want to print no more than eight
strings on a line. If the number of words on the line is less than eight, we output a blank space following
the word. If the string is the eighth word on the line, we output a newline. Here is our implementation:
const int line_size = 8;
int cnt = 1;
// these statements are executed many times, with
// a_string representing a different value each time
// and cnt growing by one with each execution ...
cout << a_string
<< (cnt % line_size ? ' ' : '\n');
The parenthetical expression following the output operator likely makes no sense to you unless you are
already familiar with the conditional operator (
?:). The result of the expression is to output either a space
or a newline character depending on whether the remainder operator evaluates to a zero or a nonzero value.
Let's see what sense we can make of it.
![](https://csdnimg.cn/release/download_crawler_static/11181730/bg13.jpg)
10
The expression
cnt % line_size
evaluates to zero whenever cnt is a multiple of line_size; otherwise, it evaluates to a nonzero value.
Where does that get us? The conditional operator takes the following general form:
expr
? execute_if_expr_is_true
: execute_if_expr_is_false;
If expr evaluates to true, the expression following the question mark is evaluated. If expr evaluates to
false, the expression following the colon is evaluated. In our case, the evaluation is to feed either a space or
a newline character to the output operator.
A conditional expression is treated as evaluating to false if its value is zero. Any nonzero value is treated
as true. In this example, whenever
cnt is not a multiple of eight, the result is nonzero, the true branch of
the conditional operator is evaluated, and a space is printed.
A compound assignment operator provides a shorthand notation for applying an arithmetic operation on
the object to be assigned. For example, rather than write
cnt = cnt + 2;
a C++ programmer typically writes
cnt += 2; // add 2 to the current value of cnt
A compound assignment operator is associated with each arithmetic operator: +=, -=, *=, /=, and %=.
When an object is being added to or subtracted by 1, the C++ programmer uses the increment and
decrement operators:
cnt++; // add 1 to the current value of cnt
cnt--; // subtract 1 from the current value of cnt
There is a prefix and postfix version of the increment and decrement operators. In the prefix application,
the object is either incremented (or decremented) by 1 before the object's value is accessed:
int tries = 0;
cout << "Are you ready for try #"
<< ++tries << "?\n";
In this example, tries is incremented by 1 before its value is printed. In the postfix application, the
object's value is first used in the expression, and then incremented (or decremented) by 1:
int tries = 1;
cout << "Are you ready for try #"
<< tries++ << "?\n";
In this example, the value of tries is printed before it is incremented by 1. In both examples, the value 1
is printed.
Each of the relational operators evaluates to either true or false. They consist of the following six operators:
![](https://csdnimg.cn/release/download_crawler_static/11181730/bg14.jpg)
11
== equality a == b
!= inequality a != b
< less than a < b
> greater than a > b
<= less than or equal a <= b
>= greater than or equal a >= b
Here is how we might use the equality operator to test the user's response:
bool usr_more = true;
char usr_rsp;
// ask the user if she wishes to continue
// read the response into usr_rsp
if (usr_rsp == 'N')
usr_more = false;
The if statement conditionally executes the statement following it if the expression within parentheses
evaluates to true. In this example,
usr_more is set to false if usr_rsp is equal to 'N'. If usr_rsp is
not equal to
'N', nothing is done. The reverse logic using the inequality operator looks like this:
if (usr_rsp != 'Y')
usr_more = false;
The problem with testing usr_rsp only for 'N' is that the user might enter a lowercase 'n'. We must
recognize both. One strategy is to add an
else clause:
if (usr_rsp == 'N')
usr_more = false;
else
if (usr_rsp == 'n')
usr_more = false;
If usr_rsp is equal to 'N', usr_more is set to false and nothing more is done. If it is not equal to 'N',
the else clause is evaluated. If usr_rsp is equal to 'n', usr_more is set to false. If usr_rsp is not
equal to either,
usr_more is not assigned.
A common beginner programmer error is to use the assignment operator for the equality test, as in the
following:
// oops this assigns usr_rsp the literal character 'N'
// and therefore always evaluates as true
if (usr_rsp = 'N')
// ...
The logical OR operator (||) provides an alternative way of testing the truth condition of multiple
expressions:
if (usr_rsp == 'N' || usr_rsp == 'n')
usr_more = false;
The logical OR operator evaluates as true if either of its expressions evaluates as true. The leftmost
expression is evaluated first. If it is true, the remaining expression is not evaluated. In our example,
usr_rsp is tested for equality with 'n' only if it is not equal to 'N'.
The logical
AND operator (&&) evaluates as true only if both its expressions evaluate as true. For example,
剩余243页未读,继续阅读
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/default.jpg!1)
weixin_39273207
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
最新资源
- 新型矿用本安直流稳压电源设计:双重保护电路
- 煤矿掘进工作面安全因素研究:结构方程模型
- 利用同位素位移探测原子内部新型力
- 钻锚机钻臂动力学仿真分析与优化
- 钻孔成像技术在巷道松动圈检测与支护设计中的应用
- 极化与非极化ep碰撞中J/ψ的Sivers与cos2φ效应:理论分析与COMPASS验证
- 新疆矿区1200m深孔钻探关键技术与实践
- 建筑行业事故预防:综合动态事故致因理论的应用
- 北斗卫星监测系统在电网塔形实时监控中的应用
- 煤层气羽状水平井数值模拟:交替隐式算法的应用
- 开放字符串T对偶与双空间坐标变换
- 煤矿瓦斯抽采半径测定新方法——瓦斯储量法
- 大倾角大采高工作面设备稳定与安全控制关键技术
- 超标违规背景下的热波动影响分析
- 中国煤矿选煤设计进展与挑战:历史、现状与未来发展
- 反演技术与RBF神经网络在移动机器人控制中的应用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)