1、变量、声明、引用(C++核心语法)

1 C++概述

1.1 C++两大编程思想
	1.1.1 面向对象
	1.1.2 泛型编程
1.2移植性和标准
	1.2.1 ANSI 在1998制定出C++第一套标准

2 c++初识

2.1 引入头文件  #include <iostream> 标准输入输出流
2.2 使用标准命名空间   using  namespace  std;
2.3 标准输出流对象 cout << “..” << 1234 << 3.14 << endl;
2.4 面向对象三大特性
	2.4.1 封装、继承、多态(一个接口,多种方法)

3 双冒号作用域运算符

3.1 ::代表作用域  如果前面什么都不添加 代表全局作用域
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
//using namespace std;

int atk = 1000;
void test01()
{
   
	int atk = 2000;
	std::cout << "atk = " << atk << std::endl;
	// ::代表作用域  如果前面什么都不添加 代表全局作用域
	std::cout << "全局 atk = " << ::atk << std::endl;
}

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

4 namespace命名空间

4.1 命名空间用途:解决名称冲突
4.2 命名空间下可以存放 : 变量、函数、结构体、类…
4.3 命名空间必须要声明在全局作用域
4.4 命名空间可以嵌套命名空
4.5 命名空间是开放的,可以随时将新成员添加到命名空间下
4.6 命名空间可以匿名的
4.7 命名空间可以起别名

//namemain.c

#define _CRT_SECURE_NO_WARNINGS
#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 << veryShortName::m_E << endl;
	cout << veryLongName::m_E << endl;
}

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

//game1.h

#include <iostream>
using namespace std;

namespace KingGlory
{
   
	void goAtk();
}

//game1.cpp

#include "game1.h"

void KingGlory::goAtk()
{
   
	cout << "王者荣耀攻击实现" << endl;
}

//game2.h

#include <iostream>
using namespace std;
namespace LOL
{
   
	void goAtk();
}

//game2.cpp
#include "game2.h"
void  LOL::goAtk()
{
   
	cout << "LOL攻击实现" << endl;

}

5 using声明以及using编译指令

5.1 using声明
	5.1.1 using KingGlory::sunwukongId
	5.1.2 当using声明与 就近原则同时出现,出错,尽量避免
5.2 using编译指令
	5.2.1 using namespace KingGlory;
	5.2.2 当using编译指令  与  就近原则同时出现,优先使用就近
	5.2.3 当using编译指令有多个,需要加作用域 区分
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值