文章目录
- (***)案例1:输出Hello World
- (***)1.1 . 源文件后缀
- (***)1.2.引用头文件
- (***)1.3. 函数重载(同名函数的解决)
- 案例2: 函数重载在C与C++的编译执行结果
- 案例3: 函数重载的简单应用
- (***)1. 4. 命名空间(同名且参数相同)
- 案例4: 命名空间的简单应用
- 1.5. 类型
- 1.6. 思想
- (***)1.7. 动态内存
- (***)1.8. 初始化
- (***)1.9. 面试
- 扩展阅读
(***)案例1:输出Hello World
C plus plus – C语言的超集
C++可以完全引用C
写代码之前的准备:
(1)安装编译环境:yum install gcc-c++ -y
(2)设置缩进: cd /etc/vimrc >> set autoindent
(3)设置行号:set nu
(4)替换::%s/lijing/mapeili/g %%把lijing替换成mapeili
- 源代码:
001_HelloWorld.cpp
1
2
3
4
5
6
7// 第一个C++程序 #include <iostream> using namespace std; int main(){ cout<< "Hello world" <<endl; }
- 编译:
g++ HelloWorld.cpp -o HelloWorld
- 执行:
./HelloWorld
- 结果:
Hello world
麻雀虽小,五脏俱全。
从HelloWorld.cpp
,看C++与C的基本区别:
- 单行注释
//
(C99开始支持单行注释) - 文件后缀名
.cpp
- 头文件
#include <iostream>
- 命名空间
using namespace std;
- 标准输出流
cout
、输出运算符<<
、换行控制器endl
- 编译工具
g++
(***)1.1 . 源文件后缀
- C/C++头文件后缀名区别
c | *.h |
---|---|
c++ | *.h *.hpp |
- C/C++源文件后缀名区别
c | *.c |
---|---|
c++ | *.cpp *.cc *.cxx |
- 不同编译器C++源文件后缀名区别
平台 | 可用后缀名 |
---|---|
Unix | *.C , *.cc , *.cxx , *.c |
GNU C++ | *.C , *.cc , *.cxx , *.cpp , *.c++ |
Borland C++ | *.cpp |
Microsoft Visual C++ | *.cpp , *.cxx , *.cc |
(***)1.2.引用头文件
C++头文件使用C标准库,在C标准库文件名前加上字母c
,并且省略后缀名.h
,例如:
注意:<>表示引用库里面的头文件,“”自定义的头文件;
c | c++ |
---|---|
#include <stdio.h> | #include <iosteam> /#include <cstdio> |
#include <stdlib.h> | #include <cstdlib> |
#include <string.h> | #include <cstring> |
#include <math.h> | #include <cmath> |
有些C++编译器同时支持以上两种头文件,但有些不。请使用C++标准方式
- 类定义与类实现分离
- 头文件 – 声明
方式:#pragma once
或者#ifnde...#endif
作用:防止头文件二次编译 - 源文件 -- 实现
引用头文件:#include <>
(标准库函数)/#include ""
(自定义/第三方函数)
在C++书籍中为了方便
.h
与.cpp
不做分离,但是项目开发中,需要分开。
(***)1.3. 函数重载(同名函数的解决)
c | c++ |
---|---|
不支持重载 | 支持重载 |
(1)printf().c //出错,出现了相同的(矛盾的声明);
(2)printf().cpp //循序函数重载;
函数重载:函数名相同只有参数(个数或者类型)不相同。
案例2: 函数重载在C与C++的编译执行结果
完整代码见 00201_printf.c
1
2
3
4
5
6
7
8#include <stdio.h> void printf(){ printf("Hello world"); } int main(){ printf(); }
完整代码见 00202_printf.cpp
1
2
3
4
5
6
7
8#include <iostream> using namespace std; void printf(){ printf("Hello world"); } int main(){ printf();
案例3: 函数重载的简单应用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <iostream> using namespace std; void Func(int n){ cout << "int n " << n << endl; } void Func(double n){ cout << "double n " << n << endl; } void Func(int n, int m){ for(int i = 0; i < m; ++i){ cout << n << " " ; } cout<<endl; } int main(){ Func(100); Func(12.5); Func(10,3); }
(***)1. 4. 命名空间(同名且参数相同)
c | c++ |
---|---|
不支持命名空间 | 支持命名空间 |
-
命名空间的作用:避免全局变量、函数、类的命名冲突(因为名字相同而编译失败)。
-
定义命名空间
1
2
3
4namespace 空间名 { // 定义类/函数 }
- 引用命名空间
-
using指令(using directive)
复制代码1
2using namespace 空间名;
例如使用标准命名空间
std
复制代码1
2using namespace std;
-
using声明(using declaration)
复制代码1
2using 空间名::标识符;
例如使用标准命名空间
std
的cout
复制代码1
2using std::cout;
-
案例4: 命名空间的简单应用
(1)作用,用途:
用来解决函数名相同,函数参数也相同
(2)声明的方法:
1
2
3
4
5
6
7
8namespace oldFunc{ //(1.1)函数名相同,函数参数也相同;用命名空间进行区分 void Func(int n){ cout << "int n " << n << endl; } }
(3)调用的方案:
方案1:在main()函数之前进行命名空间的声明;
1
2
3using namespace oldFunc; //方案1:进行命名空间的声明;
方案2:在调用的时候加上命名空间
1
2
3newFunc::Func(100); //方案2:在调用的时候加上命名空间
方案3: 如果有命名空间,则进行匹配;没有的话,则是标准命名空间;
1
2cout <<("abc")<< ednl; //如果有命名空间,则进行匹配;没有的话,则是标准命名空间;
(完整代码见004_namespace.cpp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38#include <iostream> using namespace std; namespace oldFunc{ //(1.1)函数名相同,函数参数也相同;用命名空间进行区分 void Func(int n){ cout << "int n " << n << endl; } } namespace oldFunc{ void Func(double n){ cout << "double n " << n << endl; } void Func(int n, int m){ for(int i = 0; i < m; ++i){ cout << n << " " ; } cout<<endl; } } namespace newFunc{ //(1.1)函数名相同,函数参数也相同;用命名空间进行区分 void Func(int m){ cout << "int m " << m << endl; } } using namespace oldFunc; //方案1:进行命名空间的声明; int main(){ newFunc::Func(100); //方案2:在调用的时候加上命名空间 Func(12.5); Func(10,3); cout <<("abc")<< ednl; //如果有命名空间,则进行匹配;没有的话,则是标准命名空间; }
全局命名空间
- 默认的命名空间,所有名字都在全局命名空间中。
- 使用方式:直接忽略或者只写
::
例如:定义全局函数void test();
,默认就是在全局命名空间中,调用方式test()
或者::test()
。
总结
(1) 在C++中,不带.h
后缀的头文件所包含和定义的标识符在std
空间中;
带.h
后缀的头文件所包含和定义的标识符在全局命名空间中,不需要声明使用std
空间
(2)两种书写方式:
a.如果有声明的话, using namespace old; main()函数调用不用加前缀;
b.如果没有声明,仅仅有 namespace new {// 定义类/函数 }
main()函数调用的时候需要加上new::
(3)全局命名:同一空间,重名的话,采用就近的原则;
::func() (全局对象)其他注意:cin ,cout ,endl前面的std::可以带也可以不带;
1.5. 类型
- 新增基本类型
bool
–true
/false
在C99中
stdbool.h
中增加三个宏定义bool
、true
和false
。在C++中是内置类型和常量。
如何验证C的bool
是宏定义,C++的bool
不是宏定义?
password.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <stdio.h> int main(){ printf("input user name:"); char name[BUFSIZ]; scanf("%s",name); printf("input 3 number password:"); int password1; scanf("%d",&password1); printf("input 3 number password again:"); int password2; scanf("%d",&password2); printf("password check:%dn", password1 == password2); }
password.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <iostream> #include <cstdio> using std::cout; using std::cin; using std::endl; int main(){ cout << "input user name:"; char name[BUFSIZ]; cin >> name; cout << "input 3 number password:"; int password1; cin >> password1; cout << "input 3 number password again:"; int password2; cin >> password2; cout << "password check:" << (password1 == password2) << endl; }
- 新增自定义类型
class
详细信息参见:类与对象章节
1.6. 思想
c | c++ |
---|---|
面向过程 | /基于对象 |
何为面向过程?何为面向对象?
- 面向过程:强调如何处理(如何解决)
- 面向对象:强调执行处理的对象(找谁解决)
面向过程与面向对象:厨师与老板
思维区别
- 将问题按照过程方式来解决?
- 将问题抽象为一个对象来解决?
(***)1.7. 动态内存
- 基本类型的动态内存
dynamic_mem.c
(c语言)
1
2
3
4
5
6
7
8
9#include <stdio.h> #include <stdlib.h> int main(){ int* num = malloc(sizeof(int)); *num = 100; printf("%dn",*num); free(num); }
dynamic_mem.cpp
(c++)
(完整代码见005_dynamic_mem.cpp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <stdlib.h> #include <stdio.h> using namespace std; int main(){ int m = 3; int *n = new int[m]; //数组的初始化; int n = new int(3); //数字的初始化; for(int i = 0; i < m; ++i){ n[i] = i + 100; } for(int i = 0; i < m; ++i){ printf("%dn",n[i]); } delete []n; }
动态内存区别
C | C++ |
---|---|
malloc() /free() | new /delete |
_C++仍然可以使用malloc()
/free()
,但是不建议这么做。malloc需要加上返回值类型
问题:
malloc()
申请内存,是否可以使用delete
销毁内存? nonew
申请内存,是否可以使用free()
销毁内存? no
(***)1.8. 初始化
C++特殊初始化方法
1
2
3int n=10; int m(10);
(完整代码见00502_init.cpp)
1
2
3
4
5
6
7
8
9
10
11
12
13#include <iostream> using namespace std; int main(){ int* m = new int(10); //创建单个对象; cout << *m << endl; // int* n = new int[10](10); int* n = new int[10]{10,11}; //创建数组对象(没有定义的是0) for(int i = 0; i < 10;++i){ cout << n[i] << endl; } }
(***)1.9. 面试
(命名空间)1. 在C++中,不带
.h
后缀的头文件所包含和定义的标识符在std
空间中;
带.h
后缀的头文件所包含和定义的标识符在全局命名空间中,不需要声明使用std
空间
(内存分配)2. | C | C++|
|malloc()
/free()
|new
/delete
(自动调用默认构造函数、析构函数) |
C++仍然可以使用malloc()
/free()
,但是不建议这么做。malloc需要加上返回值类型
(完整代码见006_new_delete.cpp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <iostream> using namespace std; #include <cstdlib> class Simple{ public: Simple(){ cout << "Simple" << endl; } ~Simple(){ cout << "~Simple" << endl; } }; int main(){ Simple* s = new Simple;//(1)自动调用默认构造函数 delete (s); //(2)自动调用默认析构函数 }
扩展阅读
- <<Accelerated C++中文版>> 第0~2章
- 为什么
for
循环的下标习惯从0
开始? - 为什么
for
循环的的判断条件通常使用!=
?
- 为什么
- <<C++程序设计原理与实践>>
最后
以上就是无辜月亮最近收集整理的关于C++基础1:从c过渡到c++(***)案例1:输出Hello World的全部内容,更多相关C++基础1:从c过渡到c++(***)案例1:输出Hello内容请搜索靠谱客的其他文章。
发表评论 取消回复