赞
踩
预编译指令,引入头文件iostream
使用标准命名空间
namespace是命名空间关键字。
命名空间是C++的一种机制,用来把单个标识符下的大量有逻辑联系的程序实体组合到一起。此标识符作为组群的名字。
std打开一个房间,使得输入和输出流等关键字都是从这个房间出去的。
printf功能,输出字符串”hello wrold”。
cout是c++中的标准输出流,endl是输出换行并刷新缓冲区。
起到阻塞功能,这样程序运行完不会转跳,防止屏幕一闪而过
正常结束返回
#define _CRT_SECURE_NO_WARNINGS
#include <iostream> //标准输入输出流 in输入 out 输出
//using namespace std;//使用命名空间 std 打开一个叫std房间
//函数入口地址
int main()
{
// cout 标准的输出
// << 左移运算符
// endl 结束换行
std::cout << "hello world" << 123456 << std::endl;
system("pause"); //阻塞功能
return EXIT_SUCCESS; //返回正常退出
}
如果有两个同名变量,一个是全局变量,另一个是局部变量,那么局部变量在其作用域内具有较高的优先权,它将屏蔽全局变量。
//全局变量
int a = 10;
void test(){
//局部变量
int a = 20;
//全局a被隐藏
cout << "a:" << a << endl;
}
//输出输出结果是a:20,在test语句中输出内部定义的局部变量
作用域运算符可以用来解决局部变量与全局变量的重名问题,即在局部变量的作用域内,通过**::访问被屏蔽同名的全局变量**
//全局变量
int a = 10;
//1. 局部变量和全局变量同名
void test(){
int a = 20;
//打印局部变量a
cout << "局部变量a:" << a << endl;
//打印全局变量a
cout << "全局变量a:" << ::a << endl;
}
#define _CRT_SECURE_NO_WARNINGS #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; }
为避免在大规模程序的设计中,以及在程序员使用各种各样的C++库时,标识符的命名发生冲突,标准C++引入关键字namespace(命名空间/名字空间/名称空间),更好地控制标识符的作用域
主要功能:解决命名冲突的问题
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;
}
错误写法!!!
void test(){
namespace A{
int a = 10;
}
namespace B{
int a = 20;
}
cout << "A::a : " << A::a << endl;
cout << "B::a : " << B::a << endl;
}
namespace A
{
void func();
int m_A = 20;
struct Person
{
};
class Animal{};
}
namespace A
{
namespace B
{
int m_A = 10;
}
}
void test()
{
cout << "作用域B下的m_A为: " << A::B::m_A << endl;
}
namespace A { int a = 10; } namespace A { void func() { cout << "hello namespace!" << endl; } } void test() { cout << "A::a : " << A::a << endl; A::func(); }
namespace
{
int m_C = 0;
int m_D = 0;
}
//当写了无名命名空间,相当于写了 static int m_C ; static int m_D;
//只能在当前文件内使用
无名命名空间,意味着命名空间中的标识符只能在本文件内访问,相当于给这个标识符加上了static,使得其可以作为内部连接
namespace veryLongName
{
int m_A = 0;
}
void test()
{
//起别名
namespace veryShortName = veryLongName;
cout << veryLongName::m_A << endl;
cout << veryShortName::m_A << endl;
}
#define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; #include "game1.h" #include "game2.h" //namespace命名空间主要用途 用来解决命名冲突的问题 void test01() { LOL::goAtk(); KingGlory::goAtk(); } //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; } void test03() { cout << "A::下的m_A为" << A::m_A << " m_B为: " << A::m_B << endl; } //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; } int main(){ //test01(); //test02(); //test03(); test04(); system("pause"); return EXIT_SUCCESS; }
#define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; namespace KingGlory { int sunwukongId = 10; } void test01() { int sunwukongId = 20;//局部变量 //using 声明 注意避免二义性问题 //写了using声明后 下面这行代码说明以后看到的sunwukongId 是用KingGlory下的 //但是 编译器又有就近原则 //二义性,没法区分sunwukongID使用的是那个 //using KingGlory::sunwukongId; cout << sunwukongId << endl; } //using编译指令 namespace LOL { int sunwukongId = 30; } void test02() { //int sunwukongId = 20; //using编译指令 using namespace KingGlory; //打开王者荣耀房间 using namespace LOL;//打开LOL房间 //如果打开多个房间,也要避免二义性问题 cout << LOL::sunwukongId << endl; } int main(){ //test01(); test02(); system("pause"); return EXIT_SUCCESS; }
//C
int a;//没有赋值,当做声明
int a = 10;//赋值,当做定义
//C++
//int a;
int a = 10;
//C: 函数检测增强 int getRectS(w, h) { } void test02() { getRectS(10, 10, 10); } //C++: 函数检测增强 ,参数类型增强,返回值检测增强,函数调用参数检测增强 int getRectS(int w, int h) { return w*h; } void test02() { getRectS(10, 10); }
//C: 类型转换检测增强
void test()
{
char * p = malloc(sizeof(64)); //malloc返回值是void*
}
//C++: 类型转换检测增强
void test()
{
char * p = (char*)malloc(sizeof(64)); //malloc返回值是void*
}
//C: struct 增强 struct Person { int m_Age; //void plusAge(); //c语言中struct不可以加函数 }; void test() { struct Person p1; //使用时候必须加入struct关键字 } //C++: struct 增强 struct Person { int m_Age; void plusAge(){ m_Age++; }; //c++中struct可以加函数 }; void test() { Person p1; //使用时候可以不加struct关键字 p1.m_Age = 10; p1.plusAge(); cout << p1.m_Age << endl; }
//C: bool类型增强 C语言中没有bool类型
//bool flag;
//C++: bool类型增强 C语言中没有bool类型
bool flag = true; //只有真或假 true代表 真(非0) false 代表假(0)
void test()
{
cout << sizeof(bool) << endl;//值是1
flag = 100;
//bool类型 非0的值 转为 1 ,0就转为0
cout << flag << endl;//值是1
}
//C: 三目运算符增强 void test() { 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 ,b = %d \n", a, b); } //C++: 三目运算符增强 void test() { int a = 10; int b = 20; cout << "ret = " << (a > b ? a : b) << endl;//返回20 (a > b ? a : b) = 100; //b = 100 C++中返回的是变量,加小括号 cout << "a = " << a << endl;//10 cout << "b = " << b << endl;//100 }
6 引用基本语法
6.1.1 用途起别名
6.1.2 Type &别名 = 原名
6.1.3 引用必须初始化
6.1.4 一旦初始化后 不能修改
6.1.5 对数组建立引用
6.2 参数3种传递方式
6.2.1 值传递
6.2.2 地址传递
6.2.3 引用传递
6.3 注意事项,不要返回局部变量的引用
6.4 如果函数返回值是引用,那么函数的调用可以作为左值
6.5 引用的本质 就是一个指针常量
7 指针的引用
7.1 用一级指针引用 可以代替二级指针
8 常量引用
8.1 使用场景 修饰形参为只读
8.2 const int &a = 10;会分配内存
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。