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

文章目录

  • 0.什么是C++
  • 1.C++关键字
  • 2.命名空间
    • 导入
    • 什么是命名空间
    • 命名空间的使用
  • 3.C++ 输入&输出
  • 4.缺省参数
    • 什么是缺省参数
    • 缺省参数的应用场景
  • 5.函数重载

0.什么是C++

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


1.C++关键字

asm do if return try continue
bool dynamic_cast int signed typeid public
break else long sizeof typename throw
case enum mutable static union wchar_t
catch explicit namespace static_cast unsigned default
char export new struct using friend
class extern operator switch virtual register
const false private template void true
const_cast float protected this volatile while
delete goto reinterpret_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 重名?
    C++初阶 -1- C++入门_第1张图片

    • 同样 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 
using namespace std;

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

C++初阶 -1- C++入门_第2张图片

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;
}

默认实参不在形参列表的结尾
C++初阶 -1- C++入门_第3张图片
如果这个需要调用Sum函数,且只给 int y 传参 Sum(2),这个数据2到底是传给x、y、·z中的哪一个就不得而知
C++初阶 -1- C++入门_第4张图片

  • 缺省参数不能在声明和定义中同时出现!推荐在声明的时候给缺省参数
//函数声明:
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++初阶 -1- C++入门_第5张图片

缺省参数的应用场景

(举例:栈的初始化)

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. 自动识别函数参数的类型 (会不会使运行速度变慢?不会,可能影响编译速度,但不影响运行速度)

你可能感兴趣的:(C++,c++,算法,c语言)