C++入门阶段<1>-------->命名空间、输入输出与缺省参数

一、命名空间

1.命名空间的引入

C++是带类的C语言,为什么要有C++的存在?这个问题很大,我们在今后的学习过程中会不断深入对此进行讲解,今天我们先从作用域这个角度讲解它们的不同之处。

我们知道编译器查找域的时候,正常情况下先查找局部域再查找全局域。

下面这段代码是我们在局部域中定义了一个变量并将它打印出来,好像看上去没什么问题。

#include <iostream>


int main()
{
	int rand = 0;
	printf("%d",rand);
	return 0;
}

运行结果:

2.域作用限定符

如果定义在全局中,我们要使用这个全局变量我们该如何使用呢?此时我们使用域作用限定符即可。::域作用限定符左侧没有操作对象时,默认为从全局域去查找。

#include <iostream>

int a = 10;

int main()
{
	int a = 0;
	printf("%d",::a);
	return 0;
}

运行结果:

 但是,值得注意的是C语言的编译器并不支持域作用限定符

3.介绍命名空间

不知道大家在学习C语言的时候,有没有遇到过定义一个变量名时,与C语言的库函数或者自己定义的函数中使用的变量发生了冲突。

比如,典型的例子之一rand的定义。

众所周知,下面这个例子是不会出现错错误的。

#include <stdio.h>

int main()
{
	int rand = 0;
	printf("rand=%d\n",rand);
	return 0;
}

那如果我们将rand改为全局变量并且加上<stdlib.h>这个库之后呢?

 

此时,我们发现出现了一定的错误。

 那如何解决这个问题呢?我们可以使用命名空间namespace。

加上命名空间之后:运行程序可以通过。

 如果我们要使用命名空间中的内容我们如何解决呢?

4.使用命名空间的三种方式

通常来说有三种方式:

1.方式1:指定访问

#include <stdio.h>
#include <stdlib.h>
namespace lgn
{
	int rand = 402;
}
int main()
{
	printf("rand=%d\n", lgn::rand);
	return 0;
}

运行结果:

2.方式2:全展开

#include <stdio.h>
#include <stdlib.h>

namespace lgn
{
	int rand = 402;
}
//展开命名空间  全展开
using namespace lgn;

int main()
{
	printf("rand=%d\n", lgn::rand);
	return 0;
}

运行结果:

 

3.方式3:指定展开

#include <iostream>

namespace lgn
{
	int x = 402;
}
//指定展开某一个
using lgn::x;

int main()
{
	printf("x=%d\n",x);
	return 0;
}

 运行结果:

总结:目前学习C++的命名空间后,我们得到了下面三个域:

1.局部域

2.全局域

3.命名空间域

其中,命名空间域并不影响变量(对象)的生命周期,均是全局变量。不同域可以定义同名的变量、函数、类型。

二、输入与输出

在学习C语言的时候,我们学习过输入与输出。

1.输出

在C语言中,将内容打印在屏幕上的函数是printf函数。由于篇幅受限,此处不过多讲述。感兴趣的读者可以参考我之前的博客:C语言(5)--------->printf函数_c语言printf-CSDN博客

#include <stdio.h>

int main()
{
	int x = 100;
	printf("%d\n",x);
	return 0;
}

C++中的输出使用的是流插入操作符:

#include <iostream>

int main()
{
	int x = 10;
	std::cout << x << '\n' << std::endl;
	return 0;
}

 运行结果:

#include <stdio.h>

int main()
{
	int n = 0;
	scanf_s("%d",&n);
	printf("n=%d\n",n);
	return 0;
}

2.输入

在C语言中,我们使用scanf来输入,此处我们简单回忆一下,如果我们想深入探究,可以参考我之前的博客文章:C语言(6)------->scanf函数_c语言scanf函数-CSDN博客 

#include <stdio.h>

int main()
{
	int n = 0;
	scanf_s("%d",&n);
	printf("n=%d\n",n);
	return 0;
}

运行结果:

 在C++中,我们使用流提取操作符>>来输入。

#include <iostream>

int main()
{
	int a = 0;
	std::cin >> a;
	std::cout << a << std::endl;
	return 0;
}

运行结果:

3.多输出

如果有多输出,我们应该如何解决呢?

我们使用多条输出语句时,要多次使用std::cout以及std::endl。

这种情况,我们怎样优化以及解决呢?

#include <iostream>

using std::cout;
using std::endl;
int main()
{
	int x = 10;
	cout << x << '\n' << endl;
	cout << x << '\n' << endl;
	cout << x << '\n' << endl;
	cout << x << '\n' << endl;
	cout << x << '\n' << endl;
	cout << x << '\n' << endl;
	cout << x << '\n' << endl;
	cout << x << '\n' << endl;
	return 0;
}

 这也就是我们看到很多书上面为什么一开头就写std::cout和std::endl的原因。

三、缺省参数

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

1.不传入参数时:

#include <iostream>
int Add(int x = 0, int y = 0)
{
	return x + y;
}

int main()
{
	int x = 10;
	int y = 10;
	printf("%d\n",Add());
	return 0;
}

2.传入一个参数时: 

#include <iostream>
int Add(int x = 0, int y = 0)
{
	return x + y;
}

int main()
{
	int x = 10;
	int y = 10;
	printf("%d\n",Add(1));
	return 0;
}

 

3.传入两个参数时:

#include <iostream>
int Add(int x = 0, int y = 0)
{
	return x + y;
}

int main()
{
	int x = 10;
	int y = 10;
	printf("%d\n",Add(1,1));
	return 0;
}

 

4.半缺省

注意的是,从右往左依次给出半缺省参数,不能间隔着给。

C语言不支持缺省参数。

#include <iostream>
int Add(int x , int y = 0,int z=0)
{
	return x + y + z;
}

int main()
{
	int x = 10;
	int y = 10;
	printf("%d\n", Add(1, 1));
	return 0;
}

 

#include <iostream>
int Add(int x , int y = 0,int z=0)
{
	return x + y + z;
}

int main()
{
	int x = 10;
	int y = 10;
	printf("%d\n", Add(1));
	return 0;
}

#include <iostream>
int Add(int x , int y = 0,int z=0)
{
	return x + y + z;
}

int main()
{
	int x = 10;
	int y = 10;
	printf("%d\n", Add(1,1,1));
	return 0;
}

 若不传入参数,则会报错。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值