文章目录
C plus plus – C语言的超集
C++可以完全引用C
写代码之前的准备:
(1)安装编译环境:yum install gcc-c++ -y
(2)设置缩进: cd /etc/vimrc >> set autoindent
(3)设置行号:set nu
案例1:输出Hello World
- 源代码:
HelloWorld.cpp
// 第一个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. 源文件后缀
- 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 |
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++标准方式
3. 函数重载(同名函数的解决)
案例2: 以下C与C++的编译执行结果
printf.c
#include <stdio.h>
void printf(){
printf("Hello world");
}
int main(){
printf();
}
printf.cpp
#include <cstdio>
void printf(){
printf("Hello world");
}
int main(){
printf();
}
Results:
(1)printf().c //出错,出现了相同的(矛盾的声明);
(2)printf().cpp //循序函数重载;
函数重载:函数名相同只有参数(个数或者类型)不相同。
c | c++ |
---|---|
不支持重载 | 支持重载 |
案例3: 函数重载的简单应用
#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);
}
4. 命名空间(同名且参数相同)
c | c++ |
---|---|
不支持命名空间 | 支持命名空间 |
-
命名空间的作用:避免全局变量、函数、类的命名冲突(因为名字相同而编译失败)。
-
定义命名空间
namespace 空间名 {
// 定义类/函数
}
- 引用命名空间
-
using指令(using directive)
using namespace 空间名;
例如使用标准命名空间
std
using namespace std;
-
using声明(using declaration)
using 空间名::标识符;
例如使用标准命名空间
std
的cout
using std::cout;
-
案例4: 命名空间的简单应用
#include <iostream>
using namespace std;
namespace oldFunc{
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{
void Func(int m){
cout << "int m " << m << endl;
}
}
using namespace oldFunc;
int main(){
newFunc::Func(100);
Func(12.5);
Func(10,3);
}
全局命名空间
- 默认的命名空间,所有名字都在全局命名空间中。
- 使用方式:直接忽略或者只写
::
例如:定义全局函数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::可以带也可以不带;
5. 类型
- 新增基本类型
bool
–true
/false
在C99中
stdbool.h
中增加三个宏定义bool
、true
和false
。在C++中是内置类型和常量。
如何验证C的bool
是宏定义,C++的bool
不是宏定义?
password.c
#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:%d\n", password1 == password2);
}
password.cpp
#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
详细信息参见:类与对象章节
6. 思想
c | c++ |
---|---|
面向过程 | /基于对象 |
何为面向过程?何为面向对象?
- 面向过程:强调如何处理(如何解决)
- 面向对象:强调执行处理的对象(找谁解决)
面向过程与面向对象:厨师与老板
思维区别
- 将问题按照过程方式来解决?
- 将问题抽象为一个对象来解决?
7. 动态内存
- 基本类型的动态内存
dynamic_mem.c
#include <stdio.h>
#include <stdlib.h>
int main(){
int* num = malloc(sizeof(int));
*num = 100;
printf("%d\n",*num);
free(num);
}
dynamic_mem.cpp
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main(){
int m = 3;
int *n = new int[m];
for(int i = 0; i < m; ++i){
n[i] = i + 100;
}
for(int i = 0; i < m; ++i){
printf("%d\n",n[i]);
}
delete []n;
}
动态内存区别
C | C++ |
---|---|
malloc() /free() |
new /delete |
_C++仍然可以使用malloc()
/free()
,但是不建议这么做。malloc需要加上返回值类型
问题:
malloc()
申请内存,是否可以使用delete
销毁内存? nonew
申请内存,是否可以使用free()
销毁内存? no
8. 初始化
C++特殊初始化方法
int n=10;
int m(10);
扩展阅读
- <<Accelerated C++中文版>> 第0~2章
- 为什么
for
循环的下标习惯从0
开始? - 为什么
for
循环的的判断条件通常使用!=
?
- 为什么
- <<C++程序设计原理与实践>>
- 第2章Hello,World!
- 第3章 对象、 类型和值
练习
- 完成上面扩展阅读中的习题和练习。
9. 面试
(命名空间)1. 在C++中,不带
.h
后缀的头文件所包含和定义的标识符在std
空间中;
带.h
后缀的头文件所包含和定义的标识符在全局命名空间中,不需要声明使用std
空间
(内存分配)2. | C | C++|
|malloc()
/free()
|new
/delete
|
C++仍然可以使用malloc()
/free()
,但是不建议这么做。malloc需要加上返回值类型