#include <iostream> // iostream:标准输入输出流 i: in(输入) o: out(输出) stream(流)
using namespace std; // 使用命名空间 std: 打开一个叫 std 的房间;
int main() {
// cout 标准的输出
// << 左移运算符
// endl(end line): 结束换行
cout << "hello visual studio" << 123456 << endl;
system("pause"); // 阻塞功能
return EXIT_SUCCESS; // 返回正常退出
}
//#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int atk = 200;
void atkTest() {
int atk = 100;
cout << "攻击力为:" << atk << endl; // atk 为局部变量
cout << "攻击力为:" << ::atk << endl; // ::atk 为全局变量
}
int main() {
atkTest();
return EXIT_SUCCESS;
}
#include <iostream>
using namespace std;
#include "game1.h"
#include "game2.h"
void test01() {
LOL::goAtk();
KingGlory::goAtk();
}
// namespace 命名空间主要用途 用来解决命名冲突的问题;
// 1. 命名空间下 可以放函数、变量、结构体、类;
// 2. 命名空间必须定义在全局作用域下;
// 3. 命名空间可以嵌套命名空间;
namespace A {
void function();
int m_A = 20;
struct Person {};
class MyClass {};
namespace B {
int m_A = 10;
}
void function() {
cout << "A::function" << endl;
}
}
// 4. 命名空间是开放的, 可以随时往原先的命名空间添加内容;
namespace A {
int m_B = 100;
}
void function() {
cout << "function" << endl;
}
// 5. 匿名命名空间 (只能再当前文件内使用)
namespace {
int m_C = 0; // 相当于 static int m_C
int m_D = 0; // 相当于 static int m_D
}
// 6. 命名空间可以起别名
namespace veryLongName {
int m_A = 0;
}
void test02() {
namespace veryShortName = veryLongName;
cout << veryLongName::m_A << endl;
cout << veryShortName::m_A << endl;
}
int main() {
A::function();
function();
cout << "A::m_A: " << A::m_A << endl;
cout << "A::B::m_A " << A::B::m_A << endl;
cout << "A::m_B " << A::m_B << endl;
test02();
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;
}
#include <iostream>
using namespace std;
namespace KingGlory {
int sunwukongId = 100;
}
void test01() {
int sunwukongId = 200;
// 写了 using 声明后 下面这行代码说明以后看到的 sunwukongId 是用 KingGlory下的;
//using KingGlory::sunwukongId;
cout << "test01() 函数局部变量 sunwukongId: " << sunwukongId << endl;
cout << "KingGlory::sunwukongId: " << KingGlory::sunwukongId << endl;
}
namespace LOL {
int sunwukongId = 30;
}
void test02() {
//int sunwukongId = 200;
// using 编译指令;(就近原则)
//using namespace KingGlory;
using namespace LOL;
cout << "LOL::sunwukongId: " << sunwukongId << endl;
}
int main() {
test01();
test02();
return EXIT_SUCCESS;
}
// C++对C语言的增强;
#include <iostream>
using namespace std;
// 全局变量检测增强 (c++语言中可以检测出重复定义的全局变量)
int a;
//int a = 10;
// 函数检测增强
int getRectArea(int weight, int height) {
return weight * height;
}
// struct 增强 (可以在结构体中写函数)
struct Person
{
int m_Age;
void plusAge() {
m_Age++;
};
};
void personTest() {
Person person;
person.m_Age = 10;
cout << "before:" << person.m_Age << endl;
person.plusAge();
cout << "after:" << person.m_Age << endl;
}
// C++中可以直接用bool类型
bool flag = true;
void test02() {
int a = 10;
int& b = a;
int c = 20;
b = c;
cout << "a:" << a << endl; // 20
cout << "b:" << b << endl; // 20
cout << "c:" << c << endl; // 20
}
void test03() {
int arr[10];
for (int i = 0; i < 10; i++) {
arr[i] = i;
}
int(&pArray)[10] = arr;
for (int i = 0; i < 10; i++) {
cout << pArray[i] << " ";
}
cout << endl;
// 给数组起别名 (一个具有 10 个元素的 int类型数组)
typedef int(arrayRef)[10];
arrayRef& pArray2 = arr;
for (int i = 0; i < 10; i++) {
cout << pArray2[i] << " ";
}
cout << endl;
}
int main() {
int area = getRectArea(10, 10);
//personTest();
//test02();
test03();
return EXIT_SUCCESS;
}
// 参数的几种传递方式
#include <iostream>
using namespace std;
void swap_1(int a, int b) {
int t = a;
a = b;
b = t;
}
void test01() {
int a = 10;
int b = 20;
swap_1(a, b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
void swap_2(int *a, int *b) {
int p = *a;
*a = *b;
*b = p;
}
void test02() {
int a = 10;
int b = 20;
swap_2(&a, &b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
void swap_3(int& a, int& b) {
int p = a;
a = b;
b = p;
}
void test03() {
int a = 10;
int b = 20;
swap_3(a, b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
int main() {
test03();
return EXIT_SUCCESS;
}