C++ 一周刷完C++基础课程(同C程序进行比较)

**参考bilibili视频av29504365**

### 一段简单的程序Hello World
```
#include <iostream>
using namespace std;
int main(){
cout<<"hello world"<<endl;
system("pause");
return 0;
}
```
### 注释
- 单行注释//
- 多行注释/**/
- #if 0 #end if
快捷键
Ctrl K C 快速注释
Ctrl K U 取消注释

### main
```
int main(){
//一般写法
return 0;
}
```
```
int main(void){
//标准写法
return 0;
}
```
```
int main(int argc,char* argv[]){
//需要使用命令行
return 0;
}
```
```
main(){
//仅限C语言可以
}
```
```
void main(){
//C Premiere Plus书中不建议这样写
}
```

### 头文件
```
#include <iosteam>//新版推荐
```
```
#include <iosteam.h>//c++早期,vs不可以编译
```
### cincout
```
cout<<""<<"";
```
```
cin>>c
```
- 1.连续输出
- 2.自动识别类型

### endl
```
cout<<""<<endl;
```
换行符,并且清空缓冲区
```
cout<<""<<" "
```
仅换行

### namespace
C语言中不能出现一样的函数,但是C++中同一个namespace不能出现一样函数

```
#include <iostream>
using namespace std;
namespace stu{
void print(){
cout<<"im stu"<<endl;
}
}
namespace tea{
void print(){
cout<<"im tea"<<endl;
}
}
int main(){
stu::print();
tea::print();
system("pause");
return 0;
}
如果同时using了stu和tea的命名空间并调用print函数,会报错,不知道应该调用哪个命名空间的print函数
```
在std命名空间中有C++常用的函数
```
using std::cout;
```
可以打开某一个函数
```
std::cout<<"";
```
不打卡也可以直接调用

### struct

```
struct Node{
int n;
};
int main(){
Node n;//C++结构体不用加struct
n.n=4;
return 0;
}
```
```
struct Node{
int n;
};
int main(){
struct Node n;//C语言需要加
n.n=4;
return 0;
}
```
```
typedef struct Node{//C语言中要想不加,需要加typedef
int n;
}Node;
int main(){
Node n;
n.n=4;
return 0;
}
```
**C语言拓展:函数指针**
```
#include <stdio.h>

struct Node{
int m;
void (*P)();
};
void fun(){
printf("fun");
}
int main(void){
struct Node a={1,fun};
a.P();
return 0;
}
```
### new delete 内存申请与释放
```
int *p=(int*)malloc(sizeof(int));
int *p1=new int;
int *p2=new int(11);//申请并且初始化
free(p);
```
空间申请
```
delete p1;//delete+指针
```
**区别**
C语言中malloc和free进行申请内存和释放,而C++中使用new和delete进行申请和释放
本质上区别不大,如果涉及到类,必须使用new和delete进行申请和释放

### 基本类型的引用
```
举例
int main(){
int a=5;
int c1=a;//传值
c1=6;//a不变
int &c2=a;//声明变量a的一个引用,c是a的一个别名,不是取地址符号
c2=7;//a会改变
int *p=&a;//&表示取地址
system("pause");
return 0;
}
```
### 其他类型的引用
**数组的引用**

```
int arr[10];
int (&p)[10]=arr;
```
**二维数组的引用**
```
int arr[10][10];
int (&p)[10][10]=arr;
```
**指针的引用**
```
int b=12;
int *point=&b;
int* &p3=point;
```

### 引用作参数

```
void swap(int& a,int& b){
int temp=a;
a=b;
b=temp;
}
```
实际还可以指针作参数交换

```
void swap(int* a,int* b){
int temp=*a;
*a=*b;
*b=temp;
}
```

### 引用作返回值
```
int& fun(){
int a=12;
return a;
}
int main(){
int& b=fun();
cout<<b<<endl;
//因为fun()执行完就释放了,所以b的引用是一块非法空间,编译器会警告
system("pause");
return 0;
}
```
**注意:引用做返回值,一定不能返回局部变量**

### 增强for循环
可以在for循环内部定义局部变量,i的作用域在循环体

```
int a[]={1,2,3,4,5,6,7};
for(int i=0;i<7;i++){
cout<<i<<endl;
}
for(int i=0;i<7;i++){
cout<<i<<endl;
}
```
vc6.0编译器以及C语言都是在for循环定义的i在全局有作用
```
int a[]={1,2,3,4,5,6,7};
for(int i=0;i<7;i++){
cout<<i<<endl;
}
for(i=0;i<7;i++){
cout<<i<<endl;
}
```

### 函数参数的缺省
```
void print(int a=10);
int main(){
print();
system("pause");
return 0;
}
void print(int a){
cout<<a<<endl;
}
```
在函数声明后可以进行参数缺省

### 函数的重载
```
void add(int a,int b);
void add(int a,int b,int c);
int main(){
add(1,2);
add(1,2,3);
system("pause");
return 0;
}
void add(int a,int b){
cout<<a+b<<endl;
}
void add(int a,int b,int c){
cout<<a+b+c<<endl;
}
```
函数名字相同,参数列表不同或者类型不同,与返回值无关

### 头文件重复包含问题
C语言中
```
#ifndef AAA
#define AAA
void fun();
#endif
```
C++中,也能实现同样的功能
```
#pragma once
void fun();
```
**注意**
pragma可能不适用于vc编译器

### 函数模板
```
template<typename T>
void fun(T a){
cout<<a<<enl;
}
int main(){
fun(‘a’);
system("pause");
return 0;
}
```
这样可以任意传参数

### 函数模板的具现化
```
struct Node
{
int a;
double b;
};
template<class T>
void fun(T a){
cout<<a<<endl;
}
template<> void fun<Node>(Node no){
cout<<no.a<<endl;
}
int main(){
Node node;
fun(no);
system("pause");
return 0;
}
```

### 类模板
```
template<typename T>
class father{
public:
int a;
father(T t){
a=t;
}
void show(){
cout<<a<<endl;
}
}
int main(){
father<int> pf;
system("pause");
return 0;
}
```
只对下面的类有效

### 继承模板
```
class son:public father<int>{

}
int main(){
son s;
system("pause");
return0;
}
```
### 多态的模板
```
int main(){
father<int> *pf=new son<int>;
pf->fun();
system("pause");
return 0;
}
```
### 类型是类的模板
```
father<Caa a> pf;
```

原文地址:https://www.cnblogs.com/littlepage/p/10989598.html