C++初阶 -1- C++入门

0.什么是C++

C++是基于C语言而产生的,它既可以进行C语言的过程化程序设计,又可以进行以抽象数据类型为特点的基于对象的程序设计,还可以进行面向对象的程序设计。


1.C++关键字

asmdoifreturntrycontinue
booldynamic_castintsignedtypeidpublic
breakelselongsizeoftypenamethrow
caseenummutablestaticunionwchar_t
catchexplicitnamespacestatic_castunsigneddefault
charexportnewstructusingfriend
classexternoperatorswitchvirtualregister
constfalseprivatetemplatevoidtrue
const_castfloatprotectedthisvolatilewhile
deletegotoreinterpret_cast

2.命名空间

导入

  • 全局变量与局部变量冲突时,优先哪个?
    • 局部变量 → 因为查找的顺序是:先在局部 ⇢ 再去全局
  • [ (域) ]:: 域作用限定符 ⇨ 在左边这个(域)里面查找变量,为空就是在全局找
  • 为了解决变量名冲突的问题,命名空间应运而生

什么是命名空间

namespace (name){ 
   ……(在这里面定义变量)}
  • namespace 关键字
  • (name) :: →在名为 (name) 的域内查找变量(name中不要加空格)
    在这里插入图片描述

正确使用示例:

namespace fantasy
{
	int a = 13;
	int l = 7;
	struct MyStructRB
	{
		int* array;
		int size;
	}RB;
}


int main()
{
	int sum = fantasy::a + fantasy::l;

	fantasy::RB.size = 0;
	fantasy::RB.array = nullptr;

	return 0;
}

命名空间的使用

  • 命名空间的 name 重名?
    在这里插入图片描述

    • 同样 name 的命名空间会被合并
  • 命名空间可以嵌套

namespace fantasy
{
	int a = 13;
	int l = 7;
	namespace RoundBottle
	{
		struct MyStructRB
		{
			int* array;
			int size;
		}RB;
	}
}

int main()
{
	fantasy::RoundBottle::RB.size = 0;
	fantasy::RoundBottle::RB.array = nullptr;
	return 0;
}
  • 命名空间的三种使用方式

    1.指定命名空间访问
    2.全局展开(日常练习中可以,项目中一般不会用全局展开)
    using namespace (name);
    3.部分展开
    using (name)::(命名空间的成员);

1.指定命名空间访问
namespace fantasy
{
	struct MyStructRB
	{
		int* array;
		int size;
	}RB;

}

int main()
{
	fantasy::RB.size = 0;
	fantasy::RB.array = nullptr;
	return 0;
}
2.全局展开(日常练习中可以,项目中一般不会用全局展开)
namespace fantasy
{
	struct MyStructRB
	{
		int* array;
		int size;
	}RB;

}

//全局展开
using namespace fantasy;

int main()
{
	RB.size = 0;
	RB.array = nullptr;
	return 0;
}
3.部分展开
namespace fantasy
{
	struct MyStructRB
	{
		int* array;
		int size;
	}RB;

}

//部分展开
using fantasy::RB;

int main()
{
	RB.size = 0;
	RB.array = nullptr;
	return 0;
}

3.C++ 输入&输出

  • std:C++标准库
std是C++标准库的命名空间名,C++将标准库的定义实现都放到这个命名空间中
using namespace std;
  • cout:输出👉 cout << 输出内容 << endl;
  • cin:输入
  • endl 👉 换行符
  • << 流插入运算符; >> 是流提取运算符。
#include <iostream>
using namespace std;

int main()
{
	int num = 0;
	// 可以自动识别变量的类型
	cin >> num;
	cout << num << endl;
	return 0;
}

在这里插入图片描述

4.缺省参数

什么是缺省参数

缺省参数是声明或定义函数时为函数的参数指定一个缺省值。在调用该函数时,如果没有指定实参则采用该形参的缺省值,否则使用指定的实参。

示例:

  • 缺省参数分为全缺省部分缺省
//全缺省
int Add(int x = 1, int y = 2)
{
	return x + y;
}

//部分缺省
int Sum(int x, int y, int z = 3)
{
	return x + y + z;
}
  • 缺省参数不能跳跃,必须从左往右连续使用
    • 错误示例:
×错误的:
//部分缺省
int Sum(int x = 1, int y, int z = 3)
{
	return x + y + z;
}
//or
int Sum(int x = 1, int y, int z)
{
	return x + y + z;
}

👉默认实参不在形参列表的结尾
在这里插入图片描述
💭如果这个需要调用Sum函数,且只给 int y 传参 👉 Sum(2),这个数据2到底是传给x、y、·z中的哪一个就不得而知
在这里插入图片描述

  • 缺省参数不能在声明和定义中同时出现!👉推荐在声明的时候给缺省参数
//函数声明:
int Add(int x = 1, int y = 2);

int main()
{
	//函数调用:
	cout << Add() << endl;
	return 0;
}

//函数定义:
int Add(int x = 1, int y = 2)
{
	return x + y;
}

在这里插入图片描述

缺省参数的应用场景

(举例:栈的初始化)

C:

// 初始化栈 
void StackInit(Stack* ps)
{
	STDataType* tmp = (STDataType*)malloc(4 * sizeof(STDataType));
	if (!tmp)
	{
		perror("malloc fail");
		exit(-1);
	}
	ps->_a = tmp;
	ps->_top = 0;
	ps->_capacity = 4;
}

CPP:

// 初始化栈 
void StackInit(Stack* ps, int initsize = 4)
{
	int* tmp = (int*)malloc(initsize * sizeof(int));
	if (!tmp)
	{
		perror("malloc fail");
		exit(-1);
	}
	ps->_a = tmp;
	ps->_top = 0;
	ps->_capacity = initsize;
}
  • 知道栈中最多存100个数据:

    • StackInit(&stack, 100);
  • 不知道栈中最多存多少数据:

    • StackInit(&stack);

5.函数重载

C语言不允许同名函数名的存在 →(为了解决这个问题) C++函数重载

函数重载 → 在同一个命名空间,且函数名相同,参数不同
1.类型不同
2.个数不同
3.(类型的)顺序不同

1.参数类型不同:
void func(int x, int y)
{
	cout << x + y << endl;
}

void func(char x, char y)
{
	cout << x << y << endl;
}

int main()
{
	func(1, 2);
	func('a', 'b');

	return 0;
}

output:
3
ab

2.参数个数不同:
void func(int x)
{
	cout << x << endl;
}

void func(int x, int y)
{
	cout << x + y << endl;
}

int main()
{
	func(1, 2);
	func(3);
	return 0;
}

output:
3
3

3.(参数的类型)顺序不同 
void func(char x, int y)
{
	cout << x << y << endl;
}

void func(int x, char y)
{
	cout << x << y << endl;
}

int main()
{
	func('a', 13);
	func(7, 'l');
	return 0;
}

output:
a13
7l

  • 仅返回值不同,能构成函数重载吗?

    • 不能
  • 函数重载是如何实现的?

    1. C++对函数名进行了修饰,不同平台下的修饰规格不同
    2. 自动识别函数参数的类型 (会不会使运行速度变慢?👉不会,可能影响编译速度,但不影响运行速度)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

畋坪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值