演讲稿第1章C++ 概述.ppt

  1. 1、本文档共54页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
单行注释和新的I/O流(续) cout 是预定义的输出流对象,类似于C语言中的stdout。 输出运算符,可用于输出C++语言中任何基本类型的数据。 cin 是预定义的输入流对象,类似于C语言中的stdin。 输入运算符,可用于输入C++语言中任何基本类型的数据。 (注意:输入和输出并不是C++语言的组成部分,它们由流库iostream支持。) ., * 输入含有空格的字符串 // Use getline() to read a string that contains spaces. #include iostream #include fstream using namespace std; int main() { char str[80]; cout Enter your name: ; cin.getline(str, 79); cout str \n; return 0; } ., * 1.3.2 const 存取修饰符 对象 A:亲爱的,你千万不能变心? 对象 B:放心吧!亲爱的。 对象 A:你发誓! 对象 B:不用发誓,因为我是const ! const 对象 B; ., * 常量 Constants 在C中,可以使用#define来定义符号常量 。 C++提供了一种更灵活、更安全的方式来定义常量,即使用关键字const来定义符号常量。 ., * 常量例子 Constant examples const float PI = 3.1415926; // PI是一个常量 const int v[ ]={1,2,3,4}; //数组元素v[i]是常量 const int x; // error: no initializer //定义常量时应初始化,否则出错。 void f( ) { model =200; // error v[2]++; // error } ., * 值替换 value substitution #define BUFSIZE 100 const int bufsize = 100; Because of subtle bugs that the preprocessor might introduce, you should always use const instead of #define value substitution. ., * 常量const和指针 指针所指向的对象为常量 Pointer to const 指针本身为常量 const pointer The const modifies the thing it is “closest to.” ., * 指向常量的指针 Pointer to const const int* u; // pointer to constant *u=18; // error: u points to constant u= p; // OK ., * 常指针 const pointer int d = 1; int* const w = d; *w=2; // OK w=p; // error: w is const ., * const修饰函数参数 void print_salary (const float *salary) { coutsalary’\n’; } const可以阻止参数被函数修改 ., * const 的其他用途 Other uses of const return types, class objects and member functions ., * volatile 存取修饰符 修饰符volatile通知编译器,变量值可能由程序中没有显示说明的方式所改变,因此在作编译优化时,不要通过将该变量放入寄存器来提高速度。 Volatile原意是:可变的,不稳定的 extern volatile clock; extern const volatile clock; ., * 1.3.3  内联函数(内嵌函数, 内置函数 inline function) 在函数声明或定义的前面加上关键字“inline”,该函数就被声明为内联函数。 inline int max(int x, int y) // max被说明为内联函数 { int z; if(xy) z=x; else z=y; return (z); } 内联函数的调用方法与普通函数没有区别。 ., * 类中定义的内联函数Inline function Any function defin

文档评论(0)

liuxiaoyu92 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档