《C++ Without Fear》学习笔记

本文是基于Brian Overland的《C++ Without Fear》的学习笔记,涵盖了变量初始化、类型转换、流程控制、表达式与语句、函数使用等方面的基础知识。强调了良好的编程风格,如变量初始化、使用cout和endl进行输出、理解浮点数精度、区分前缀和后缀递增运算符等。同时,讨论了条件语句、循环、指针和引用、内存管理以及C++中的类和对象概念,提倡理解每种结构的作用和执行顺序,以提高代码质量。
摘要由CSDN通过智能技术生成

书名:C++ Without Fear

A Beginner’s Guide That Makes You Feel Smart

作者:Brian Overland

阅读时间:2018/10/6-2018/10/20

 

书摘+书评:

An important rule of good programming style is that variables should usually be initialized, which means giving them a value as soon as you declare them.

C++ also stores floating-point constants in double precision unless you specify otherwise (for example, by using the notation 12.5F instead of 12.5).

Type double stands for “double-precision floating point.”

building is the process of compiling and linking a program.


To print output and advance to the next line, use the cout object and send a newline character (endl). For example:
 

cout << "Never fear, C++ is here!" << endl;

Variables that may store a fractional portion should have type double. This stands for “double-precision floating point.” The single-precision type (float) should be used only when storing large amounts of floating-point data on disk.

 

You can assign values between integer and floating-point variables, but if you assign a floating-point value to an integer, the compiler complains about loss of data.

int n = 5;
double x = n; // Ok: convert 5 to floating-pt
n = 3.7; // Warning: convert from double to int
n = 3.0;/* This also gets a warning, because the presence of a decimal point (.) causes a value to be stored in floating-point (double) format.*/


 

What if, instead of executing one statement in response to a condition, you want to do a series of things? The answer is you can use a compound statement (or block):

if (x == y) {
    cout << "x and y are equal." << endl;
    cout << "Isn't that nice?";
    they_are_equal = true;
}

The significance of the block is that either all these statements are executed or none of them is executed. If the condition is not true, the program jumps past the end of the block. A block can be plugged into the if statement syntax because of another cardinal rule:

Anywhere you can use a statement in C++ syntax, you can use a compound statement (or block).

The block itself is not terminated by a semicolon (;)—only the statements inside are terminated by a semicolon. This is one of the few exceptions to the general rule that statements end with a semicolon in C++.


C++ treats “x = y” as an expression that produces a value. And there’d be nothing wrong with that, except that almost any valid numeric expression can be used as a condition. Therefore, the compiler does not complain if you write this, even though it’s usually wrong:
 

if (x = y)
// Do action... (Probably should have used ==)

 

The following statement divides by 2 and gets the remainder. This is called modulus or remainder division. The result is stored in a variable named (appropriately enough) “remainder.”
 

int remainder = n % 2;

 

Dividing two integers pr

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Now updated for the newest C++14 standard Paperback: 624 pages Publisher: Prentice Hall; 3 edition (December 30, 2015) Language: English ISBN-10: 0134314301 ISBN-13: 978-0134314303 Learning C++ Doesn’t Have to Be Difficult! Have you ever wanted to learn programming? Have you ever wanted to learn the C++ language behind many of today’s hottest games, business programs, and even advanced spacecraft? C++ Without Fear, Third Edition, is the ideal way to get started. Now updated for the newest C++14 standard and the free Microsoft Visual C++ Community Edition, it will quickly make you productive with C++ even if you’ve never written a line of code! Brian Overland has earned rave reviews for this book’s approach to teaching C++. He starts with short, simple examples you can easily enter and run. Within a couple of chapters, you’ll be creating useful utilities, playing games, and solving puzzles. Everything’s simplified to its essentials, patiently explained, and clearly illustrated with practical examples and exercises that help you make progress quickly. Overland reveals the “whys” and “tricks” behind each C++ language feature. And you’ll never get bogged down in complex or pointless examples: He keeps you 100% focused on learning what works and what matters—while having fun! This new and improved edition Covers installing and using the free Microsoft Visual C++ Community Edition—but you can use any version of C++ Explains valuable improvements in the new C++14 standard Modularizes C++14 coverage so it’s easy to write code that works with older versions Teaches with even more puzzles, games, and relevant exercises Offers more “why” and “how-to” coverage of object orientation, today’s #1 approach to programming Presents more ways to use Standard Template Library (STL) code to save time and get more done Contains an expanded reference section for all your day-to-day programming Whether you want to learn C++ programming for pleasure or you’re considering a career in programming, this book is an outstanding choice.
If you've always wanted to learn how to program a computer, or to learn the widely used C++ programming language in particular, C++ Without Fear, Second Edition, offers an ideal way to get you started. Written with the same approach that earned the first edition rave reviews, the author first emphasizes short, simple examples that are easy to enter; then, within a couple of chapters, he has you creating useful utilities, playing games, and using the computer to solve interesting puzzles. His approach is a welcome departure from many programming texts, which quickly get bogged down in complex and sometimes meaningless examples. You'll find here, patiently explained and clearly illustrated, everything you need to learn programming quickly, and to have fun doing it! Yes, programming can be a complex task, and C++ is a language often used by professionals. In fact, many of the coolest games, graphics, and Internet applications are created with C++, and it's even been used on the Mars rovers. But the language, like the monster pictured on the cover, need not be all that fearsome. Broken down to its essentials, and enhanced by simple examples, practical exercises, and the whys and tricks behind each language feature, you'll be amazed at the rapid progress you can make. With C++ Without Fear, Second Edition, you will * Learn the basics of C++ programming * Get started writing your own programs * See how and why each piece of a program does what it does * Create useful and reusable program code * Understand object-oriented programming--for once explained in simple, down-to-earth terms NEW AND IMPROVED While compatible with nearly every version of the C++ language, this second edition has been updated to cover important features of the newest and greatest C++ standard (C++0x). In addition, it now includes * Even more figures, examples, and exercises * Even more puzzles and games * An expanded 75-page language reference * Instructions for downloading free C++ software Whether you wish to learn C++ programming for pleasure, or you're considering a career in programming, this book is an outstanding choice.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值