ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++学习记录(一)结构体,字符串,内联函数,const,函数参数,函数重载

2022-05-04 22:04:21  阅读:120  来源: 互联网

标签:std const 函数 int C++ 函数参数 using cout


终于又更新博客了,这次更新的内容是关于C++学习记录的。
  1 #include <iostream>
  2 #include <cstring>
  3 
  4 //using namespace std;  // 这种方式会包含许多不需要的内容
  5 using std::cout;        // 这种方式就很轻便
  6 using std::endl;
  7 using std::string;
  8 
  9 // C++手册,https://www.apiref.com/cpp-zh/index.html
 10 
 11 // 结构体的用法,C++中默认是public; 而struct换成class后变为private
 12 struct Stu
 13 {
 14     int age = 27;
 15     string name = "Heze";
 16     int height = 171;
 17     void action() { cout << " he is writing the code !" << endl; }
 18 };
 19 
 20 bool compair(string str1, string str2)
 21 {
 22     if(str1 < str2) { cout << str1 << endl; }
 23     else{ cout << str2 << endl;}
 24 }
 25 
 26 // 内联函数
 27 inline void show()
 28 {
 29     cout << " hello C++ !" << endl;
 30 }
 31 
 32 inline int Max(int a, int b)
 33 {
 34     return a > b ? a : b;
 35 }
 36 
 37 // const修饰变量
 38 const int c = 30;
 39 
 40 void test()
 41 {
 42     const int a = 100;
 43     int* p = (int*)&a;
 44     *p = 200;
 45     cout << "a = " << a << endl;                // 100
 46     cout << "*p = " << *p << endl;              // 200
 47 
 48     // 当一个基本变量被const修饰时,C++会在内存中建立一个常量表(只有直接赋值情况),
 49     // a中的值被保存到这个常量表中,再次访问a时,就会从常量表中读取这个值。
 50     // volatile const int a = 100; 就不会放到常量表中
 51     cout << "&(a) = " << &a << endl;
 52     cout << "p = " << &p << endl;
 53 }
 54 
 55 // 从右往左赋值(因为函数调用时是从左往右赋值)
 56 int showdata(int a = 1, int b = 2, int c = 3)
 57 {
 58     return a+b+c;
 59 }
 60 
 61 // 函数重载
 62 void func()
 63 {
 64     cout << "This is the function one" << endl;
 65 }
 66 
 67 void func(int a)
 68 {
 69     cout << "This is the function two" << endl;
 70 }
 71 
 72 void func(float a, int b)
 73 {
 74     cout << "This is the function three" << endl;
 75 }
 76 
 77 int main()
 78 {
 79     // -1- 结构体的用法
 80     /*
 81     Stu stu;
 82     cout << stu.name << endl;
 83     cout << stu.age << endl;
 84     cout << stu.height <<endl;
 85     stu.action();
 86     */
 87 
 88     // -2- 字符串的用法
 89     /*
 90     string str1 = "hello";
 91     cout << str1.length() << endl;              // 获取字符串长度,为5
 92     cout << str1.size() << endl;
 93 
 94     const char* p = str1.c_str();               // 转换成c类型的字符,长度变为6
 95     //while(*p != '\0'){ cout << *(p++) << endl; }
 96     cout << strlen(str1.c_str()) << endl;       // strlen计算字符串长度时,为'\0'时返回
 97 
 98     string str2 = "C++";
 99     if (str1 < str2) { cout << str1 << endl; }  // 比较哪一个字符串小
100     else { cout << str2 <<endl;}
101 
102     string str3 = "C++";                        // 比较字符串是否相等
103     cout << (str3 == str2) << endl;
104 
105     if(compair(str1, str3)) { cout << "str1 small" << endl; }
106     else { cout << "str3 small" << endl; }
107 
108     string str4 = str1 + str2;                  // 字符串相加
109     cout << str4 << endl;
110 
111     string str5 = str2.append(str3);            // 字符串追加
112     cout << str5 << endl;
113     */
114 
115     // -3- 内联函数(尽量简短,调运频次高时使用)
116     /*
117     show();
118     int a = 10;
119     int b = 11;
120     cout << Max(a,b) << endl;
121     cout << Max(a,++b) << endl;
122     */
123 
124     // -4- const修饰符
125     /* 进程加载之后:4G空间
126        静态区:代买段,只读数据段(被const修饰的全局变量,字符串,.data,.bss段);
127               程序编译链接时已经被加载了;如有const修饰的变量,这时已经分配内存了。
128        动态区:堆,栈,内核(kernel)
129              堆:手动申请,手动释放,malloc,calloc,relloc开辟空间,free(pointer)
130              栈:随用随释放,指出这个代码块,变量就自动销毁了。
131     */
132     /*
133     int a = 10;
134     const int b = 20;               // 只读变量,不能发生改变
135     static const int d = 40;
136     cout << &a << endl;             // 0x62fe1c (栈)
137     cout << &b << endl;             // 0x62fe18 (栈)
138     cout << &c << endl;             // 0x404004 (静态区)
139     cout << &d << endl;             // 0x404008 (静态区)
140 
141     //const int* p = &a;              // 只读整型的指针,无法通过指针修改变量的值
142     int const* p = &a;
143     cout << *p << endl;             // const在*之前,用法都一样
144 
145     int e = 50;
146     int* const p1 = &e;             // 常指针,无法修改指针,但是可以修改地址中的值
147     *p1 = 51;
148     cout << *p1 << endl;
149 
150     test();
151     */
152 
153     // -5- 函数参数的默认值
154     // 预处理(宏定义),编译,汇编,链接,生成可执行文件
155     /*
156     const int max = 1024;
157     cout << max << endl;
158 
159     cout << showdata(1,2) << endl;  // 从左往右依次赋值
160     */
161 
162     // -6- 函数重载
163     // 在C++中同一作用域,一组函数名相同,参数列表不同,返回值不受影响,这一组函数就会发生重载
164     // 原理:在C++编译器中,会根据函数名字符数+参数列表中的类型及顺序,自动生成新的函数名并记录在符号表中,
165     //      在函数调用时就会根据这个结果进行调用。又称编译时的多态(静态多态)。
166     func();
167     func(1);
168     func(3.14,5);
169 
170     cout << "Hello World!" << endl;
171     return 0;
172 }

 

标签:std,const,函数,int,C++,函数参数,using,cout
来源: https://www.cnblogs.com/heze/p/16222578.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有