C++对C的扩展
1#include
1.1using namespace std;
1.2cout << “hello …” << endl;
1.3system(“pause”)
1.4retrun 0
01 Hello World.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream> //标准输入输出流 in输入 out 输出
//using namespace std;//使用命名空间 std 打开一个叫std房间
//函数入口地址
int main()
{
// cout 标准的输出
// << 左移运算符
// endl 结束换行
std::cout << "hello world" << 123456 << std::endl;
system("pause"); //阻塞功能
return EXIT_SUCCESS; //返回正常退出
}
2::双冒号作用域运算符
2.1全局作用域 直接加::
02 双冒号作用域运算符.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int atk = 200;
void test01()
{
int atk = 100;
cout << "攻击力为 : " << atk << endl;
//双冒号 作用域运算符 ::全局作用域
cout << "全局攻击力为 : " << ::atk << endl;
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
3namespace 命名空间
3.1用途 解决名称冲突问题
3.2必须在全局作用域下声明
3.3命名空间下可以放入 函数、变量、结构体、类…
3.4命名空间可以嵌套命名空间
3.5命名空间是开放的,可以随时加入新的成员
3.6匿名命名空间 static
3.7可以起别名
03 namespace的使用.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include "game1.h"
#include "game2.h"
//namespace命名空间主要用途 用来解决命名冲突的问题
void test01()
{
LOL::goAtk();
KingGlory::goAtk();
}
//1、命名空间下 可以放函数、变量、结构体、类
namespace A
{
void func();
int m_A = 20;
struct Person
{
};
class Animal{};
namespace B
{
int m_A = 10;
}
}
//2、命名空间必须定义在全局作用域下
//3、命名空间可以嵌套命名空间
void test02()
{
cout << "作用域B下的m_A为: " << A::B::m_A << endl;
}
//4、命名空间是开放的,可以随时往原先的命名空间添加内容
namespace A //此A命名空间会和上面的命名空间A进行合并
{
int m_B = 1000;
}
void test03()
{
cout << "A::下的m_A为" << A::m_A << " m_B为: " << A::m_B << endl;
}
//5、无名、匿名命名空间
namespace
{
int m_C = 0;
int m_D = 0;
}
//当写了无名命名空间,相当于写了 static int m_C ; static int m_D;
//只能在当前文件内使用
//6、命名空间可以起别名
namespace veryLongName
{
int m_A = 0;
}
void test04()
{
//起别名
namespace veryShortName = veryLongName;
cout << veryLongName::m_A << endl;
cout << veryShortName::m_A << endl;
}
int main(){
//test02();
//test03();
test04();
system("pause");
return EXIT_SUCCESS;
}
game1.h
#include <iostream>
using namespace std;
namespace LOL
{
void goAtk();
}
game1.cpp
#include "game1.h"
void LOL::goAtk()
{
cout << "LOL攻击实现" << endl;
}
game2.h
#include <iostream>
using namespace std;
namespace KingGlory
{
void goAtk();
}
game2.cpp
#include "game2.h"
void KingGlory::goAtk()
{
cout << "王者农药攻击实现" << endl;
}
4using声明和using编译指令
4.1 using LOL:: sunwukongID;
4.2如果局部范围内还有 sunwukongID,会出现二义性问题,要注意避免
4.3 编译指令
4.4using namespace LOL
4.5如果局部范围内还有 sunwukongID ,使用局部的ID
4.6如果打开多个房间,那么也要注意二义性问题
04 using声明和using编译指令.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
namespace KingGlory
{
int sunwukongId = 10;
}
void test01()
{
int sunwukongId = 20;
//using 声明 注意避免二义性问题
//写了using声明后 下面这行代码说明以后看到的sunwukongId 是用KingGlory下的
//但是 编译器又有就近原则
//二义性
//using KingGlory::sunwukongId;
cout << sunwukongId << endl;
}
//using编译指令
namespace LOL
{
int sunwukongId = 30;
}
void test02()
{
//int sunwukongId = 20;
//using编译指令
using namespace KingGlory; //打开王者荣耀房间
using namespace LOL;//打开LOL房间
//如果打开多个房间,也要避免二义性问题
cout << LOL::sunwukongId << endl;
}
int main(){
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
5C++对C语言增强
5.1全局变量检测增强
5.2函数检测增强
5.2.1参数类型检测
5.2.2返回值检测
5.2.3传参个数检测
5.3类型转换检测增强
5.3.1malloc返回void* ,C中可以不用强转,C++必须强转
5.4struct增强
5.4.1C中不许有函数 C++可以
5.4.2使用C必须加关键字 struct ,C++可以不加
5.5bool数据类型增强
5.5.1C没有 C++有
5.5.2true 真 false假
5.5.3sizeof 1
5.6三目运算符增强
5.6.1C中返回的是值
5.6.2C++中返回的是变量
5.7const增强
5.7.1C语言中const是伪常量,可以通过指针修改
5.7.2C++中const会放入到符号表中
5.7.3C语言中const默认是外部链接,C++中const默认是内部链接
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
extern const int a; //告诉编译器在a在外部
printf("a = %d \n ", a);
system("pause");
return EXIT_SUCCESS;
}
test.c
const int a = 10; //C语言中默认const是外部链接
C++
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int main(){
extern const int a;
cout << a << endl;
system("pause");
return EXIT_SUCCESS;
}
5.7.4const分配内存情况
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <string>
using namespace std;
//1、const分配内存 取地址会分配临时内存
//2、extern 编译器也会给const变量分配内存
void test01()
{
const int m_A = 10;
int * p = (int*)&m_A; //会分配临时内存
}
//3、 用普通变量初始化 const 的变量
void test02()
{
int a = 10;
const int b = a; //会分配内存
int * p = (int *) &b;
*p = 1000;
cout << "b = " << b << endl;
}
//4、 自定义数据类型 加const也会分配内存
struct Person
{
string m_Name; //姓名
int m_Age;
};
void test03()
{
const Person p1;
//p1.m_Name = "aaa";
Person * p = (Person*)&p1;
p->m_Name = "德玛西亚";
(*p).m_Age = 18;
cout << "姓名: " << p1.m_Name << " 年龄: " << p1.m_Age << endl;
}
int main(){
//test02();
test03();
system("pause");
return EXIT_SUCCESS;
}
5.7.4.1对变量取地址,会分配临时内存
5.7.4.2extern关键字下的const会分配内存
5.7.4.3用普通变量初始化const变量
5.7.4.4自定义数据类型会分配内存
5.7.5尽量用const代替define
5.7.5.1define宏没有作用域概念
5.7.5.2define宏常量没有类型
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//1、全局变量检测增强
int a;
int a = 10;
//2、函数检测增强
int getRectS(w, h)
{
}
void test02()
{
getRectS(10, 10, 10);
}
//3、类型转换检测增强
void test03()
{
char * p = malloc(sizeof(64)); //malloc返回值是void*
}
//4、struct 增强
struct Person
{
int m_Age;
//void plusAge(); //c语言中struct不可以加函数
};
void test04()
{
struct Person p1; //使用时候必须加入struct关键字
}
//5、 bool类型增强 C语言中没有bool类型
//bool flag;
//6、三目运算符增强
void test06()
{
int a = 10;
int b = 20;
printf("ret = %d \n", a > b ? a : b);
//a > b ? a : b = 100; // 20 = 100 C语言返回的是值
//C语言中想模仿C++写
*(a > b ? &a : &b) = 100;
printf("a = %d ,b = %d \n", a, b);
}
//7、 const增强
const int m_A = 10; //收到保护,不可以改
void test07()
{
//m_A = 100;
const int m_B = 20; //伪常量
//m_B = 100;
int * p = (int *)&m_B;
*p = 200;
printf("*p = %d , m_B = %d \n", *p, m_B);
//int arr[m_B]; 不可以初始化数组
}
int main(){
// test06();
test07();
system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//1、全局变量检测增强
//int a;
int a = 10;
//2、函数检测增强 ,参数类型增强,返回值检测增强,函数调用参数检测增强
int getRectS(int w, int h)
{
return w*h;
}
void test02()
{
getRectS(10, 10);
}
//3、类型转换检测增强
void test03()
{
char * p = (char*)malloc(sizeof(64)); //malloc返回值是void*
}
//4、struct 增强
struct Person
{
int m_Age;
void plusAge(){ m_Age++; }; //c++中struct可以加函数
};
void test04()
{
Person p1; //使用时候可以不加struct关键字
p1.m_Age = 10;
p1.plusAge();
cout << p1.m_Age << endl;
}
//5、 bool类型增强 C语言中没有bool类型
bool flag = true; //只有真或假 true代表 真(非0) false 代表假(0)
void test05()
{
cout << sizeof(bool) << endl;
flag = 100;
//bool类型 非0的值 转为 1 ,0就转为0
cout << flag << endl;
}
//6、三目运算符增强
void test06()
{
int a = 10;
int b = 20;
cout << "ret = " << (a < b ? a : b) << endl;
(a < b ? a : b) = 100; //b = 100 C++中返回的是变量
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
const int m_A = 10; //收到保护,不可以改
void test07()
{
const int m_B = 20; //真正常量
//m_B = 100;
int * p = (int *)&m_B;
*p = 200;
cout << "*p = " << *p << endl;
cout << "m_B = " << m_B << endl;
int arr[m_B]; //可以初始化数组
}
int main(){
//test04();
// test05();
// test06();
test07();
system("pause");
return EXIT_SUCCESS;
}
6引用基本语法
6.1.1用途起别名
6.1.2Type &别名 = 原名
6.1.3引用必须初始化
6.1.4一旦初始化后 不能修改
6.1.5对数组建立引用
6.2参数3种传递方式
6.2.1值传递
6.2.2地址传递
6.2.3引用传递
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
void mySwap(int a, int b)
{
int tmp = a;
a = b;
b = tmp;
cout << "mySwap::a = " << a << endl;
cout << "mySwap::b = " << b << endl;
}
void test01()
{
int a = 10;
int b = 20;
mySwap(a, b); //值传递
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
//地址传递
void mySwap2(int * a, int * b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void test02()
{
int a = 10;
int b = 20;
mySwap2(&a, &b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
//引用传递 类似传地址
void mySwap3(int &a, int &b) //&a = a &b = b
{
int tmp = a;
a = b;
b = tmp;
}
void test03()
{
int a = 10;
int b = 20;
mySwap3(a, b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
//引用的注意事项
//1、 引用必须引一块合法的内存空间
//2、不要返回局部变量的引用
int& doWork()
{
int a = 10;
return a;
}
void test04()
{
//int &a = 10; // 引用必须引一块合法的内存空间
int &ret = doWork();
cout << "ret = " << ret << endl; //第一次10是编译器做了优化
cout << "ret = " << ret << endl;
cout << "ret = " << ret << endl;
cout << "ret = " << ret << endl;
cout << "ret = " << ret << endl;
}
int& doWork2()
{
static int a = 10;
return a;
}
void test05()
{
int &ret = doWork2();
//如果函数的返回值是引用,那么这个函数调用可以作为左值
doWork2() = 1000; //相当于写了 a = 1000;
}
int main(){
//test01();
//test02();
//test03();
//test04();
test05();
system("pause");
return EXIT_SUCCESS;
}
6.3注意事项,不要返回局部变量的引用
6.4如果函数返回值是引用,那么函数的调用可以作为左值
6.5引用的本质 就是一个指针常量
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//1、引用基本语法 Type &别名 = 原名
void test01()
{
int a = 10;
int &b = a;
b = 20;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
//2、引用必须初始化
void test02()
{
//int &a; 必须初始化
int a = 10;
int &b = a; //引用初始化后不可以修改了
int c = 20;
b = c; //赋值!!!
}
//3、对数组建立引用
void test03()
{
int arr[10];
for (int i = 0; i < 10;i++)
{
arr[i] = i;
}
//给数组起别名
int(&pArr)[10] = arr;
for (int i = 0; i < 10;i++)
{
cout << pArr[i] << " ";
}
cout << endl;
//第二种方式 起别名
typedef int(ARRAYREF)[10]; //一个具有10个元素的int类型的数组
ARRAYREF & pArr2 = arr;
for (int i = 0; i < 10; i++)
{
cout << pArr2[i] << " ";
}
cout << endl;
}
int main(){
//test01();
test03();
system("pause");
return EXIT_SUCCESS;
}
7指针的引用
7.1用一级指针引用 可以代替二级指针
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
struct Person
{
int m_Age;
};
void allocatMemory(Person ** p) // **p 具体的Person对象 *p 对象的指针 p 指针的指针
{
*p = (Person *)malloc(sizeof(Person));
(*p)->m_Age = 100;
}
void test01()
{
Person * p = NULL;
allocatMemory(&p);
cout << "p的年龄: " <<p->m_Age << endl;
}
//利用指针引用开辟空间
void allocatMemoryByRef(Person* &p)
{
p = (Person*)malloc(sizeof(Person));
p->m_Age = 1000;
}
void test02()
{
Person * p = NULL;
allocatMemoryByRef(p);
cout << "p的年龄:" << p->m_Age << endl;
}
int main(){
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
8常量引用
8.1使用场景 修饰形参为只读
8.2const int &a = 10;会分配内存
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
void test01()
{
//int &ref = 10;//引用了不合法的内存,不可以
const int &ref = 10; //加入const后 ,编译器处理方式为: int tmp = 10; const int &ref = tmp;
//ref = 10;
int * p = (int*)&ref;
*p = 1000;
cout << "ref = " << ref << endl;
}
//常量引用使用场景 用来修饰形参
void showValue(const int &val)
{
//val += 1000; //如果只是想显示内容,而不修改内容,那么就用const修饰这个形参
cout << "val = " << val << endl;
}
void test02()
{
int a = 10;
showValue(a);
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}