【记录】Accelerated C++:Practical Programming by Example第0章:开始学习C++

//一个小的C++程序
#include<iostream>
int main()
{
	std::cout<<"Hello World!"<<std::endl;
	return 0;
}

运行结果:

 程序分析:

1.作用:在标准输入输出上显示Hello World!,显示在窗口上。

2.注释: //  一行

                /*   多行  */    编译时会忽略注释

3.#include 指令:#include指令,表示使用哪些 标准库 ,例如iostream(对流输入输出的支持,因为我们需要在窗口上输出Hello World!)

4.主函数main:  必须,要求返回一个整型数值作为结果,说明程序是否运行成功。0表示成功,其它表示失败。

5.花括号{ }:表示所有内容作为一个整体

6.使用标准库进行输入和输出:这条语句的作用是使用标准库iostream的输出运算符<<把 Hello World!写入到标准输出中,然后标准输出写入std:endl的值。

   cout之前的std::表示cout是命名空间std的一部分,标准库iostream使用std来包含所有由它定义的名称。eg:标准头文件iostream定义了cout和endl,对应程

   序中的std::cout std::endl.

  std::cout 名字为 标准输出流,写入到std::cout的内容会出现在cmd窗口中。

  std::endl作为一行输出的结束,下面的输出会另起一行。

7.返回语句  return 0: 所在之处终止程序,并把 0 返回函数。0 的类型必须与 int相一致。

8.这个程序中包含表达式和作用域。

  表达式: eg:  3+4 是一个表达式,它产生的结果是7,但7没有任何副作用

                           std::cout<"Hello World!"<<std::endl;  也是一个表达式,它产生的结果是把Hello World!写入标准输出流,显示于窗口,并用std::endl结束当前行。

  表达式中有操作数和运算符。其中两个<<是运算数,std::cout 、“Hello World!” 、std::endl是操作数。

OVER

——

为什么不使用using namespace std; 在谭**的《C++程序设计》中全部是这一种:

using namespace std - Google Search

  

When you make a call to using namespace <some_namespace>; all symbols in that namespace will become visible without adding the namespace prefix. A symbol may be for instance a function, class or a variable.

E.g. if you add using namespace std; you can write just cout instead ofstd::cout when calling the operatorcout defined in the namespacestd.

This is somewhat dangerous because namespaces are meant to be used to avoid name collisions and by writingusing namespace you spare some code, but loose this advantage. A better alternative is to use just specific symbols thus making them visible without the namespace prefix. Eg:

#include <iostream>
using std::cout;

int main() {
  cout << "Hello world!";
  return 0;
}
出处:http://stackoverflow.com/questions/18914106/what-is-the-use-of-using-namespace-std

              http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

渣渣翻译:

当你决定使用using namespace<some_namespace>,(例如Hello World!程序的中的using namespace std;),所有在命名空间中的符号都将可见而无需添加前缀。这个符号可能是函数,类或者一个变量。E.G.如果你添加using namespace std;你可以简写cout而不是std::cout。这有点危险因为命名空间原本是用来避免名字碰撞(命名冲突)。使用using namespace std可以简写代码,但是失去了这个优势。一个好的可选的方案是使用指定的符号,使符号无需添加命名空间的前缀而可见。


无意中找的到的实例,可以说明潜在的问题:http://blog.sina.com.cn/s/blog_a459dcf50101e2kx.html


为什么使用 int main而不是void main:

The difference is one is the correct way to define main, and the other is not.

And yes, it does matter.

int main(int argc, char** argv)

or

int main()

is the proper definition of your main per the C++ spec.

void main(int argc, char** argv)

is not and was, IIRC, a perversity that came with Microsoft's C++ compiler.

渣渣翻译:两者的区别之处在于一个是正确定义main,另一个则不是。是的,

                  这无关紧要。                

int main(int argc, char** argv)

or

int main()
按照C++的说明书,这才是正确main的定义。

void main(int argc, char** argv)

并不正确,它是一个由一开始由微软的C++编译器带来的错误。

出处:http://stackoverflow.com/questions/636829/difference-between-void-main-and-int-main


tips:(转义字符)  详情请见:http://blog.csdn.net/boyinnju/article/details/6877087

把\n 插入Hello World!中可实现 换行。 n是newline的缩写。


把\t  插入Hello World!中可实现 8个字符的空格,与按下TAB键作用类似。t是TAB的缩写。

\b  回退符,具体看效果: 吧\b的前一个字符去掉

 

\'   \\  \"的作用,嗯,C#中也可用。



习题:



乱七八糟的搜索:

make a call to 意思 - Google Search

Your call! = Make the call!

你决定!

Call,通常是指电话,但是当一群人正在思考如何做决定,或是如何解决问题时,其中有人对著你说,"Your call." 或是"Make the call."他们不是叫你去接电话或是去打电话,而是请你做决定。另外一个很常用的口语讲法 "It's up to you." 同样也是由你决定的意思,这也是你一定要学会的用法。

出处:http://cet.hjenglish.com/new/p172524/

……

命名空间的symbol - Google Search

C#的System.Activities.Debugger.Symbol 命名空间 ,symbol代表符号。

http://msdn.microsoft.com/zh-cn/library/system.activities.debugger.symbol%28v=vs.110%29.aspx

……

关于using namespace 的一些体会 - delmore的个人空间 - 开源中国社区

http://my.oschina.net/delmore/blog/4775


水平有限,笔记一定有不足错误之处,希望能告知,我会立即修改。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
If you don’t have a lot of time, but still want to learn the latest in C++, you don’t have to learn C first. You might learn more by digging into current language features and classes from the very beginning. That’s the approach that’s offered by Accelerated C++, a text that delves into more advanced C++ features like templates and Standard Template Library (STL) collection classes early on. This book arguably can get a motivated beginning programmer into C++ more quickly than other available tutorials. What separates this title from the rest of the pack is that it jumps right in with samples that take advantage of the Standard C++ of today–from streams to built-in container classes, such as vectors and linked lists. Early examples are more complex than in other books, but the thoroughness and relaxed pace of the explanations will bring the novice up to speed. (Although it ships at a slender 350 pages, Accelerated C++ is packed with samples, tips, and example problems; about 10 per chapter.) After a tour of basic C++, the book looks at more advanced C++ features, such as templates, including built-in support for containers. Besides tapping the strength of Standard C++, you also will learn to design with your own templates. (Other tutorials would defer this material until later on.) The authors have tested the approach in the book in their own teaching, and honed a set of worthwhile examples that will help anyone get familiar with these powerfullanguage features . All examples make use of the command line and console (without GUI programs), but the advantage is that this code should run on any of today’s operating systems and compilers. Later sections cover the basics of class design, which include good coverage of operator overloading and inheritance. With its innovative approach to teaching the language, Accelerated C++ will challenge readers in the right way. It suggests that you don’t need to learn C to be productive in C++. Written in an approachable style, it deserves a close look from any C++ novice. –Richard Dragan

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值