目录
6.2.1、这里using编译指令和就近原则同时出现的时候,编译器会选择就近原则
一、c++概述
1、c++是c语言的超集,也就是说任何有效的c程序都是有效的c++程序,c++程序可以使用已有的c程序库。
2、c++在c的基础上添加的面向对象编程和泛型编程的支持,c++继承了c的高效、简洁、快速和可移植的传统。
二、初识c++
写一个helloworld。
#include <iostream> /* 标准输入输出流 i-input 输入 o-output 输出 stream 流 相当于stdio.h */
using namespace std; /* 使用 标准 命名空间 */
/*
#include <interrupt.h>
#include <cinterrupt>
#include <start.h>
#include <cstart>
*/
/* 程序入口函数 */
int main()
{
/* cout 标准输出流对象
* << 在c语言中是左移,在c++中用于在cout后拼接输出的内容,好处:不用再去使用类似%d、%s,可以直接输出想要的内容
* endl -end line 刷新缓冲区,并且换行,好处:不用考虑用\r\n还是\n,c++中都是endl
*
*/
cout << "hello world" << endl;
system("pause"); /* 阻塞 */
return EXIT_SUCCESS; /* 返回正常退出 */
}
三、面向过程和面向对象
3.1、面向过程
面向过程是一种以过程为中心的编程思想。
面向过程编程思想的核心:功能分解,自顶向下,逐层细化(程序=数据结构+算法)。
面向过程编程语言存在的主要缺点是不符合人的思维习惯,而是要用计算机的思维方式去处理问题,而且面向过程编程语言重用性低,维护困难。
3.2、面向对象
面向对象编程(Object-Oriented Programming)简称 OOP 技术
使用 OOP 技术,常常要使用许多代码模块,每个模块都只提供特定的功能,它们是彼此独立的,这样就增大了代码重用的几率,更加有利于软件的开发、维护和升级。
在面向对象中,算法与数据结构被看做是一个整体,称作对象
可以用下面的等式来定义对象和程序:
对象 = 算法 + 数据结构
程序 = 对象 + 对象 + ……
面向对象编程思想的核心:应对变化,提高复用。
3.3、面向对象三大特性
封装
继承
多态
四、双冒号(::)作用域
:: 代表作用域,前面什么都不添加,表示全局作用域
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
int atk = 1000;
void test01()
{
int atk = 2000;
cout << "atk = " << atk << endl;
/* :: 代表作用域,前面什么都不添加,表示全局作用域 */
cout << "::atk = " << ::atk << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
输出:
将上面代码中的“using namespace std;”注释掉,在cout和endl前添加std::,作用一样
修改如下:
//using namespace std;
int atk = 1000;
void test01()
{
int atk = 2000;
std::cout << "atk = " << atk << std::endl;
/* :: 代表作用域,前面什么都不添加,表示全局作用域 */
std::cout << "::atk = " << ::atk << std::endl;
}
五、namespace命名空间
命名空间的用途:
1、解决命名冲突
2、命名空间下可以放变量、函数、结构体、类......
3、命名空间必须写在全局作用域下
4、命名空间可以嵌套命名空间
5、命名空间是开放的,可以随时给命名空间添加新成员
6、命名空间可以是匿名的
7、命名空间可以起别名
用法总结如下代码:
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;
}
namespace.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "game1.h"
#include "game2.h"
using namespace std;
/* 1、命名空间用途:解决命名冲突 */
void test01()
{
LOL::goAtk();
}
/* 2、命名空间下可以放变量、函数、结构体、类....... */
namespace A
{
int m_A;
void func();
struct Person
{};
class Animal // class--类的关键字
{};
}
/* 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、命名空间可以是匿名的
* 相当于写了 static int m_C = 20 static int m_D = 30
*/
namespace
{
int m_C = 20;
int m_D = 30;
}
/* 调用匿名的命名空间中变量的两种方式 */
void test05()
{
cout << "m_C = " << m_C << endl;
cout << "m_D = " << ::m_D << endl;
}
/* 7、命名空间可以起别名 */
namespace verylongName
{
int m_E = 1000;
}
void test06()
{
namespace shortName = verylongName;
cout << "m_E = " << shortName::m_E << endl;
cout << "m_E = " << verylongName::m_E << endl;
}
int main()
{
//test01();
//test03();
//test04();
//test05();
test06();
system("pause");
return EXIT_SUCCESS;
}
六、using声明以及using编译指令
6.1、using声明
using声明是直接告诉编译器我要用哪个作用域的变量
当using声明的时候,应该尽量避免和就近原则同时出现,否则会出错
void test01()
{
//int sunwukong_id = 2; //eer:多次声明,当using声明与就近原则同时出现,出错,尽量避免
//using声明
using KingGlory::sunwukong_id;
cout << KingGlory::sunwukong_id << endl;
cout << sunwukong_id << endl;
}
6.2、using编译指令
打开一个namespace,先看有没有就近原则出现,如果没有再从打开的namespace中找,如果打开多个namespace的话,在使用的时候最好做一个区分。
void test02()
{
int sunwukong_id = 2;
//using 编译指令
using namespace KingGlory;
cout << "sunwukong_id = " << sunwukong_id << endl;
}
6.2.1、这里using编译指令和就近原则同时出现的时候,编译器会选择就近原则
输出如下
6.2.2当using编译指令有多个,需做区分
void test02()
{
//int sunwukong_id = 2;
//using 编译指令
using namespace KingGlory;
//当using编译指令有多个,需加作用域区分
using namespace LOL;
//当using编译指令和就近原则同时出现。优先选择就近原则
cout << "KingGlory::sunwukong_id = " << KingGlory::sunwukong_id << endl;
cout << "LOL::sunwukong_id = " << LOL::sunwukong_id << endl;
}
完整代码
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
namespace KingGlory
{
int sunwukong_id = 1;
}
namespace LOL
{
int sunwukong_id = 3;
}
void test01()
{
//int sunwukong_id = 2; //eer:多次声明,当using声明与就近原则同时出现,出错,尽量避免
//using声明
using KingGlory::sunwukong_id;
cout << KingGlory::sunwukong_id << endl;
cout << sunwukong_id << endl;
}
void test02()
{
//int sunwukong_id = 2;
//using 编译指令
using namespace KingGlory;
//当using编译指令有多个,需加作用域区分
using namespace LOL;
//当using编译指令和就近原则同时出现。优先选择就近原则
cout << "KingGlory::sunwukong_id = " << KingGlory::sunwukong_id << endl;
cout << "LOL::sunwukong_id = " << LOL::sunwukong_id << endl;
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}