【C++】初识C++

【C++】初识C++



前言

本篇文章将详细讲到C++的初识,双冒号作用域运算符,namespace命名空间,using声明以及using编译指令,C++对C语言的增强和扩展,C++下const 修饰全局变量,const 分配内存情况,引用基本语法,参数的传递形式,指针引用,常量的引用。


一、C++的初识

#include<iostream>	//标准输入输出流   i - input 输入  o - output 输出  stream 流 相当于stdio.h
using namespace std;//使用  标准  命名空间

//程序入口函数
int main()
{
	//cout 标准输出流对象
	// << 左移  在C++下有了新的寓意 用与在cout后拼接输出的内容
	// endl  --- end line  刷新缓冲区 并且换行
	cout << "hello" << endl;

	system("pause"); //阻塞
	return EXIT_SUCCESS;  //返回正常退出

#include; 预编译指令,引入头文件iostream.
using namespace std; 使用标准命名空间
cout << “hello world”<< endl; 和printf功能一样,输出字符串”hello wrold”


二、双冒号作用域运算符

通常情况下,如果有两个同名变量,一个是全局变量,另一个是局部变量,那么局部变量在其作用域内具有较高的优先权,它将屏蔽全局变量。
::代表作用域 如果前面什么都不添加 代表全局作用域

#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
//using namespace std;

int atk = 999;

void test01()
{
	int atk = 1000;
	std::cout << "attack =" << atk << std::endl;
	std::cout << "全局 atk = " << ::atk << std::endl;

}

int main()
{
	test01(); 
	 
	system("pause");
	return EXIT_SUCCESS; 

}

三、namespace命名空间

  1. 命名空间用途 : 解决名称冲突
  2. 命名空间下 可以放 变量 函数 结构体 类 …
  3. 命名空间 必须要声明在全局作用域下
  4. 命名空间可以嵌套命名空间
  5. 命名空间是开放的,可以随时给命名空间添加新成员
  6. 命名空间可以是匿名的
  7. 命名空间可以起别名
  • game1.h
#pragma once
# include <iostream>
using namespace std;
namespace KingGlory
{
	void goAtk();
}
  • game1.c
#define  _CRT_SECURE_NO_WARNINGS 1
# include "game1.h"

void KingGlory::goAtk() 
{
	cout << "王者荣耀攻击实现" << endl;
}
  • game2.h
#pragma once
#include <iostream>
using namespace std;
namespace LOL
{
	void goAtk();
}
  • game2.c
#define  _CRT_SECURE_NO_WARNINGS 1
# include "game2.h"
void LOL::goAtk()
{
	cout << "LOL攻击实现" << endl;
}
#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include "game1.h"
#include "game2.h"

//1. 命名空间用途 : 解决名称冲突
void test01()
{
	KingGlory::goAtk();
	LOL::goAtk();
}

//2.命名空间下 可以放 变量 函数 结构体 类 ...
namespace A
{
	int m_A;
	void func();
	struct Person
	{};
	class Animal
	{};
}

//3. 命名空间 必须要声明在全局作用域下
void test02()
{
	//namespace B {}; // 不可以命名到局部作用域
}

//4. 命名空间可以嵌套命名空间
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;
}

//5. 命名空间是开放的,可以随时给命名空间添加新成员
namespace B {
	int m_B = 100;
}
void test04()
{
	cout << "B空间下的m_A = " << B::m_A << endl;
	cout << "B空间下的m_B = " << B::m_B << endl;
}

//6.命名空间可以是匿名的
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;
}

//7、命名空间可以起别名
namespace veryLongName
{
	int m_E = 10000;
	void func()
	{
		cout << "aaa" << endl;
	}
}

void test06()
{
	namespace veryShortName = veryLongName;
	cout << veryLongName::m_E << endl;
	cout << veryShortName::m_E << endl;

}

int main()
{
	//test01();
	//test03();
	//test04();
	//test05();
	//test06();
	system("pause");
	return EXIT_SUCCESS;
}

