C/C++总述:Study C/C++-CSDN博客
C++是什么
C++是在C的基础之上,容纳进去了面向对象编程思想,并增加了许多有用的库,以及编程范式等 。
最简单的C++程序
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World";
return 0;
}
C++关键字
(C++总计63个关键字,C语言32个关键字)
asm | else | new | this |
auto | enum | operator | throw |
bool | explicit | private | true |
break | export | protected | try |
case | extern | public | typedef |
catch | false | register | typeid |
char | float | reinterpret_cast | typename |
class | for | return | union |
const | friend | short | unsigned |
const_cast | goto | signed | using |
continue | if | sizeof | virtual |
default | inline | static | void |
delete | int | static_cast | volatile |
do | long | struct | wchar_t |
double | mutable | switch | while |
dynamic_cast | namespace | template |
命名空间
在 C/C++ 中,变量、函数和后面要学到的类都是大量存在的,这些变量、函数和类的名称将都存在于全局作用域中,可能会导致很多冲突。使用命名空间的目的是对标识符的名称进行本地化, 以避免命名冲突或名字污染 , namespace 关键字的出现就是针对这种问题的。
命名空间定义
定义命名空间,需要使用到 namespace 关键字 ,后面跟 命名空间的名字 ,然 后接一对 {} 即可, {} 中即为命名空间的成员。
namespace Gh
{
// 命名空间中可以定义变量/函数/类型
int rand = 10;
int Add(int left, int right)
{
return left + right;
}
struct Node
{
struct Node* next;
int val;
};
}
一个命名空间就定义了一个新的作用域,命名空间中的所有内容都局限于该命名空间中
//命名空间还可嵌套使用
namespace N1
{
int a;
int b;
int Add(int left, int right)
{
return left + right;
}
namespace N2
{
int c;
int d;
int Sub(int left, int right)
{
return left - right;
}
}
}
命名空间使用
1.加命名空间名称及作用域限定符
int main()
{
printf("%d\n", N::a);
return 0;
}
2.使用using将命名空间中某个成员引入
using N::b;
int main()
{
printf("%d\n", N::a);
printf("%d\n", b);
return 0;
}
3.使用using namespace 命名空间名称 引入
using namespce N;
int main()
{
printf("%d\n", a);
printf("%d\n", b);
return 0;
}
缺省参数
缺省参数概念:
缺省参数是 声明或定义函数时 为函数的 参数指定一个缺省值 。在调用该函数时,如果没有指定实参则采用该形参的缺省值,否则使用指定的实参。
#include <iostream>
using namespace std;
// 声明一个带有缺省参数的函数
void printMessage(string message = "Hello, World!") {
cout << message << endl;
}
int main() {
// 调用函数时不提供参数值,将使用默认值
printMessage(); // 输出: Hello, World!
// 调用函数时提供参数值,将使用提供的值
printMessage("Hello, C++!"); // 输出: Hello, C++!
return 0;
}
缺省参数分类:
全缺省参数
void Func(int a = 10, int b = 20, int c = 30)
{
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl;
}
半缺省参数
void Func(int a, int b = 10, int c = 20)
{
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl;
注意:
1. 半缺省参数必须从右往左依次来给出,不能间隔着给。2. 缺省参数不能在函数 声明 和 定义 中同时出现(如果声明与定义位置同时出现,恰巧两个位置提供的值不同,那编译器就无法确定到底该用那个缺省值)。
//a.h
void Func(int a = 10);
// a.cpp
void Func(int a = 20)
{}
函数重载
函数重载定义
在同一个作用域内,可以声明几个功能类似的同名函数,但是这些同名函数的形式参数(指参数的个数、类型或者顺序)必须不同。您不能仅通过返回类型的不同来重载函数。
#include <iostream>
using namespace std;
class printData
{
public:
void print(int i) {
cout << "整数为: " << i << endl;
}
void print(double f) {
cout << "浮点数为: " << f << endl;
}
void print(char c[]) {
cout << "字符串为: " << c << endl;
}
};
int main(void)
{
printData pd;
// 输出整数
pd.print(5);
// 输出浮点数
pd.print(500.263);
// 输出字符串
char c[] = "Hello C++";
pd.print(c);
/*输出为:
整数为: 5
浮点数为: 500.263
字符串为: Hello C++
*/
return 0;
}
引用
引用定义:
引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字。对引用的操作与对其所绑定的变量或对象的操作完全等价。
注意:
1. 引用在 定义时必须初始化2. 一个变量可以有多个引用3. 引用一旦引用一个实体,再不能引用其他实体
引用做参数
#include <iostream>
using namespace std;
// 函数声明
void swap(int& x, int& y);
int main ()
{
// 局部变量声明
int a = 100;
int b = 200;
cout << "交换前,a 的值:" << a << endl;
cout << "交换前,b 的值:" << b << endl;
/* 调用函数来交换值 */
swap(a, b);
cout << "交换后,a 的值:" << a << endl;
cout << "交换后,b 的值:" << b << endl;
return 0;
}
// 函数定义
void swap(int& x, int& y)
{
int temp;
temp = x; /* 保存地址 x 的值 */
x = y; /* 把 y 赋值给 x */
y = temp; /* 把 x 赋值给 y */
return;
}
引用做返回值
#include <iostream>
using namespace std;
int& change(int& num) {
num *= 2;
return num;
}
int main() {
int number = 5;
cout << "原始值:" << number << endl;
change(number); // 使用返回的引用修改number的值
cout << "修改后的值:" << number << endl;
return 0;
}