Day27.C++01
001.第一个程序(Hello World!!)
#include<iostream>//标准输入输出库
using namespace std;//使用命名空间std(打开一个叫std的房间)
int main()
{
//cout 标准输出
//<< 左移运算符
//endl 结束换行
cout << "Hello World!" << endl;
system("pause");//阻塞功能
return EXIT_SUCCESS;
}
002.双冒号作用域运算符
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int atk = 200;
void test101()
{
int atk = 100;
cout << "攻击力:" << atk << endl;
//全局作用域,直接加::
cout << "全局攻击力:" << ::atk << endl;
}
int main()
{
test101();
system("pause");
return EXIT_SUCCESS;
}
003.namespace的使用
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include"game_1.h"
#include"game_2.h"
using namespace std;
void test301()
{
LOL::goAtk();
kingGlory::goAtk();
}
//namespace 命名空间主要用途,用来解决命名冲突的问题
//1、命名空间下,可以放函数、变量、结构体、类
//2、命名空间必须定义在全局作用下
//3、命名空间可以嵌套命名空间
//4、命名空间是开放的,可以随时往原先的命名空间添加内容
//5、匿名命名空间/无名命名空间
//当写了无名命名空间,相当于写了 static int m_C;static int m_D;
namespace
{
int m_C = 0;
int m_D = 0;
}
//6、命名空间可以起别名
namespace veryLongName
{
int m_A = 0;
}
void test302()
{
//起别名
namespace veryShortName = veryLongName;
cout << veryShortName::m_A << endl;
cout << veryLongName::m_A << endl;
}
int main()
{
//test301();
test302();
system("pause");
return EXIT_SUCCESS;
}
004.using声明和using编译指令
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
namespace kingGlory
{
int sunWuKongId = 10;
}
void test401()
{
int sunWuKongId = 20;
//using声明 注意避免二义性的问题
//写了using声明后,下面这行代码说明以后见到的sunWuKongId是用kingGlory下的
//但是编译器又有就近原则
//using kingGlory::sunWuKongId;
cout << sunWuKongId << endl;
}
//using编译指令
namespace LOL
{
int sunWuKongId = 30;
}
void test402()
{
//int sunWuKongId = 20;
//using 编译指令
using namespace kingGlory;//打开王者荣耀的房间
using namespace LOL;//带开LOL房间
//如果打开多个房间,也要避免二义性问题
cout << LOL::sunWuKongId << endl;
}
int main()
{
//test401();
test402();
system("pause");
return EXIT_SUCCESS;
}
005.C++对C语言的增强(全局变量、函数检测、类型转换、struct、bool、三目)
- C语言与C++对比:
C语言版本
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//1、全局变量检测增强
int a;
int a = 10;
//2、函数检测增强
int getRectS(w, h)
{
}
void test502()
{
getRectS(10, 10, 10);
}
//3、类型转换检测增强
void test503()
{
char* p = malloc(sizeof(int) * 64);//malloc返回值是void*
}
//4、struct增强
struct Person
{
int m_Age;
//void plusAge();//C语言中struct 不可以添加函数
};
void test504()
{
struct Person p1;//使用时必须加入struct关键字
}
//5、bool类型增强,C语言中没有bool类型
//bool flag;
//6、三目运算符增强
void test506()
{
int a = 10;
int b = 20;
printf("ret=%d", a > b ? a : b);
//(a > b ? a : b) = 100; //20=10 C语言返回的是值
//C语言中想模仿C++写
*(a > b ? &a : &b) = 100;
}
//7、const增强
//C语言中,const修饰的变量,是伪常量,编译器是会分配内存的
//C++中,const不会分配内存
const int m_A = 10;
void test507()
{
//m_A = 100;
const int m_B = 20;
//m_B = 200;
int* p = (int*)&m_B;
*p = 200;
printf("*p=%d m_B=%d\n", *p, m_B);
//int arr[m_B];//不可以初始化数组
}
int main(void)
{
//test506();
test507();
system("pause");
return EXIT_SUCCESS;
}
C++版本
#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 test602()
{
getRectS(10, 10);
}
//3、类型转换检测增强
void test603()
{
char* p = (char*)malloc(sizeof(int) * 64);//malloc返回值是void*
}
//4、struct增强
struct Person
{
int m_Age;
void plusAge() { m_Age++; };//C++中struct 可以添加函数
};
void test504()
{
Person p1;//使用时可以不加struct关键字
}
//5、bool类型增强,C语言中没有bool类型
bool flag;//只有真或者假 true代表真(非0) false代表假(0)
void test605()
{
cout << sizeof(bool) << endl;
flag = 100;
//bool类型 非0的值都转为1
cout << flag << endl;
}
//6、三目运算符增强
void test606()
{
int a = 10;
int b = 20;
cout << "ret = " << (a > b ? a : b) << endl;
(a > b ? a : b) = 100;//b = 100 C++中返回的是变量
//printf("ret=%d", a > b ? a : b);
}
//7、const增强
const int m_A = 10;
void test607()
{
const int m_B = 20;
//m_B = 200;
int* p = (int*)&m_B;
*p = 200;
cout << "*p=" << *p << endl;
cout << "m_B=" << m_B << endl;
int arr[m_B];//可以初始化数组
}
int main()
{
test607();
system("pause");
return EXIT_SUCCESS;
}
006.C语言中const与C++中的区别
test.c
const int a = 10;//C语言中默认是外部链接
test.cpp
extern const int a = 10;//C++语言中默认是内部链接,extern 提高其作用域
C语言中:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
extern const int a;//告诉编译器a在外部(可以在整个文件夹中寻找a)
printf("a = %d\n", a);
system("pause");
return EXIT_SUCCESS;
}
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;
}
007.const分配内存情况
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
//1、取地址会分配临时内存
//2、前面加extern 编译器会给const变量分配内存
void test901()
{
const int m_A = 10;
int* p = (int*)&m_A;
}
//3、用变量初始化 const 变量
void test902()
{
int a = 10;
const int b = a;//会分配内存(只要分配内存的,就可以通过地址改变其值)
int* p = (int*)&b;
*p = 1000;
cout << "b = " << b << endl;
}
//4、自定义数据类型 加const也会分配内存
struct Person_09
{
string m_Name;//该数据类型需要包含头文件 <string>
int m_Age;
};
void test903()
{
const Person_09 p1;
//p1.m_Name = "aaa";
Person_09* p = (Person_09*)&p1;
p->m_Name = "徐";
(*p).m_Age = 18;
cout << "姓名: " << p1.m_Name << "年龄: " << p1.m_Age << endl;
}
int main()
{
system("pause");
return EXIT_SUCCESS;
}
还有一点要注意:
尽量使用 const 来代替 #define
例如: #define MAX 1024
可替换为: const int max = 1024;
const和#define区别总结:
1. const有类型,可进行编译器类型安全检查。#define无类型,不可进行类型检查
2. const有作用域,而#define不重视作用域,默认定义处到文件结尾,如果定义在指定作用域下有效的常量,那么#define就不能用。
008.引用(起别名)
引用基本用法:
引用是C++对C的重要扩充,在C/C++中指针的作用基本都是一样的,但是在C++增加了另外一种给函数传递地址的途径,这就是引用传递
- 变量名实质上是一段连续内存空间的别名,是一个标号(门牌号)
- 程序中通过变量来申请并命名内存空间
- 通过变量的名字可以使用存储空间
基本语法:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//1、引用的基本语法 type& 别名 = 原名
void test1001()
{
int a = 10;
int& b = a;
b = 20;
cout << "b = " << b << endl;
cout << "a = " << a << endl;
}
//2、引用必须初始化
void test1002()
{
//int& a;//必须初始化
int a = 10;
int& b = a;//引用初始化之后不可以修改
int c = 20;
}
//3.对数组建立引用
void test1003()
{
int arr[10];
for (int i = 0; i < 10; ++i)
{
arr[i] = i;
}
//给数组起别名
int(&pArr1)[10] = arr;
for (int i = 0; i < 10; ++i)
{
cout << pArr1[i] << " ";
}
cout << endl;
//第二种方式起别名
typedef int(ARRAYREF)[10];
ARRAYREF& pArr2 = arr;
for (int i = 0; i < 10; ++i)
{
cout << pArr2[i] << " ";
}
cout << endl;
}
int main()
{
//test1001();
test1003();
system("pause");
return EXIT_SUCCESS;
}
009.参数的传递方式(引用传递)
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//引用传递(类似于传地址)
void mySwap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
void test1101()
{
int a = 10;
int b = 20;
mySwap(a, b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
int main()
{
test1101();
system("pause");
return EXIT_SUCCESS;
}
010.引用的注意事项
注意事项:
1. 引用必须引用一块合法的内存空间
2. 不要返回局部变量的引用
3. 必须在声明引用变量时进行初始化。
4. 引用初始化之后不能改变
还有一点补充:如果函数的返回值是引用,那么函数的调用可以作为左值。
引用的本质:
引用的本质在C++内部实现就是一个指针常量
示例:
int a = 10;
int& aRef = a;//自动转换为 int* const aref = &a; 这也能说明引用为什么必须初始化
aRef = 20;//内部发现aRef时引用,自动帮我们转换为:*aRef = 20;
011.指针引用
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
struct Person_13
{
int m_Age;
};
void allocatMemory(Person_13** p)
{
*p = (Person_13*)malloc(sizeof(Person_13));
(*p)->m_Age = 100;
}
void test1301()
{
Person_13* p = NULL;
allocatMemory(&p);
cout << "p的年龄: " << p->m_Age << endl;
}
//利用引用开辟空间
void allocatMemoryByRef(Person_13*& p)
{
p = (Person_13*)malloc(sizeof(Person_13));
p->m_Age = 200;
}
void test1302()
{
Person_13* p = NULL;
allocatMemoryByRef(p);
cout << "p的年龄: " << p->m_Age << endl;
}
int main(void)
{
test1301();
test1302();
system("pause");
return EXIT_SUCCESS;
}
012.常量引用
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
void test1401()
{
//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 test1402()
{
int a = 10;
showValue(a);
}
int main(void)
{
//test1401();
test1402();
system("pause");
return EXIT_SUCCESS;
}