Hello world
#include <iostream> //标准输入输出流 in:输入 out:输出
using namespace std; //使用命名看空间
//函数入口地址
int main()
{
//cout 标准的输出
//endl 输出换行并刷新缓冲区
cout<<"hello world!" << endl;
return 0; }
如果不想使用 using namespace std;
#include <iostream> //标准输入输出流 in:输入 out:输出
//函数入口地址
int main()
{
//cout 标准的输出
std::cout<<"hello world!" << std::endl;
return 0;
}
可以看出cout和endl的是在std的作用域里面的,这是因为在开发过程中,很多人一起开发,有的函数名和变量名可能会产生重叠,通过引入namespace,就可以区分开使用的是哪个模块的变量。
后面涉及到命名空间的时候会详细讲解这一块的知识
双冒号作用域运算符
#include <iostream>
using namespace std;
int life =200;
void test()
{
int life = 100;
cout << "life =" << life << endl;
//双冒号 作用域运算符 ::全局作用域
cout << "global life =" << ::life << endl;
}
int main()
{
test();
return 0;
}
运行结果:
我们可以看到相同名字的变量,一个是局部变量一个是全局变量,有时候我们需要用到全局变量,只需要加上双冒号改变作用域,就可以方便的使用了。
命名空间(namespace)的使用
namespace命名空间主要用途:用来解决命名冲突的问题
#include <iostream>
using namespace std;
//namespace命名空间主要用途:用来解决命名冲突问题
namespace GAME1
{
void Atk();
}
void GAME1::Atk()
{
cout<<"game1 attack" << endl;
}
namespace GAME2
{
void Atk();
}
void GAME2::Atk()
{
cout<<"game2 attack" << endl;
}
int main()
{
GAME1::Atk();
GAME2::Atk();
return 0;
}
1.命名空间下可以放 函数、变量、结构体、类
2.命名空间必须定义在全局作用下
3.命名空间可以嵌套
4.命名空间是开放的,可以随时往原先的命名空间添加内容
5.命名空间可以是匿名的相当于写了static m_A
6.命名空间可以起别名
#include <iostream>
using namespace std;
//1.命名空间下可以放 函数、变量、结构体、类
namespace A
{
void func();
int m_A = 10;
struct Persion
{
int age;
};
class Animal{};
namespace B
{
int m_B = 20;
}
}
//2.命名空间必须定义在全局作用下
//3.命名空间可以嵌套
void test01()
{
cout<<"namespace A m_A:"<<A::m_A << endl;
cout<<"namespace B m_B:"<<A::B::m_B << endl;
}
//4.命名空间是开放的,可以随时往原先的命名空间添加内容
namespace A //会和上面的命名空间A进行合并
{
int m_B = 30;
}
void test02()
{
cout<<"namespace A m_A:"<<A::m_A << endl;
cout<<"namespace A m_B:"<<A::m_B << endl;
}
//5.命名空间可以是匿名的相当于写了static int m_C 只可以在当前文件使用
namespace
{
int m_C = 0; //static int m_C;
int m_D = 0; //static int m_D;
}
//6.命名空间可以起别名
namespace LongLongName
{
int m_A = 60;
}
void test03()
{
//起别名
namespace Long = LongLongName;
cout<<"namespace long m_A:"<<Long::m_A << endl;
}
int main()
{
//test01();
//test02();
//test03();
return 0;
}
这些就是namespace相关的东西,我在看framework代码的时候,这个还是很常见的。
using的声明和编译的指令
#include <iostream>
using namespace std;
namespace GAME1
{
int id = 10;
}
void test01()
{
int id = 20;
//using 声明 注意避免二义性
//写了using声明之后,下面这行代码说明以后看到的id是GAME命名空间的 优先级跟定义变量相同
//但是编译器有就近原则
//出现了二义性
//using GAME1::id;
cout<<"id:"<<id<<endl;
}
//using 编译
namespace GAME2
{
int id = 60;
}
void test02()
{
//int id = 30;
//using 编译指令 同时打开同样得等级就j就会出现二义性
using namespace GAME1; //打开命名空间,优先级低于声明 避免了二义性
using namespace GAME2;
//cout<<"id:"<<id<<endl;
cout<<"id:"<<GAME1::id<<endl;
cout<<"id:"<<GAME2::id<<endl;
}
int main()
{
//test01();
test02();
return 0;
}
这里主要涉及的就是一个是声明命名空间的变量和局部变量需要注意二义性
另外一个就是打开多个命名空间的时候也需要注意两个命名空间中变量的二义性
C++对C语言的增强
1.全局变量检测增强
2.函数检测增强
3.类型转换增强
4.struct增强
5.bool类型增强
6.三目运算符增强
7.const增强
//C++语言
#include <iostream>
using namespace std;
//1.全局变量检测增强
//下面的代码在C++环境是不可以编译通过的
//int a;
//int a = 10;
//2.函数检测增强 参数类型增强 返回值检测增强 函数调用参数检测增强
//在C++环境参数必须有类型,需要返回值的必须要返回值
int add(int i, int j)
{
return i + j;
}
//在C++环境中函数调用的参数数量必须与定义相同
void test02()
{
//add(10,20,30);
add(10,20);
}
//3.类型转换增强
//在C++中必须要对类型进行强转
void test03()
{
char *p = (char*)malloc(sizeof(64)); //malloc返回值是void*
}
//4.struct增强
struct Person
{
int m_Age;
void addAge(){m_Age++;}; //C++中struct可以加函数
};
void test04()
{
Person p1; //在C++语言中可以不加加入struct关键字
p1.m_Age = 10;
p1.addAge();
cout<<p1.m_Age<<endl;
}
//5. bool类型增强
//C++语言中才有bool类型
bool flag = true; //只有真(true 非0)或假(false 0)
void test05()
{
cout<<sizeof(flag)<<endl;
flag = 100;
//bool类型 非0的值转为1,0就是0
cout<<flag<<endl;
}
//6.三目运算符增强
void test06()
{
int a = 10;
int b = 20;
cout<<"ret = " <<(a>b?a:b)<<endl;
(a>b?a:b) = 100; //b = 100 C++返回的是变量
cout<<"a = " <<a<<endl;
cout<<"b = " <<b<<endl;
}
//7.cons增强
const int m_A = 10; //受到保护不可以修改(全局只读区)
void test07()
{
const int m_B = 20; //真正的常量,C++不会分配内存,C++中有个符号表,
//以key-value的形式存放
int *p = (int *)&m_B; //编译器会临时开辟一块内存空间
//int tmp = m_B; tmp有内存
//int *p = (int *)&tmp; *p指向的临时的地址,不可见
*p = 200;
cout<<"*p =" <<*p<<endl;
cout<<"m_B =" <<m_B<<endl;
int a[m_B]; //可以初始化数组
}
int main()
{
//test04();
//test05();
//test06();
//test07();
return 0;
}
//C语言
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//1.全局变量检测增强
//在C语言环境中是可以编译通过的
int a;
int a = 10;
//2.函数检测增强 参数类型增强 返回值检测增强 函数调用参数检测增强
//在C语言环境中是可以编译通过的,会有警告但是没有error
int add(i,j)
{
}
//在C语言中调用函数的时候参数数量与定义不一致可以编过
void test02()
{
add(10,20,30);
}
//3.类型转换增强
void test03()
{
char *p = malloc(sizeof(64)); //malloc返回值是void*
}
//4.struct增强
struct Person
{
int m_Age;
//void addAge(); //C语言中struct不可以加函数
};
void test04()
{
struct Person p1; //在C语言中必须加入struct关键字
}
//5. bool类型增强
//C语言中没有bool类型
//bool flag;
//6.三目运算符增强
void test06()
{
int a = 10;
int b = 20;
printf("ret = %d \n",a>b?a:b);
//(a>b?a:b) = 100; //20 = 100 C语言返回的是值
//C语言中模仿C++写
*(a>b?&a:&b) = 100;
printf("a = %d \n",a);
printf("b = %d \n",b);
}
//7.cons增强
const int m_A = 10; //受到保护不可以修改(全局只读区)
void test07()
{
const int m_B = 20; //伪常量,编译器是会分配内存
int *p = (int *)&m_B;
*p = 200;
printf("*p = %d \n",*p);
printf("m_B = %d \n",m_B);
//int a[m_B]; 不可以初始化数组
}
int main()
{
//test06();
test07();
return 0;
}