c++初识
cout在std命名空间下
换行endl;
system("pause");//阻塞
return EXIT_SUCCESS;返回正常退出
#define _CRT_SECURE_NO_WARNINGS
//当使用老的不安全函数仍然能编译通过
面向对象三大特性:封装、继承、多态
双冒号作用域运算符
::代表作用域 如果前面什么都不添加 代表全局作用域
cout<<"atk="<<atk<<std::endl;
//atk为局部变量,endl为std作用域下的变量
cout<<"全局atk="<<::atk<<std::endl;
//atk为全局变量。
namespace命名空间
为了避免二义性,解决名称冲突,会在头文件里将同名函数定义到命名空间下即可。
//game1.h内部分代码
···
namespace KingGlory
{
void goAtk();
}
···
//game2.h内部分代码
···
namespace LOL
{
void goAtk();
}
···
//game1.cpp内部分代码
#include"game1.h"
#include"game2.h"
···
void KingGlory::goAtk()
{
cout << "王者荣耀攻击实现" << endl;
}
void LOL::goAtk()
{
cout << "LOL攻击实现" << endl;
}
命名空间下可以存放 : 变量、函数、结构体、类…
命名空间必须要声明在全局作用域
不可以在函数体内使用命名空间
命名空间可以嵌套命名空间
···
namespace B
{
int m_A = 10;
namespace C
{
int m_A = 20;
}
}
void test03()
{
cout << "B空间下的m_A = " << B::m_A << endl;
cout << "C空间下的m_A = " << B::C::m_A << endl;
}
···
命名空间是开放的,可以随时将新成员添加到命名空间下
再声明命名空间,作用是合并而不是覆盖。
命名空间可以匿名的
匿名命名空间是全局的,相当于是声明了静态变量。
namespace
{
int m_C = 1000;
int m_D = 2000;
//当写的命名空间的匿名的,相当于写了 static int m_C = 1000; static int m_D = 2000;
}
void test05()
{
cout << "m_C = " << m_C << endl;
cout << "m_D = " << ::m_D << endl;
}
命名空间可以起别名
namespace veryLongName
{
int m_E = 10000;
}
void test06()
{
namespace veryShortName = veryLongName;
cout << veryShortName::m_E << endl;
cout << veryLongName::m_E << endl;
//两个名都可以找到命名空间的变量
}
using声明
void test01()
{
int sunwukongId = 2;
//1、using声明
//using KingGlory::sunwukongId ;
//当using声明与 就近原则同时出现,出错,尽量避免
//就近原则:先选择调用函数体内的,再选择调用函数体外的
cout << sunwukongId << endl;
}
using编译指令
void test02()
{
//int sunwukongId = 2;
//2、using编译指令
using namespace KingGlory;
using namespace LOL;
//当using编译指令 与 就近原则同时出现,优先使用就近
//当using编译指令有多个,需要加作用域 区分
cout << KingGlory::sunwukongId << endl;
cout << LOL::sunwukongId << endl;
}
C++对C语言的增强和拓展
1、全局变量监测增强
int a;
int a = 10;//c++中会报错重复定义
C++检测出重定义,而C监测不出来。
2、函数监测增强
getRectS( w , h)//c中不会报错
{
return w *h;
}
void test01()
{
printf("%d\n", getRectS(10, 10, 10));
}
int getRectS(int w,int h)//c++中必须加上形参类型。
{
return w *h;
}
void test01()
{
printf("%d\n", getRectS(10, 10));
}
c中返回值不能检测,形参类型不能检测,函数调用个数不能检测,而c++中这些都可以实现。
3、类型转换监测增强
void test02()
{
char * p = malloc(64);//c中不会报错
}
void test02()
{
char * p = (char *)malloc(64);//cpp中需要强制类型转换
}
4、struct增强
c++可以放函数,创建结构体变量,可以简化关键字 struct,而c不可以
struct Person
{
int age;
void func()//C中会报错
{
age++;
}
};
5、bool类型拓展
C语言下,没有bool类型,而c++下有。
bool flag = true; //bool类型 代表 真和假 true ----真(1) false ----假(0)
void test04()
{
cout << sizeof(bool) << endl; //结果是1个字节
//flag = false;
//flag = 100; //将非0的数都为1,false为0;
cout << flag << endl;
}
6、三目运算符增强
void test05()
{
//?:
int a = 10;
int b = 20;
printf("ret = %d\n", a > b ? a : b);
(a < b ? a : b )= 100; // C++下返回的是变量 b = 100
printf("a = %d\n", a);
printf("b = %d\n", b);
}
7、const增强
全局const
c++与c结论一致
const int m_A = 100;
局部const
void test06()
{
//m_A = 200;
//int * p = (int *)&m_A;
//*p = 200;
//局部const
const int m_B = 100;
//m_B = 200;
int * p = (int *)&m_B;//修改失败
*p = 200;
cout << "m_B = " << m_B << endl;
int arr[m_B]; //C++下const修饰的变量称为常量
}
外部链接属性
C语言下的const修饰全局变量默认是外部链接属性
//test.c
const int g_a = 1000;
//main.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//外部链接属性:
int main(){
extern const int g_a;
//告诉编译器在外部有一个g_a
//报错:一个无法解析的外部命令:链接出的错
printf("g_a = %d\n", g_a);
system("pause");
return EXIT_SUCCESS;
}
而C++下的const修饰的全局变量默认是内部链接属性
//test.cpp
extern const int g_b = 1000;//可以加关键字extern提高作用域
//const int g_b=1000;//默认是内部链接属性
//main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int main(){
extern const int g_b;
cout << "g_b = " << g_b << endl;;
system("pause");
return EXIT_SUCCESS;
}
const分配内存情况
1、 对const变量取地址,会分配临时内存
void test01()
{
const int a = 10;//改不掉,因为没有分配内存
int * p = (int *)&a;
}
2、使用普通变量初始化const变量的时候
void test02()
{
int a = 10;
const int b = a;//b分配到栈上
//如果把a换成10就修改不成功,因为这时候把b放在符号表中,没有分配内存,没有地址
//然后分配了一个临时变量temp给b,让指针p指向b
int *p = (int *)&b;//定义一个一级指针储存b的地址
*p = 1000;//指针p指向的变量修改为1000
//分配内存了,所以修改成功
cout << "b = " << b << endl;
}
3、对于自定义的数据类型
struct Person
{
string m_Name;//需要包含头文件
int m_Age;
};
void test03()
{
const Person p={"xiaoming",20};//const变量必须初始化//p.m_Age = 10;//直接修改是失败的
Person * pp = (Person *)&p;
(*pp).m_Name = "Tom";//pp是一个指针
pp->m_Age = 10;
cout << "姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
}
尽量用const替换define
//define MAX 1024
//undifine 卸载宏
//const int max =1024;//const是有类型的而define没有类型,const有作用域,define没有作用域
引用的基本语法
引用的目的:起别名
引用的基本语法:
类型 &别名 = 原名;
引用必须初始化,并且引用一旦初始化后就不可以引向其他向量。
对数组建立引用
1、直接建立引用
int arr[10];
int(&pArr)[10] = arr;
for (int i = 0; i < 10; i++)
{
arr[i] = 100 + i;
}
for (int i = 0; i < 10; i++)
{
cout << pArr[i] << endl;
}
//用原名方式赋值,别名方式打印
2、先定义数组类型,再通过类型定义引用
typedef int(ARRAY_TYPE)[10];
//创建一个10个元素数组类型
//类型 &别名 = 原名
ARRAY_TYPE & pArr2 = arr;
//类型 ARRAY_TYPE 别名 pArr2
//别名可以起多个
for (int i = 0; i < 10; i++)
{
cout << pArr2[i] << endl;
}
参数的传递方式
1、值传递
void mySwap01(int a , int b)
{
int temp = a;
a = b;
b = temp;
/*cout << ":::a = " << a << endl;
cout << ":::b = " << b << endl;*/
}
//只能修改函数里的ab,不能修改函数外的ab
2、地址传递
void mySwap02(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
3、引用传递
void mySwap03(int &a , int &b) // int &a = a; int &b = b;
{
int temp = a;
a = b;
b = temp;
}
引用注意事项
1、引用必须引一块合法内存空间
void test02()
{
//int &a = 10;
2、不要返回局部变量的引用
int &ref = func();
cout << "ref = " << ref << endl;
cout << "ref = " << ref << endl;
//指向了一个局部变量
//第一次可以保留,第二次就释放掉了
}
3、函数返回值是引用的时候,可以当做左值去计算
int& func2()
{
static int a = 10;//没有被释放掉
return a;
}
void test03()
{
int &ref = func2();
cout << "ref = " << ref << endl;
cout << "ref = " << ref << endl;
cout << "ref = " << ref << endl;
cout << "ref = " << ref << endl;
//当函数的返回值是引用,可以作为左值去计算
func2() = 1000;
cout << "ref = " << ref << endl;
}
指针引用
p指向指针的指针 *p 指向的是person本体 **p person本体
struct Person
{
int age;
};
void allocateSpace(Person ** p)
{
*p = (Person *)malloc(sizeof(Person));//malloc返回的是void *,然后强制转换为一个Person*
(*p)->age = 10;
}
void test01()
{
Person * p = NULL;//定义一个空指针
allocateSpace(&p);//通过一段代码给它分配内存
cout << "p.age = " << p->age << endl;
}
//利用引用可以简化指针
//可以直接用同级指针的引用给同级指针分配空间
malloc动态内存空间分配函数,如果分配成功:则返回指向被分配内存空间的指针。不然,返回空指针NULL。malloc只管分配内存,并不能对其进行初始化。所以得到的一片新内存中,其值将是随机的。一般意义上:我们习惯性的将其初始化为NULL。当然,也可以用memset函数的。void *类型可以强转为任何其他类型的的指针。malloc则必须由我们计算字节数,并且在返回的时候强转成实际指定类型的指针。
引用的自动转换
#include<iostream>
using namespace std;
//发现是引用,转换为 int*const ref = &a
void testFunc(int& ref){
ref=100;
}
int main()
{
int a = 10;
int& aRef = a;
//自动转换为int* const aRef = &a;
//这也能说明引用为什么必须初始化
aRef = 20;//内部发现aRed是引用,自动帮我们转换为*aRef = 20;
cout<<"a:"<<endl;
cout<<"aRef:"<<aRef<<endl;
testFunc(a);
return EXIT_SUCCESS;
}
void test01()
{
//int &ref = 10;//引用必须要有一个合法空间
const int &ref = 10;
// 加了const之后,相当于写成
//int temp = 10;
//const int &ref = temp;
int *p = (int *)&ref;// int* = const int*
*p = 10000;
cout << ref << endl;//指针修改临时内存
}
void showValue(const int &a)//防止修改掉a
{
//a = 100000;//把外面的a修改掉了,因为引用指向的是外面的a
cout << "a = " << a << endl;
}
常量引用的使用场景 修饰函数中的形参,防止误操作
void test02()
{
int a = 100;
showValue(a);
}