黑马培训班Day1
01Hello World
#include<iostream> // 标准输入输出流 in 输入 out 输出
using namespace std; //使用命名空间std 打开一个叫std房间
//函数入口地址
int main() {
//cou标准输出
//<<左移运算符
//endl 结束换行
cout << "hello world" << endl;
system("pause"); //阻塞功能
return EXIT_SUCCESS; //返回正常退出
}
02 双冒号作用域运算符
#define _CRT_SECURE_NO_WARINGS
#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;
}
03 namespace的使用
源文件namespace的 使用.CPP
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include "game1.h"
#include "game2.h"
void test01() {
LOL::goAtk();
KingGlory::goAtk();
}
//namespace 命名空间主要用途 用来解决命名冲突的问题
//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;
}
//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;
}
void test03() {
cout << "作用域A下的m_A为:" << A::m_A <<"作用域A下的m_B为"<<A::m_B <<endl;
}
int main() {
test04();
//test03();
//test02();
system("pause");
return EXIT_SUCCESS;
}
头文件game1.h
#pragma once
#include<iostream>
using namespace std;
namespace LOL {
void goAtk();
}
头文件game2.h
#pragma once
#include<iostream>
using namespace std;
namespace KingGlory {
void goAtk();
}
源文件 game1.cpp
#include "game1.h"
void LOL::goAtk() {
cout << "LOL攻击实现" << endl;
}
源文件 game2.cpp
#include "game2.h"
void KingGlory::goAtk() {
cout << "王者荣耀攻击实现" << endl;
}