1.C++和C的区别
-
创建源文件是.cpp 文件,头文件不变,依旧是.h文件
-
包含头文件的方式
-
包含自己的头文件没有任何区别 #include "myHead.h"
-
包含库目录
-
C语言中的: 采用原来方式包含可以,可以用C++包含方式,去掉.h 加个c
-
#include <stdio.h> //#include<cstdio>
-
iostream C++标准输入输出流的头文件
-
#include<iostream>
-
-
- 命名空间的引入
- 作用:可以增加标识符的使用率,C语言同一个作用域下不允许定义相同的标识符
- 命名语法
- 基本创建空间方法
-
#include<iostream> namespace MM{ int a=1; print("这是MM中的a:%d\n",a); } namespace GG{ int a=2; print("这是GG中的a:%d\n",a); }
-
- 用空间名限定去访问
- 作用域分辨符::
- 空间名限定,类名限定 ,告诉别人::后面东西是哪里的来
-
#include<iostream> namespace MM{ int a=1; void print{ printf("这是MM中的a:%d\n",a); } } namespace GG{ int a=2; void print{ printf("这是GG中的a:%d\n",a); } int main(){ MM::print();//a=1; GG::print();//a=2; MM::a=10; GG::a=20; MM::print();//a=10; MM::print();//a=20; while(1); return 0; }
-
用来标识全局变量
-
#include<iostream> int a=2; int main(){ int a=1; printf("%d\n", a); //采用的是就近原则 a=1; printf("%d\n", ::a); //全局变量 a=2 while(1); return 0; }
-
- 函数可以在空间中声明,在空间外实现
-
namespace GG { int a=2; void print(); //空间中声明 } //空间外实现 GG::void print(){ printf("这是GG中的a:%d\n",a); }
-
- 作用域分辨符::
- using语法 ,可以省略前缀的写法
-
using namespace 空间名; //就可以省略当前的空间名 //只在当前作用域下有效
-
-
空间名的嵌套与空间内容访问
-
#include<iostream> namespace A { int aa=1; namespace B { int bb=1; } } int main(){ A::B::bb=1001; using namespace A; aa = 1002; B::bb = 1004; using namespace A::B; bb=1002; return 0; }
-
- 基本创建空间方法
2. 基本输入和输出
-
包含头文件#include <iostream>
-
一般情况都会包含命名空间 using namespace std,避免std的前缀使用
-
输出: cout<< 做输出
-
#include <iostream> #include <string> using namespace std; int main(){ //1.基本变量打印不需要格式控制 cout << "ILoveyou\n"; char str[] = "ILoveyou\n"; cout << str; int a = 1; float b = 1.1; cout << a; cout << b; cout << "\n"; //2.如何一次性打印多个数据 cout << a << "\t" << (int)b/*类型转换*/ << "\n"; cout << a << "\t" << int(b) /*类型转换*/ << "\n"; //C语言运算符的隐式转换都是一样的 retuen 0; }
-
-
输入: cin>>做输入
-
#include <iostream> #include <string> using namespace std; int main(){ int iNum = 0; float fNum = 0.0f; //默认小数是double char str[] = "ILoveyou\n"; cout << "请输入一个整数,一个浮点数,一个字符串:"; //一个一个处理 //cin >> iNum; //cin >> fNum; //cin >> str; //一次处理多个 cin >> iNum >> fNum >> str; cout << iNum << "\t" << fNum << "\t" << str << "\n"; retuen 0; }
-
-
换行:<<endl
-
#include <iostream> #include <string> using namespace std; int main(){ //cout << "ILoveyou\n"; cout << "ILoveyou"<<endl; retuen 0; }
-
3. 新数据类型
-
bool类型
-
占用内存是一个字节
-
计算机非零表示成立,只有0或者指针空表示不成立
-
一般充当函数返回值,或者开关
-
正常输出是0和1
-
C++专有的赋值方式,false和true
-
bool bNum = 1234; cout << bNum << endl;//1 //boolalpha 用true和false方式打印bool类型 cout << boolalpha << bNum << endl;//true bool bNum = 0; cout << bNum << endl;//0 cout << boolalpha << bNum << endl;//false
-
-
指针的NULL C++种改为 nullptr
-
C++中常量要求更严格
-
#include <iostream> #include<string> using namespace std; void printStr(const char* str) //如果没有const,则不能传进常量字符串 { cout << str << endl; } int main(){ printStr("ILoveyou"); //形参必须要有const char str[] = "ILoveyou"; printStr(str); return 0; }
-
-
引用类型 &
-
理解为起别名
-
基本引用
-
函数参数(防止拷贝产生)
-
//类型名& 别名=要起别名的东西 //a和b使用的是同一段内存,非拷贝 int a=1; int& b=a; //a有另一个名字叫b ,a就是b,b就是a //int& x = 1; //直接报错,C++对const要求更严格 int aa = 1; const int& x = 1; //第一种写法:const修饰 const int& x2 = aa;
-
#include <iostream> using namespace std; void modifyA(int a) { a = 1001; } void modifyB(int& a) //int& a=实参; { a = 1001; } void swap(int& a, int& b) { int temp = a; a = b; b = temp; } int g_num = 1001; void modifyPointA(int* p) { p = &g_num; } void modifyPointB(int*& p) //int*& p=实参 { p = &g_num; } int main(){ int aa = 1; modifyA(aa); //拷贝本 cout << aa << endl; // aa=1 modifyB(aa); //任何传参都是赋值的方式传参 cout << aa << endl; // aa=1001 int bb = 22; swap(aa, bb);//aa=1001 bb=22 cout << aa << "\t" << bb << endl;//aa=22 bb=1001 int pa = 1008; int* p = &pa; modifyPointA(p); cout << *p << endl;//pa=1008 modifyPointB(p); cout << *p << endl;//pa=1001 return 0; }
-
-
-
常引用(右值引用)
-
//类型名&& 别名=右值 int&& a=1; //1就是a ,a就是1 int aa = 1; int&& xx = 1; //常量是右值(运算符的右边) //int&& xx2 = aa; //右值引用只能给右值起别名
-
#include <iostream> #include<string> using namespace std; void printRightValue(int&& a) { a += 11; //可以增加一个可以修改的功能 cout << a << endl; } void printRight(const int& a) { //a += 11; //常引用不能修改 cout << a << endl; } int main(){ printRightValue(11); printRight(11); return 0; }
-
-
函数返回值(增加右值使用)
-
不能返回局部变量引用
-
返回值就是一个值,这个值只能是右值
-
#include <iostream> #include<string> using namespace std; int g_num = 1001; int getData() { return g_num; } int& getValue() { return g_num; } int main(){ //getData() = 0; 错误 getValue() = 0; //返回引用表示返回变量本身 cout << g_num << endl; //g_num=0; return 0; }
-
-
-
自动推断类型auto类型: 必须根据赋值的数据推断类型,不能直接推断
-
#include <iostream> using namespace std; int Max(int a, int b) { return a > b ? a : b; } void printMax(int(*pMax)(int, int), int a, int b) { cout << pMax(1, 2) << endl; } struct Node { int data; }; int main(){ auto a = 1; //auto b; //错误,auto不能定义变量 auto pMax = Max; cout << pMax(1, 2) << endl; auto ppMax = printMax; //等效下面代码 void (*p)(int(*)(int, int), int, int) = printMax; ppMax(pMax, 2, 3); //decltype struct Node pStruct = { 1 }; auto pS = &pStruct; //struct Node* auto pA = pStruct; //struct Node return 0; }
-
4. 函数思想
-
内联思想 inline关键字
-
什么样的函数可以成为inline,短小精悍
-
在结构体中或者类种实现的函数默认内联(知道即可)
-
-
函数重载: C++允许同名不同参数函数存在
-
重载调用,优先调用类型一致
-
参数数目不同
-
void print(int a) { cout << a << endl; } void print(int a, int b) { cout << a + b << endl; }
-
-
参数类型不同
-
void print(int a, int b) { cout << a + b << endl; } void print(int a, char b) { cout << a + b << endl; }
-
-
参数顺序不同(一定建立在不同类型的基础上)
-
void print(int a, int b) { cout << a + b << endl; } //和上面不是顺序不同 //void print(int b, int a) //{ // cout << a + b << endl; //} void print(int a, char b) { cout << a + b << endl; } void print(char a, int b) { cout << a + b << endl; }
-
-
-
函数缺省: C++中允许给函数形参初始化
-
缺省顺序 必须从右往左缺省,缺省的参数中间不能存在没有缺省的
-
void printData(int a=1, int b=2, int c=3, int d=4) { cout << a + b + c + d << endl; } //错误 void printData(int a=1, int b, int c=3, int d=4) { cout << a + b + c + d << endl; } //正确 void printData(int a, int b, int c=3, int d=4) { cout << a + b + c + d << endl; }
-
-
没有参入参数,使用的是默认值
-
#include <iostream> using namespace std; void printData(int a=1, int b=2, int c=3, int d=4) { cout << a + b + c + d << endl; } int main() { printData(); //a=1 b=2 c=3 d=4 printData(10); //a=10 b=2 c=3 d=4 printData(10, 20); //a=10 b=20 c=3 d=4 printData(10, 20, 30); //a=10 b=20 c=30 d=4 printData(10, 20, 30, 40);//a=10 b=20 c=3 d=40 return 0; }
-
-