-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/************************************************************************* | ||
> File Name: const.cpp | ||
> Author: | ||
> Mail: | ||
> Created Time: 日 9/11 14:43:40 2016 | ||
************************************************************************/ | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
int x = 10; | ||
|
||
// 常量不可修改 | ||
// 大范围的值可以设定小范围变量,小范围的值不可以设定大范围的变量 | ||
// 引用:可以理解为,将当前变量或常量的地址映射到其他变量的地址上 | ||
// 虽然常量继承了引用变量的值,但是常量仍然不可更改, | ||
// 但是可以通过修改该变量的值,从而实现修改常量值的实现 | ||
|
||
const int &MYNUM = x; | ||
|
||
int main() | ||
{ | ||
cout << "Const num:" << MYNUM << endl; | ||
int b = 12; | ||
x = b; | ||
cout << "Const num:" << MYNUM << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/************************************************************************* | ||
> File Name: mem_del.cpp | ||
> Author: | ||
> Mail: | ||
> Created Time: 日 9/11 15:08:32 2016 | ||
************************************************************************/ | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
// 先声明一个指针变量,然后给指针取申请内存,内存大小可以在后面按需所定义 | ||
int *p = new int[12]; | ||
if(NULL == p) | ||
{ | ||
cout << "The mem storage value is null!" << endl; | ||
return 0; | ||
} | ||
|
||
// 为申请下来的内存分配数值 | ||
*p = 20; | ||
cout << "The mem storage value is:" << *p << endl; | ||
// 程序最后需要回收内存空间,以及需要重置指针 | ||
delete p; | ||
p = NULL; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/************************************************************************* | ||
> File Name: overloadfunc.cpp | ||
> Author: | ||
> Mail: | ||
> Created Time: 日 9/11 15:17:23 2016 | ||
************************************************************************/ | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
// 定义函数名相同的两个函数,只要起参数个数、类型不一致,就不会报错 | ||
int getMax(int x, int y) | ||
{ | ||
if(x > y) | ||
{ | ||
return x; | ||
} | ||
|
||
return y; | ||
} | ||
|
||
double getMax(double x, double y) | ||
{ | ||
if(x > y) | ||
{ | ||
return x; | ||
} | ||
return y; | ||
} | ||
|
||
namespace Mark | ||
{ | ||
int getMax(int x, int y) | ||
{ | ||
if(x > y) | ||
{ | ||
return x; | ||
} | ||
|
||
return y; | ||
} | ||
|
||
double getMax(double x, double y) | ||
{ | ||
if(x > y) | ||
{ | ||
return x; | ||
} | ||
return y; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int x = 10; | ||
int y = 12; | ||
|
||
double m = 1.2; | ||
double n = 1.24; | ||
|
||
cout << "The max value is:" << getMax(x, y) << endl; | ||
|
||
cout << "The max value is:" << getMax(m, n) << endl; | ||
|
||
cout << "\n ------------------< 以下为 namespace 输出 >--------------------- \n" << endl; | ||
|
||
cout << "The Mark max value is:" << Mark::getMax(x, y) << endl; | ||
|
||
cout << "The Mark max value is:" << Mark::getMax(m, n) << endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/************************************************************************* | ||
> File Name: pointer.cpp | ||
> Author: | ||
> Mail: | ||
> Created Time: 日 9/11 14:56:56 2016 | ||
************************************************************************/ | ||
|
||
#include <iostream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
int *ip; /* 一个整型的指针 */ | ||
double *dp; /* 一个 double 型的指针 */ | ||
float *fp; /* 一个浮点型的指针 */ | ||
char *ch; /* 一个字符型的指针 */ | ||
|
||
|
||
int var = 20; | ||
ip = &var; // 在指针变量中存储 var 的地址 | ||
|
||
// 输出在指针变量中存储的地址 | ||
cout << "Address stored in var variable:"; | ||
cout << &var << endl; | ||
|
||
// 访问指针中地址的值 | ||
cout << "Value of var variable:"; | ||
cout << var << endl; | ||
|
||
// 输出在指针变量中存储的地址 | ||
cout << "Address stored in ip variable:"; | ||
cout << ip << endl; | ||
|
||
// 访问指针中地址的值 | ||
cout << "Value of *ip variable:"; | ||
cout << *ip << endl; | ||
|
||
return 0; | ||
} |