C++学习第三天
2013-04-11 17:41
295 查看
一些基础就可以略过了,看书不能太呆板。
如果把一本书看完了,剩下的就是在实际项目中进行整合,训练,然后整理成笔记。
每天敲点程序,记点笔记。
[b]变量作用域[/b]
代码:
结果:
hello world
42 world
[b]判断代码输出结果[/b]
代码:
结果:
100 45
用class和struct关键字定义类的唯一差别在于默认访问级别:默认情况下,struct的成员为public,而class的成员为private。
[b]一旦使用了using声明,我们就可以直接引用名字,而不需要再引用改名字的命名空间了。[/b]
代码:
结果:
The size of The expense of spirit
is22 characters,including the newline
如果把一本书看完了,剩下的就是在实际项目中进行整合,训练,然后整理成笔记。
每天敲点程序,记点笔记。
[b]变量作用域[/b]
代码:
#include <iostream> #include <string> #include <conio.h> std::string s1 = "hello"; //全局 int main(){ std::string s2 = "world"; //局部 std::cout<<s1<<" "<<s2<<std::endl; int s1 = 42; //局部+隐式全局 std::cout<<s1<<" "<<s2<<std::endl; getch(); return 0; }
结果:
hello world
42 world
[b]判断代码输出结果[/b]
代码:
#include <iostream> #include <string> #include <conio.h> int i = 42; int main(){ int i = 100,sum = 0; for(int i = 0;i!=10;++i) sum += i; std::cout<<i<<" "<<sum<<std::endl; getch(); return 0; }
结果:
100 45
用class和struct关键字定义类的唯一差别在于默认访问级别:默认情况下,struct的成员为public,而class的成员为private。
[b]一旦使用了using声明,我们就可以直接引用名字,而不需要再引用改名字的命名空间了。[/b]
代码:
#include <iostream> #include <string> #include <conio.h> using std::cin; using std::string; using std::cout; int main(){ string s; cin>>s; cout<<s; getch(); return 0; }
#include <iostream> #include <string> #include <conio.h> using std::cin; using std::string; using std::cout; using std::endl; int main(){ string st("The expense of spirit\n"); cout<<"The size of "<<st<<"is"<<st.size() <<" characters,including the newline"<<endl; getch(); return 0; }
结果:
The size of The expense of spirit
is22 characters,including the newline
相关文章推荐
- 学习C++的第三天
- c++基础学习第三天(c++程序的结构课后练习)
- 初学者:学习C++第三天,教材是C++primer
- C++对C函数加强(第三天学习)
- C++学习笔记第三天:类、虚函数、双冒号
- c++ 学习之路 第三天 作业理解
- C++第三天学习
- C++学习第二、第三天
- c++学习第三天
- C++学习笔记(1)——基类、派生类的对象空间
- c++学习笔记-------《c++自学通》第十章 高级指针
- C++ 中堆栈学习
- C/C++学习笔记
- 转---C++学习之多态及重载(overload),覆盖(override),隐藏(hide)的区别
- 学习c++的50条忠告
- 学习C++的过程(引用的)
- 我的 C++ 学习历程 之三 Button Sytle 译文(3)
- 如何正确的入门Windows系统下C/C++学习领域
- C++ Primer Plus(第六版)--学习杂记(第三章)
- c++学习--显示类型转化