赞
踩
3.1.有关于命名空间 namespace
:1.
在C++语言编写的程序中,变量和函数等的作用范围是有一定限制的。比如,在函数体中定义的一个临时变量就不可以在函数体外使用。为了解决变量和函数等的作用范围,在C++语言中引入了名空间的概念,并增加了关键字namespace和using.2.
在一个名空间中可以定义一组变量和函数,这些变量和函数的作用范围一致,可以将这些变量和函数称为这个名空间的成员。3.
通过名空间,可以在同一个文件中使用相同的变量名或函数名,只要它们属于不同的名空间。另外,名空间可以使得代码操作具有相同名字但属于不同库的变量。而且,名空间也可以提高C语言与C++语言的兼容性。using namespace std;
//C++标准名称空间std::
// using namespace std;//C++标准名称空间
std::cout <<" a = "<< a << std::endl;
注意:这是一个C++标准名称空间,如果将之注释掉,很多C++关键字就会出错!
using namespace std;//C++标准名称空间
cout <<" a = "<< a << endl;
#include<iostream> using namespace std;//C++标准名称空间 int a = 10; namespace ns//类似于C语言结构体 { int a = 2; char s = 'f'; int b = 9; } int main() { cout <<" a = "<< a << endl; cout << ns::a << endl; cout << ns::s << endl; system("pause"); return 0; }
3.2.system("pause");
:3.3.常量
C++定义常量两种方式:
#define 常量名 常量值
#define day 7//宏定义
const int month = 12;
3.4. 字符串定义
char site1[7] = {'R', 'U', 'N', 'O', 'O', 'B'};
char site2[] = "RUNOOB";
string site3 = "runoob";
3.5. 总结
#include<iostream> using namespace std;//C++标准名称空间 void name(int nt);//声明函数 void swap1(int* p1, int* p2);//输入两数指针,并使用解指针对两者数值进行交换 #define stc 10 int a = 10,day = 365; int b = 0,c = 0; namespace ns//类似于结构体 { int a = 11; char s = 'f'; int b = 9; namespace tr//命名空间嵌套 { int a = 12; } } struct score//成绩结构体 嵌套 { int SA; int SB; }; struct staff//员工结构体 { string name;//姓名 int age = 1;//年龄 //若不初始化将会有警告 struct score sc = {1,1};//若不初始化将会有警告 }; char s[10] = { 'q','w','e','t' };//C风格字符串 char str1[10] = "RUNOOB";//C风格字符串 string str2[] = { "good" };//输出信息不对❌ string str3= "hello world";//C++风格字符串,需要加入头文件 string bool asa = true;//bool 可直接赋值:true/false 1/0 当赋值不等于0或1时默认为1 int main() { const int dc = 13;//const 修饰常量 // double dou1; int arr[10] = {1,2,3,4,5,6,7,8,9,10}; int* arr_zz = &arr[4]; int* arr_zz3 = &arr[3]; int* p = arr; staff S1;//定义结构体变量 staff S2 = { "MO",18,{1,2} };//定义结构体变量 staff* S2_zz = &S2; cout << "a = " << a << endl; cout << day << " 天" << endl; cout << s << endl; cout << "名称空间" << endl; cout << ns:: a << endl; cout << ns::s << endl; cout << ns::tr::a << endl; cout << "字符串" << endl; cout << "s = " << s << endl; cout << "\r\nstr1 = " << str1 << endl; cout << "\r\nstr2 = " << str2 << endl; cout << "\r\nstr3 = " << str3 << endl; cout << "\r\nsizeof(s) = " << sizeof(s) << "\r\nsizeof(str1) = " << sizeof(str1) << endl; cout << "\r\nsizeof(str2) =" << sizeof(str2) << endl; asa = false; cout << "bool型变量" << endl; cout << "asa = " << asa << endl; cout << "sizeof(asa) = " << sizeof(asa) << "\r\n" << endl; // cout << "输入浮点型变量:" << endl; // cin >> dou1; // cout << "dou1 = " << dou1 << endl; // cout << "sizeof(dou1) = " << sizeof(dou1) << "\r\n" << endl; a = 0; a += 2;//a = a + 2; cout << "加等 \r\na = 0; a += 2;a = " << a << "\r\n" << endl; // +=,*=,-=,%=,/=; 变量 += 常量; a = 10; b = 14; cout << "比较符" << endl; cout << (a > b) << "\r\n" << endl;//真假 //比较符 == , != , < , > , <= , >= . b = 12; c = (a < b) ? 1 : 0; cout << "三目运算符(a=10; b=12)\r\n" << "a < b ? c = " << c << "\r\n" << endl; c = (a > b) ? 1 : 0; cout << "a > b ? c = " << c << "\r\n" << endl; cout << "a > b ? " << endl; a > b ? cout << "true" << endl : cout << "false" << endl;//这里有一个需要注意的地方,注意语句结束的地点. cout << "\r\n" << endl; cout << "输出实验\r\n\r\n"; for (a = 0; a < 10; a++) { for (b = 0; b < 10; b++) { cout << "*" << " ";//如果添加 <<endl 的话会执行换行 } cout << endl;//换行,移位 } cout << endl;//换行,移位 //continue与break的区别: //continue: 结束本次循环,不执行本次循环余下程序. //break : 结束整个循环. for (int a = 0; a < 100; a++) { if (a % 2 == 0) { continue; } cout << a << endl; } cout << endl; c = (sizeof(arr) / sizeof(int))-1;// cout << "arr[10] = {"; for (; c > -1; c--) { cout << arr[9-c] << ","; } cout << "};\r\n"; cout << "数组实验: \r\n" << "\r\n1.获取数组所占内存空间\r\n"; cout << "sizeof(arr) = " << sizeof(arr) << endl; cout << "2.获取数组首地址\r\n" << "&arr = " << &arr << endl;//错误方法 cout << "(int)&arr[0] = " << (int)&arr[0] << endl; cout << "(int)&arr[1] = " << (int)&arr[1] << endl; name(100); cout << endl << "指针实验" << endl; // int* arr_zz = &arr[0]; // int* a_zz = &a; cout << "打印地址(arr[4]):" << "&arr[4] = " << &arr[4] << " arr_zz = " << arr_zz << endl; cout << "打印地址(arr[3]:" << "&arr[3] = " << arr_zz3 << endl; cout << "*arr_zz= " << *arr_zz << "//`*`又称为解引用" << endl;// 通过* 操作指针变量指向的内存 cout << "打印数组arr的首地址:" << "arr = " << arr << " p = arr = " << p << " &arr[0] = " << &arr[0] << endl; cout <<"指针各个类型所占内存大小:" << endl << "int*" <<sizeof(arr_zz) << endl; cout << "sizeof(char*) = " << sizeof(char*) << endl; cout << "sizeof(float*) = " << sizeof(float*) << endl; cout << "sizeof(double*) = " << sizeof(double*) << endl << endl; cout << "数组p = arr指针自加实验" << endl; for (a = 0; a < 10; a++) { cout << "a = " << a << endl << "地址: " << p << endl << "解地址:" << *p << endl; p++; } cout << "输入两数指针, 并使用解指针对两者数值进行交换:" << endl; a = 178; b = 14; cout << "a = " << a << " b = " << b << endl; swap1(&a,&b); cout << "a = " << a << " b = " << b << endl; cout << endl << endl; cout << "Struct Experiment" << endl; cout << "name S1 :" << S1.name << endl << " " << S1.age << endl << " " << S1.sc.SA << endl << " " << S1.sc.SB << endl; cout << "name S2 :" << S2.name << endl << " " << S2.age << endl << " " << S2.sc.SA << endl << " " << S2.sc.SB << endl; S1.name = "N1"; S1.age = 23; S1.sc.SA = 250; S1.sc.SB = 50; S2_zz->name = "N2"; S2_zz->age = 22; S2_zz->sc.SA = 22; S2_zz->sc.SB = 22; cout << "`.`赋值 name S1 : " << endl << S1.name << endl << " " <<S1.age << endl << " " << S1.sc.SA << endl << " " << S1.sc.SB << endl; cout << "`->`赋值 name S2 : " << endl << S2.name << endl << " " << S2.age << endl << " " << S2.sc.SA << endl << " " << S2.sc.SB << endl; cout << "staff* S2_zz = &S2 = " << S2_zz << endl; system("pause"); return 0; } void name(int nt) { int we = 0; we = ns::a + nt; cout << "\r\n调用函数 name :" << endl; cout << "we = ns::a + nt = " << we << "\r\nns::a = " << ns::a << "\r\nnt = " << nt << endl; } void swap1(int* p1, int* p2) { int temp = *p1; *p1 = *p2; *p2 = temp; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。