四、using声明以及using编译指令

当using声明与 就近原则同时出现,出错,尽量避免
当using编译指令 与 就近原则同时出现,优先使用就近
当using编译指令有多个,需要加作用域 区分

#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;

namespace KingGlory
{
	int sunwukongId = 1;

}

namespace LOL
{
	int sunwukongId = 3;
}

void test01()
{
	int sunwukongId = 2;
	//1.using 声明
	//using KingGlory::sunwukongId;

	//当using声明与 就近原则同时出现,出错,尽量避免
	cout << sunwukongId << endl;
}

void test02()
{
	int sunwukongId = 2;
	//2.using编译指令
	using namespace KingGlory;
	using namespace LOL;
	//当using编译指令  与  就近原则同时出现,优先使用就近
	//当using编译指令有多个,需要加作用域 区分
	cout << sunwukongId << endl;
	cout << KingGlory::sunwukongId << endl;
	cout << LOL::sunwukongId << endl;
	
}

五、C++对C语言的增强和扩展

1、全局变量检测增强 C++检测出重定义
2、函数检测增强 返回值检测、形参类型检测、函数调用参数个数
3、类型转换检测增强
4、struct增强 C++可以放函数,创建结构体变量,可以简化关键字 struct
5、bool类型扩展 C语言下 没有这个类型 C++有bool类型
6、三目运算符增强
7、const增强

#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;

//1、全局变量检测增强  C++检测出重定义
int a;
//int a = 10;

//2、函数检测增强 返回值检测、形参类型检测、函数调用参数个数
int getRectS(int w, int h)
{

	return w * h;
}
void test01()
{
	printf("%d\n", getRectS(10, 19));
}

//3、类型转换检测增强
void test02()
{
	char* p =(char*) malloc(64);
}

//4、struct增强  C++可以放函数,创建结构体变量,可以简化关键字 struct
struct Person
{
	int age;
	void func()
	{
		age++;
	}
};

void test03()
{
	Person p;
	p.age = 17;
	p.func();
	cout << "p的age = " << p.age << endl;
}

//5、bool类型扩展  C语言下 没有这个类型  C++有bool类型
bool flag = true; // bool类型 代表  真和假   true  ---- 真(1)    false  ---- 假(0)

void test04()
{
	cout << sizeof(bool) << endl; //结果是一个字节
	flag = false;
	cout << flag << endl;
	flag = 100; //将非0的数都转为1
	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  //结合性 b = 100
	
	printf("a = %d\n", a);
	printf("b = %d\n", b);
}

//7、const增强
//全局const   和C语言结论一致
const int m_A = 100;
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;//100   

	int arr[m_B]; //C++下const修饰的变量 称为常量 ,可以初始化数组

}

int main() {
	//test01();
	//test03();
	//test04();
	//test05();
	test06();


	system("pause");
	return EXIT_SUCCESS;
}

在这里插入图片描述


六、C++下const 修饰全局变量

C语言下的const修饰的全局变量默认是外部链接属性
C++下的const修饰的全局变量默认是内部链接属性

  • test.c
const int g_b = 1000;
  • C++下的const修饰的全局变量默认是内部链接属性.cpp
#define  _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;


int main()
{
	extern const int g_b;

	cout << "g_b = " << g_b << endl;

	system("pause");
	return EXIT_SUCCESS;
}

无法打印出g_b的值 在test.c 文件中 const int g_b = 1000 前加extern 即可在外部文件访问到g_b


七、const 分配内存情况

1、对const变量 取地址 ,会分配临时内存
2、使用普通变量 初始化 const变量
3、对于自定义数据类型

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <string>

//1、对const变量 取地址 ,会分配临时内存  
void test01()
{
	const int a = 10;
	int* p = (int*)&a;
}

//2、使用普通变量  初始化 const变量
void test02()
{
	int a = 10;
	const int b = a;

	int* p = (int*)&b;
	*p = 1000;

	cout << "b = " << b << endl;

}

