标签:形参 一个 数值 *** swa ring 没有 3.1 空间
1,c++之引用
1 #include <iostream> 2 3 using namespace std; 4 5 /* 引用是变量的别名,对引用的操作即是对变量的操作,引用不能重新作为其他变量的别名,引用必须初始化 */ 6 int main() 7 { 8 int a = 90; 9 int *p = &a; //c语言用法 10 cout << "a = " << a << " *p = " << *p << endl; 11 12 int b = 198; 13 p = &b; //c语言指针可重新指向另一个变量的地址 14 cout << "*p = " << *p << endl; 15 16 cout << endl; 17 18 //强制性初始化,不初始化就报错 19 int &ref = a; //c++引用 20 /* 错误示例 int &ref; ref = a; */ 21 cout << "a = " << a << " ref = " << ref << endl; 22 23 //引用不能重新指向其他变量, 24 //并没有重新作为b的引用,而是将b的值附给了ref指向的变量a 25 ref = b; 26 cout << "b = " << b << " ref = " << ref << endl; 27 cout << "&a = " << (void *)a << " &ref = " << (void *)ref << " &b= " << (void *)b << endl; 28 29 return 0; 30 }
2,c++之引用与c语言之取地址
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 8 int a = 90; 9 /* 10 int &ref = a; //& --> 引用符 11 &ref; // & --> 取地址符 12 */ 13 int buf[3][4]; 14 int (*p)[4] = buf; 15 16 int (&ref)[3][4] = buf; 17 for(int i=0; i<3; i++) 18 { 19 for(int j=0; j<4; j++) 20 { 21 ref[i][j] = a++; 22 } 23 } 24 25 for(int i=0; i<3; i++) 26 { 27 for(int j=0; j<4; j++) 28 { 29 cout << ref[i][j] << ‘ ‘; 30 } 31 cout << endl; 32 } 33 34 int ff[2][3][4][5]; 35 int (&reff)[2][3][4][5] = ff; //数组类型的引用 36 37 return 0; 38 }
3,c++之不存在引用的数组
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 int *p[32] = {NULL}; 8 9 //不存在引用的数组 10 //int &ref[32]; //错误示例 11 12 return 0; 13 }
4,c++之引用
1 #include <iostream> 2 3 using namespace std; 4 5 //c语言 6 //void fun(int arr[10]) --> sizeof(arr)=4 , int arr[10]-->int *arr 7 //void fun(int *arr) --> sizeof(arr)=4 , 8 void fun(int arr[]) //--> sizeof(arr)=4 , int arr[]-->int *arr 9 {} 10 11 //void bun(int bbb[4][5]) -->4 12 //void bun(int (*bbb)[5]) -->4 13 void bun(int bbb[][5]) //-->4 14 {} 15 16 //c++ 17 void bunnn(int (&ref)[4][5]) 18 {} 19 void funnn(int (&ref)[10]) 20 {} 21 22 int main() 23 { 24 int a[10]={0}; 25 fun(a); 26 funnn(a); 27 28 int b[4][5]; 29 bun(b); 30 bunnn(b); 31 32 return 0; 33 }
5,c++之计算奇偶数
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 8 int a = 90; 9 int buf[3][4]; 10 int (*p)[4] = buf; 11 12 int ji =0,ou=0; 13 int (&ref)[3][4] = buf; 14 15 cout << "请输入数值(12)个:" << endl; 16 for(int i=0; i<3; i++) 17 { 18 for(int j=0; j<4; j++) 19 { 20 cin >> ref[i][j] ; 21 } 22 } 23 cout << endl; 24 cout << "数组:" << endl; 25 for(int i=0; i<3; i++) 26 { 27 for(int j=0; j<4; j++) 28 { 29 cout << ref[i][j] << ‘ ‘; 30 } 31 cout << endl; 32 } 33 cout << endl; 34 cout << "奇数有:" << endl; 35 for(int i=0; i<3; i++) 36 { 37 for(int j=0; j<4; j++) 38 { 39 if(1 == ref[i][j]%2) 40 { 41 ++ji; 42 cout << ref[i][j] << ‘ ‘ ; 43 } 44 } 45 } 46 cout << endl; 47 cout << "偶数有:" << endl; 48 for(int i=0; i<3; i++) 49 { 50 for(int j=0; j<4; j++) 51 { 52 if(0 == ref[i][j]%2) 53 { 54 ++ou; 55 cout << ref[i][j] << ‘ ‘ ; 56 } 57 } 58 } 59 cout << endl; 60 cout << "奇数个数:" << ji << ‘ ‘ << "偶数个数:" << ou << endl; 61 62 return 0; 63 }
6,c++之引用
#include <iostream> using namespace std; int main() { int a = 90; int *p = &a; int **p2 = &p; /* int **************p3 = &a; *p3 --> 90 90的类型为int *************型 */ int &ref = a; //错误示例 int &&reff = ref; //不能存在引用的引用 int &reff = ref; //给a又起了一个别名 return 0; }
7,c++之引用是作为变量的别名
#include <iostream> using namespace std; int main() { //引用作为变量的别名 int a = 90; int &ref = a; //错误示例 int &p = 1998; const int &p = 1998; //1,int tmp=1998 //2,const int &p = tmp cout << "p= " << p << endl << "&p= " << (void *)&p << endl; int *pp = (int *)&p; *pp =8888; cout << "p= " << p << endl; return 0; }
8,c++之不要返回局部变量的引用(考虑释放)
1 #include <iostream> 2 3 using namespace std; 4 5 int& fun() 6 { 7 int a = 90; 8 int &ref = a; 9 return ref; 10 } 11 12 int main() 13 { 14 //不要返回局部变量的引用(释放) 15 int &t = fun(); 16 17 return 0; 18 }
9,c++之函数引用
1 #include <iostream> 2 3 using namespace std; 4 5 void swap(int a,int b) //错误示例 6 { 7 int tmp = a; 8 a = b; 9 b = tmp; 10 } 11 12 void swap2(int *a,int *b) 13 { 14 int tmp = *a; 15 *a = *b; 16 *b = tmp; 17 } 18 19 void swap3(int &a,int &b) 20 { 21 int tmp = a; 22 a = b; 23 b = tmp; 24 } 25 26 int main() 27 { 28 int a = 90; 29 int b = 180; 30 /* c用法 31 swap(a,b); //错误示例 32 cout << "a= " << a << " b= " << b << endl; 33 swap2(*a,*b); 34 cout << "a= " << a << " b= " << b << endl; 35 */ 36 //c++用法 37 swap3(a,b); 38 cout << "a= " << a << " b= " << b << endl; 39 40 return 0; 41 }
10,c++之引用没有自己的独立存储空间
1 #include <iostream> 2 3 using namespace std; 4 5 struct Data 6 { 7 int &a; 8 int &b; 9 int &c; 10 }; 11 12 int main() 13 { 14 int a = 90; 15 int *p = &a; 16 17 //引用没有自己的独立空间,但引用的内部实现是通过指针实现的 18 int &ref = a; 19 20 cout << sizeof(Data) << endl; 21 22 int b = 190; 23 int c = 189; 24 cout << "&a= " << (void *)a 25 << "&b= " << (void *)b 26 << "&c= " << (void *)c << endl; 27 28 Data data = {a,b,c}; 29 cout << (void *)(*((int *)&data)) << endl; 30 cout << (void *)(*((int *)&data+1)) << endl; //32位机 31 cout << (void *)(*((int *)&data+2)) << endl; 32 33 return 0; 34 }
11,c++之函数重载
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 //函数重载取决于形参列表的不同 int &a -> float &a 7 //形参列表不同包括类型和参数的个数 8 int myAdd(const int &a, const int &b) 9 { 10 return a+b; 11 } 12 //错误示例 void myAdd(const int &a,const int &b) 13 float myAdd(const float &a, const float &b) 14 { 15 return a+b; 16 } 17 string myAdd(const string &a, const string &b) 18 { 19 return a+b; 20 } 21 22 int main() 23 { 24 cout << myAdd(23,89) << endl; 25 cout << myAdd(23.1f,89.0f) << endl; //不加f,默认为double型 26 cout << myAdd("hello","world") << endl; 27 28 return 0; 29 }
12,c++之函数重载
1 #include <iostream> 2 #include <string.h> 3 4 using namespace std; 5 6 //函数重载取决于形参列表的不同 int &a -> float &a 7 //形参列表不同包括类型和参数的个数 8 int myAdd(int a, int b) //myAddii(int a, int b) 9 { 10 return a+b; 11 } 12 float myAdd(float a, float b) //myAddff(float a,float b) 13 { 14 return a+b; 15 } 16 string myAdd(string a, string b) //myAddSsSs(string a,string b) 17 { 18 return a+b; 19 } 20 21 int main() 22 { 23 int a = 90; 24 int b = 89; 25 int c = 0; 26 c = myAdd(a,b); //c = myAddii(a,b); 27 28 float a1 = 9.0; 29 float b1 = 8.9; 30 float c1 = 0; 31 c1 = myAdd(a1,b1); //c1 = myAddff(a1,b1); 32 33 return 0; 34 }
13,c++之函数重载
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 struct STU 7 { 8 int iId; 9 string strName; 10 float fScore; 11 }; 12 13 void print(const STU &stu) 14 { 15 cout << stu.iId << ‘ ‘ << stu.strName << ‘ ‘ << stu.fScore <<endl; 16 } 17 18 void print(const int (&arr)[5]) 19 { 20 for(int i=0; i<5; i++) 21 { 22 cout << arr[i] << ‘ ‘; 23 } 24 cout << endl; 25 } 26 27 int main() 28 { 29 STU s = {1001,"jack",99}; 30 print(s); 31 32 int buf[5] = {11,22,33,44,55}; 33 print(buf); 34 35 return 0; 36 }
14,c++之函数模板
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 //函数模板 7 template <typename U> 8 U myAdd(U a, U b) 9 { 10 return a+b; 11 } 12 /* 13 template <typename U, typename M> //两个参数类型不同 14 U myAdd(U a, M b) 15 { 16 return a+b; 17 } 18 */ 19 int main() 20 { 21 int a = 90; 22 int b = 89; 23 int c = 0; 24 c = myAdd(a,b); 25 //不会调用U myAdd(U a,U b),会自动根据形参类型实例化U, 26 //根据模板产生具体类型的函数,int myAdd(int a,int b) 27 cout << c << endl; 28 29 float aa = 9.9; 30 float bb = 9.0; 31 float cc = 0; 32 cc = myAdd(aa,bb); 33 cout << cc << endl; 34 35 return 0; 36 }
15,c++之函数模板的特例化
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 struct STU 7 { 8 int iId; 9 string strName; 10 float fScore; 11 12 void info() 13 { 14 cout << iId << ‘ ‘ << strName << ‘ ‘ << fScore << endl; 15 } 16 }; 17 18 //函数模板 19 template <typename U, typename M> 20 U myAdd(U a, M b) 21 { 22 return a+b; 23 } 24 //函数模板的特例化 25 STU myAdd(const STU &s1, const STU &s2) 26 { 27 STU stu; 28 stu.iId = s1.iId + s2.iId; 29 stu.strName = s1.strName + ‘ ‘ + s2.strName; 30 stu.fScore = s1.fScore + s2.fScore; 31 return stu; 32 } 33 34 int main() 35 { 36 STU s = {1001,"jack",89}; 37 STU s2 = {1002,"rose",70}; 38 STU s3 = myAdd(s,s2); 39 s3.info(); 40 41 int a = 90; 42 int b = 89; 43 int c = 0; 44 c = myAdd(a,b); 45 //不会调用U myAdd(U a,U b),会自动根据形参类型实例化U, 46 //根据模板产生具体类型的函数,int myAdd(int a,int b) 47 cout << c << endl; 48 49 float aa = 9.9; 50 float bb = 9.0; 51 float cc = 0; 52 cc = myAdd(aa,bb); 53 cout << cc << endl; 54 55 return 0; 56 }
16,c++之默认函数(无参构造函数)
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 struct STU 7 { 8 int iId; 9 string strName; 10 float fScore; 11 //构造函数 12 STU(int id,string name,float score) 13 { 14 iId = id; 15 strName = name; 16 fScore = score; 17 cout << "$$$$$$$$$" << endl; 18 } 19 //无参构造函数 20 STU() 21 { 22 iId = 0; 23 strName = ""; 24 fScore = 0.0f; 25 cout << "**********" << endl; 26 } 27 28 void info() 29 { 30 cout << iId << ‘ ‘ << strName << ‘ ‘ << fScore << endl; 31 } 32 }; 33 34 int main() 35 { 36 STU s(1001,"jack",89); 37 s.info(); 38 39 STU *p = new STU[10]; //调用的是无参构造函数 40 STU s2; //调用的是无参构造函数 41 42 return 0; 43 }
17,c++之默认函数
#include <iostream> #include <string.h> using namespace std; struct STU { int iId; string strName; float fScore; string strPwd; //构造函数,一般将含有默认值的形参放在形参列表最后 STU(int id, string name, float score=60, string pwd="8888") { iId = id; strName = name; fScore = score; strPwd = pwd; cout << endl; } //无参构造函数 STU() { iId = 0; strName = ""; fScore = 0.0f; cout << "**********" << endl; } void info() { cout << iId << ‘ ‘ << strName << ‘ ‘ << fScore << ‘ ‘ << strPwd << endl; } }; int main() { STU s(1001,"jack",89); s.info(); STU s2(1002,"rose",78,"4566"); s2.info(); /* //错误示例 STU s3(1003, ,89,"12345"); s3.info(); */ return 0; }
18,c++
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 void fun(int a, int b = 90) 7 {} 8 9 void fun(int a) 10 {} 11 12 int main() 13 { 14 //函数调用具有二义性 15 fun(998); //错误示例 16 17 return 0; 18 }
19,c++之斐波那契数列
1 #include <iostream> 2 3 using namespace std; 4 5 int fun(int n = 25) 6 { 7 if(1 == n || 2 == n) 8 { 9 return 1; 10 } 11 else 12 { 13 return fun(n-1)+fun(n-2); 14 } 15 } 16 17 int main() 18 { 19 int n = 0; 20 int c = 0; 21 cout << "请输入要计算斐波那契数列的第几个数:" << endl; 22 cin >> n ; 23 c = fun(n); 24 cout << "斐波那契数列的第" << n << "个数为:" << c << endl; 25 26 return 0; 27 }
标签:形参 一个 数值 *** swa ring 没有 3.1 空间
原文地址:https://www.cnblogs.com/foodie-yuan/p/9409753.html