C++学习笔记(二)
来源:互联网 发布:第三次网络机器人大战 编辑:程序博客网 时间:2024/05/16 14:31
1.C++语言三特性:封装、继承、多态
2.引用类型
引用:变量的别名(变量不能只有别名)
基本数据类型的引用#include<stdlib.h>#include<iostream>using namespace std;int main(void){ int a = 3; int &b = a; // 引用的初始化且必须 b = 10; // 对别名的任何操作都是对变量本身的操作 cout<<a<<endl; return 0; }// 结构体类型的引用#include<stdlib.h>#include<iostream>using namespace std;typedef struct{ int x; int y;}Coor;int main(void){ Coor c1; Coor &c = c1; c.x = 10; c.y = 20; cout<<c1.x<<c1.y; return 0;}// 指针类型的引用 类型 *&指针引用名 = 指针#include<stdlib.h>#include<iostream>using namespace std;int main(void){ int a = 10; int *p = &a; int *&q = p; *q = 20; cout<<a<<endl; return 0;}
引用作为函数参数
// 没有学习引用前void fun(int *a,int *b){ int c = 0; c = *a; *a = *b; *b = c;}int x = 10,y = 20;fun (&x,&y);// 引用作为函数参数void fun(int &a,int &y){ int c = 0; c = a; a = b; b = c;}int x = 10,y = 20;fun(x,y);
2.控制变量const
// const与基本数据类型的关系const int a = 3; //a为值3得常量,不可更改// const与指针类型const int *p = NULL;int const *p = NULL; //二者完全等价int *const p = NULL; //与前两者完全不同// 指针类型可以添加两处constconst int *const p = NULL;int const * const p = NULL; // 二者完全等价
指针类型const形式的注意事项
int x = 3;const int *P = &x;// p = &y;正确// *p = 4;错误// const修饰*P,修改*p的值是错误的int x = 3;int *const p = &x;//p = &y;错误//const此时修饰p,修改p的指向是错误的const int x = 3;const int *const p = &x;//p = &y;*p = 4;都是错误的//此时const修饰p和*p,改变p指向或修改8P的值都是错误的
const与引用
int x = 3;const int &y = x;//x = 10;正确//y = 20;错误//此时const修饰引用y,修改y的值是错误的const int x = 3;int *y = &x;// 用可变的指针指向不可变的变量,此时可以通过改变指针指向修改x的值,风险太大被计算机禁止int x = 3;const int *y = &x;// 指针是静态的,只允许读,x是变量允许读写,计算机允许用权限较小的量承接权限较大的量
3.C++函数新特性
// 函数参数默认值void fun(int i,int j = 5,int k = 6);void fun(int i,int j = 5,int k );// 错误// 有默认值的参数必须在参数列表的最右端void fun(int i,int j = 5,int k = 10);//函数声明,此时可加默认值void fun(int i,int j,int k){ //函数定义时不加默认值 cout<<i<<j<<k;}fun(20);fun(20,30);fun(20,30,40);//无实参则用默认值,否则实参覆盖默认值//函数重载//在相同作用域下,同一函数名定义多个函数//彼此的参数个数或参数类型不同int getMax(int x,int y,int z){ //to do}double getMax(double x,double y){ //to do}//内联函数// 编译时将函数体代码和实参替代函数调用语句,节省调用过程的时间// 内联函数关键字 inlineinline int max(int a,int b,int c);int main(){ int i = 10,j = 20,k = 30,m; m = max(i,j,k); cout<<"max="<<m<<endl; return 0;}// 内联编译是建议的,由编译器决定// 需要逻辑简单,不可有循环等,大量调用时建议使用内联// 递归函数无法成为内联函数
4.总结
函数参数默认值,实参覆盖默认值
函数重载,名称相同参数可变
内联函数,inline 效率高 有条件
5.C++内存管理
内存的本质是操作系统掌控的资源,所谓管理内存便是资源的申请或管理
// 申请内存 new;// 归还内存 delete;int *p = new int;// 申请内存delete p;// 归还内存//申请和释放块内存int *arr = new int[10];delete []arr;//内存操作的注意事项//malloc与free、new与delete搭配使用int *p = new int[1000];if (p = NULL){ //事情内存失败}delete []p;p = NULL; //回收内存时将指针定义为空//内存管理实例#include<stdlib.h>#include<iostream>using namespace std;int main(void){ int *p = new int; if (NULL == P){ system("pause"); return 0; } *p = 20; cout<<*p<<endl; delete p; p = NULL; system("pause"); return 0;}#include<stdlib.h>#include<iostream>using namespace std;int main(void){ int *p = new int[1000]; if (NULL == p){ system("pause"); return 0; } p[0] = 10; p[1] = 20; cout<<p[0]<<p[1]<<endl; delete []p; p = NULL; system("pause"); return 0;}
0 0
- 学习《c++primer》笔记二
- c语言学习笔记二
- C语言学习笔记<二 >
- C/C++学习笔记(二)
- Objective-c 学习笔记(二)
- Objective C学习笔记(二)
- Objective-C 学习笔记二
- c学习笔记(二)
- 《Programming.C#》学习笔记二
- C语言学习笔记二
- 《C++.Primer.Plus》学习笔记(二)
- 《C语言宝典》学习笔记(二):概述
- ASP.Net 3.5学习笔记(C#)二
- C语言学习笔记(二)
- 嵌入式学习笔记-C语言(二)
- 《C和指针》学习笔记(二)
- C++学习笔记(Thinking in C++) 二
- c++primer学习笔记(二)
- iOS学习之——NSSet、NSMutableSet
- checkbox全选与全不选
- 图片压缩和渐进式处理
- YOLO
- 用Go构建Teamwork项目的9条教训
- C++学习笔记(二)
- x210---uboot第一阶段
- java面试题及答案(基础题122道,代码题19道)
- 基于Hubot打造自己的聊天机器人服务(二)
- 第11周项目3—警察和厨师 (2)
- AlertDialog 自定义
- js获取显示器分辨率
- 第10、11周项目5:摩托车继承自行车和机动车
- 第九周项目3