// An example of using the same variable declarations in different scopes in C++ without conflicts
#include <iostream>
using std::cout;
using std::endl;
class abc //Class declaration statement, you can use struct, and union also.
{
public:
int x; //Variable declaration within class.
abc():x(10){} //Constructor to intialize data members.
};
namespace def //Namespace declaration statement.
{
int x = 20;//Variable declaration within def space.
}
int getx() //Function return its local variable.
{
int x = 30; //Variable declaration within function space.
return x;
}
int x = 40; //Global space variable declaration.
int main()
{
int x = 50;//Local space variable declaration within main function.
abc a;
cout << "Scope within class abc: x = " << a.x
<< "\nScope within namespace def: x = " << def::x
<< "\nScope within function getx: x = " << getx()
<< "\nScope within global scope : x = " << ::x
<< "\nScope within local main function: x = " << x;
{
int x = 60; //Local scope variable declaration within segment of code
cout << "\nScope within local code block: x = " << x;
}
cout<<endl<<endl;//Send two new line to the screen
return 0;
}
/*Program Output:
Scope within class abc: x = 10
Scope within namespace def: x = 20
Scope within function getx: x = 30
Scope within global scope : x = 40
Scope within local main function: x = 50
Scope within local code block: x = 60
*/
var.rar_Conflicts
版权申诉
103 浏览量
2022-09-14
21:47:48
上传
评论
收藏 666B RAR 举报
周楷雯
- 粉丝: 80
- 资源: 1万+
最新资源
- html实现儿童节庆祝项目源码
- Typora-1.3.8(含激活文件)
- Python爬取百度贴吧数据.zip
- Python爬取大众点评数据.zip
- 计算机视觉(CV)open CV 人脸识别程序.docx
- python 端午节游戏,选择三个传统的端午节活动之一:包粽子、赛龙舟或挂艾草 根据玩家的选择,游戏会描述相应的情节,并在完成后
- C语言各版块实验题库、实验汇报以及实验教学案例资源
- 数据库连接(MySQL与VS2022)_vs2022 mysql-CSDN博客 (2024_6_1 19_11_34).html
- Flappy bird小游戏
- 数电实验五:555单稳态触发电路、555单稳态触发电路、555构成的施密特触发器
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