//3、对于自定义数据类型 
struct Person
{
	string m_Name;
	int m_Age;
};
void test03()
{
	const Person p;
	//p.m_Age = 10;

	Person* pp = (Person*)&p;
	pp->m_Name = "Tom";
	pp->m_Age = 10;

	cout << "姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl; //无法修改
}


int main() {

	test02();
	//test03();
	system("pause");
	return EXIT_SUCCESS;
}

八、引用基本语法

引用必须要初始化
引用一旦初始化后,就不可以引向其他变量

#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;


void test01()
{
	int a = 10;
	int& b = a;

	b = 100;
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
}

void test02()
{
	int a = 10;
	//int &b; //引用必须要初始化

	int& b = a;

	//引用一旦初始化后,就不可以引向其他变量

	int c = 100;

	b = c; // 赋值

	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;
}


//对数组建立引用
void test03()
{
	//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];
	//类型 &别名 = 原名
	ARRAY_TYPE& pArr2 = arr;

	for (int i = 0; i < 10; i++)
	{
		cout << pArr2[i] << endl;
	}
}


int main() {

	//test01();
	//test02();
	test03();

	system("pause");
	return EXIT_SUCCESS;
}

九、参数的传递形式

1、值传递
2、地址传递
3、引用传递

#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;

//1、值传递
void mySwap01(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;

	/*cout << ":::a = " << a << endl;
	cout << ":::b = " << b << endl;*/
}

//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;
}

void test01()
{
	int a = 10;
	int b = 20;
	mySwap01(a, b);
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;

	mySwap02(&a, &b);
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;

	mySwap03(a, b);
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
}


int main() {
	test01();
	//test02();
	//test03();



	system("pause");
	return EXIT_SUCCESS;
}

引用的注意事项:
1、引用必须引一块合法内存空间
2、不要返回局部变量的引用
当函数返回值是引用,那么函数的调用可以作为左值

int& func()
{
	int a = 10;
	return a;
}
//引用注意事项
void test02()
{
	//1、引用必须引一块合法内存空间
	//int &a = 10;

	//2、不要返回局部变量的引用
	int& ref = func();
	cout << "ref = " << ref << endl;
	cout << "ref = " << ref << endl;
}

int& func2()
{
	static int a = 10;
	return a;
}
int& func2()
{
	static int a = 10;
	return a;
}

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;


}

十、指针引用

引用的本质在c++内部实现是一个指针常量
Type& ref = val; // Type* const ref = &val

#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;

struct Person
{
	int age;
};

void allocateSpace(Person** pp)
{
	//p指向指针的指针    *p  指针 指向的是person 本体   **p  person本体
	*pp = (Person*)malloc(sizeof(Person*));
	(*pp)->age = 18;
}


void test01()
{
	Person* p = NULL;
	allocateSpace(&p);

	cout << "p.age = " << p->age << endl;
}



void allocateSpace2(Person*& pp) //Person*& pp = p
{
	pp = (Person*)malloc(sizeof(Person*));
	pp->age = 19;
}

void test02()
{
	Person* p = NULL;
	allocateSpace2(p);

	cout << "p.age = " << p->age << endl;

}


int main() {

	//test01();
	test02();
	system("pause");
	return EXIT_SUCCESS;
}

十一、常量的引用

常量引用的使用场景 修饰函数中的形参,防止误操作

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

void test01()
{
	//int& ref = 10;

	const int& ref = 10; //加了const之后,相当于写了int temp = 10  const int& ref = temp

	int* p = (int*)&ref;
	*p = 10000;

	cout << ref << endl;
}

void showValue(const int &a)
{
	//a = 10000;

	cout << "a= " << a << endl;
}

//常量引用的使用场景 修饰函数中的形参,防止误操作
void test02()
{
	int a = 100;
	showValue(a);

}

int main() {

	test01();
	//test02();
	system("pause");
	return EXIT_SUCCESS;
}

总结

到这里这篇文章的内容就结束了,谢谢大家的观看,如果有好的建议可以留言喔,谢谢大家啦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值