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<<""<<"\n"
```
仅换行
### 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;
```
C++ 一周刷完C++基础课程(同C程序进行比较)的更多相关文章
- 学习笔记(一) HTML+CSS基础课程
这个周把慕课网的<HTML+CSS基础课程>课程学完,内容都是非常非常基础的,不过还是学到了几个小知识点,记下来先. <a>超链接发送邮件 直接上把他的图片给挪过来了,我就不打 ...
- Coursera台大机器学习基础课程1
Coursera台大机器学习基础课程学习笔记 -- 1 最近在跟台大的这个课程,觉得不错,想把学习笔记发出来跟大家分享下,有错误希望大家指正. 一 机器学习是什么? 感觉和 Tom M. Mitche ...
- PHP基础课程学习总结
时间过得很快,不知不觉中过去了一个月,PHP基础课程已经学完了.休息这几天中,睡觉起来,整理下笔记,几天的假期又过去了,明天正式开始PHP的专业课程,新的征途又要开始了.开发整站时发现,过去整站做得太 ...
- C#基础课程之六(临时表)DataTable使用方法
DataTable 用法:赋取值操作,及报错情况 dataTable.Columns.Add("Name"); //Columns 对象获取该集合的全部列,添加列名. 默认stri ...
- C#基础课程之五集合(HashTable,Dictionary)
HashTable例子: #region HashTable #region Add Hashtable hashTable = new Hashtable(); Hashtable hashTabl ...
- C#基础课程之四集合(ArrayList、List<泛型>)
list泛型的使用 ArrayList list = new ArrayList(); ArrayList list = ); //可变数组 list.Add("我"); //Ad ...
- C#基础课程之三循环语句
for循环: ; i < ; i++) { Console.WriteLine("执行"+i+"次"); } while循环: while (true) ...
- C#基础课程之一注释和控制台、一些常识
注释是程序员对代码的说明,以使程序具有可读性.源代码在编译的过程中,编译器会忽略其注释部分的内容. ()行注释 格式为:// 注释内容 用两个斜杠表示注释的开始,直到该行的结尾注释结束. ()块注释 ...
- 01_Python 基础课程安排
Python 基础课程安排 目标 明确基础班课程内容 课程清单 序号 内容 目标 01 Linux 基础 让大家对 Ubuntu 的使用从很 陌生 达到 灵活操作 02 Python 基础 涵盖 Py ...
随机推荐
- MySQL 数据库 高级查询
1.连接查询select * from Info,Nation #笛卡尔积select * from Info,Nation where Info.Nation=Nation.Code join on ...
- Python之输入输出
python中变量的输出 # 打印提示 print('hello world') print('你好!') # 输出变量 url = 'loaderman' print('我是:',url) prin ...
- Struts2-Ajax整合之Jquery版本
<纯JavaScript版本 http://www.cnblogs.com/hzb462606/p/8934787.html > 大部门跟JavaScript版本一致,就是<sc ...
- 【疑难杂症】【Solved】maven-compiler-plugin 在 idea 下的问题
maven-compiler-plugin 这个插件在idea和eclipse里表现本质是一样的,但是我之前有个细节没注意到,导致我对此有误解.我之前一直以为只要配置了source和target,相应 ...
- centos v7.0配置sftp
需求: 1.建立三个sftp帐号,admin,test1,test22.三个帐号分别在/home/sftp下拥有相应的目录3.test1和test2只能进入自己的目录,admin可以进入三个目录(ch ...
- CDH6.2中capacity队列的分配
配置: yarn.scheduler.capacity.root.queues
- BATJ的常见java面试题
JAVA基础 JAVA中的几种基本数据类型是什么,各自占用多少字节. String类能被继承吗,为什么. 不可以,因为String类有final修饰符,而final修饰的类是不能被继承的,实现细节不允 ...
- 关于float的小奥秘
一. float 存储方式 1.1. float 占四个字节 1.2. 浮点数构成 1.2.1. 无论是单精度还是双精度在存储中都分为三个部分: <1>. 符号位(Sign) : 0代表正 ...
- os.path路径拓展 python3
os.path-对路径path进行的操作 在调用os.path时, 根据操作系统的不同 程序会选择使用posixpath.py或ntpath.py(由os中的代码实现). 对文件命名时应当使用unic ...
- 高效编程之 concurrent.future
背景 我们知道 Python 中有多线程threading 和多进程multiprocessing 实现并发, 但是这两个东西开销很大,一是开启线程/进程的开销,二是主程序和子程序之间的通信需要 序列 ...