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>
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"
void test01()
{
KingGlory::goAtk();
LOL::goAtk();
}
namespace A
{
int m_A;
void func();
struct Person
{
};
class Animal
{
};
}
void test02()
{
}
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;
}
namespace B
{
int m_B = 100;
}
void test04()
{
cout << "B空间下的m_A = " << B::m_A << endl;
cout << "B空间下的m_B = " << B::m_B << endl;
}
namespace
{
int m_C = 1000;
int m_D = 2000;
}
void test05()
{
cout << "m_C = " << m_C << endl;
cout << "m_D = " << ::m_D << endl;
}
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(){
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();
}
#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