C++学习第三天

简介:

一些基础就可以略过了,看书不能太呆板。
如果把一本书看完了,剩下的就是在实际项目中进行整合,训练,然后整理成笔记。
每天敲点程序,记点笔记。
 
变量作用域
代码:

复制代码
#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

判断代码输出结果
代码:

复制代码
#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。

一旦使用了using声明,我们就可以直接引用名字,而不需要再引用改名字的命名空间了。
代码:

复制代码
#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




本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/archive/2013/04/11/3014962.html,如需转载请自行联系原作者

相关文章
|
16天前
|
存储 C++ 容器
C++STL(标准模板库)处理学习应用案例
【4月更文挑战第8天】使用C++ STL,通过`std:vector`存储整数数组 `{5, 3, 1, 4, 2}`,然后利用`std::sort`进行排序,输出排序后序列:`std:vector<int> numbers; numbers = {5, 3, 1, 4, 2}; std:sort(numbers.begin(), numbers.end()); for (int number : numbers) { std::cout << number << " "; }`
19 2
|
27天前
|
存储 安全 编译器
C++学习过程中的一些值得注意的小点(1)
C++学习过程中的一些值得注意的小点(1)
|
28天前
|
存储 算法 数据库
【C++ 软件设计思路】学习C++中如何生成唯一标识符:从UUID到自定义规则
【C++ 软件设计思路】学习C++中如何生成唯一标识符:从UUID到自定义规则
104 0
|
1月前
|
C++
C++学习系列---读取文件名存入txt和从txt读取每行信息
C++学习系列---读取文件名存入txt和从txt读取每行信息
|
1月前
|
存储 资源调度 算法
Opencv(C++)系列学习---SIFT、SURF、ORB算子特征检测
Opencv(C++)系列学习---SIFT、SURF、ORB算子特征检测
|
1月前
|
编译器 测试技术 C++
【Python 基础教程 01 全面介绍】 Python编程基础全攻略:一文掌握Python语法精髓,从C/C++ 角度学习Python的差异
【Python 基础教程 01 全面介绍】 Python编程基础全攻略:一文掌握Python语法精髓,从C/C++ 角度学习Python的差异
165 0
|
2天前
|
C语言 C++
c++的学习之路:4、入门(3)
c++的学习之路:4、入门(3)
16 0
|
2天前
|
编译器 C++
c++的学习之路:23、多态(2)
c++的学习之路:23、多态(2)
16 0
|
2天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
19 0
|
2天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
17 0

热门文章

最新文章