0
点赞
收藏
分享

微信扫一扫

ACM练习——第一天

善解人意的娇娇 2023-11-17 阅读 14

目录

一、什么是C++

1、C++关键字(C++98)

2、C++兼容C

 二、C++程序预处理指令

三、命名空间 

1、命名冲突 

第一种:

第二种:

2、域作用限定符

3、实现命名空间 

4、命名空间冲突

5、访问命名空间

6、命名空间“std”

 四、输入输出

1、定义 

2、自动识别类型 

3、格式化输出

五、缺省参数

1、全缺省 

2、半缺省 


一、什么是C++

1、C++关键字(C++98)

C++总计63个关键字,C语言32个关键字。通过不断地学习我们会逐渐掌握这些关键字。

2、C++兼容C

使用C语言的语法在 .cpp 文件中依然可以运行。 

 二、C++程序预处理指令

#include <iostream>

C和C++一样,使用一个预处理器 在进行主编译之前对源文件进行处理,上述的编译指令使预处理器将 iostream 文件的内容添加到程序中。

那么什么要将 iostream 文件的内容添加到程序中呢?

#include <iostream>
using namespace std;//后续讲解
int main()
{
	int a = 0;
	cin >> a;
	cout << a << endl;
	return 0;
}

 

三、命名空间 

1、命名冲突 

大体命名冲突有两种: 

  1. 我们自己定义和库里面的名字冲突
  2. 项目组,多个人之间定义的名字冲突 

第一种:

#include <stdio.h>

int rand = 1;

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

 

#include <stdio.h>
#include <stdlib.h>
int rand = 1;

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

 

这是因为<stdlib>头文件中定义了rand函数,当我们自己声明全局变量rand时,再包含头文件<stdlib.h> 就造成了 rand 的重定义

第二种:

#include <stdio.h>
#include "list.h"
#include "queue.h"

int main()
{
	return 0;
}

2、域作用限定符

在C/C++中,域(Scope)是指程序中变量、函数、类等实体的可见范围和生命周期。根据实体的定义位置和作用范围,可以将域分为以下几种类型:

  1. 全局域(Global Scope):全局域中定义的变量、函数、类等实体在整个程序中都是可见的,它们的生命周期与程序的运行时间相同。在C/C++中,全局变量和全局函数默认情况下都属于全局域。

  2. 局部域(Local Scope):局部域中定义的变量、函数、类等实体只在其定义的代码块中可见,它们的生命周期与代码块的执行时间相同。在C/C++中,函数中定义的变量和函数参数都属于局部域。

#include<stdio.h>
int a = 2;

void f1()
{
	int a = 0;
}
void f2()
{
	int a = 1;
}

int main()
{
	printf("%d\n", a);
	return 0;
}
int a = 2;
void f1()
{
	int a = 0;
	printf("%d\n",::a);  // ::域作用限定符
}

3、实现命名空间 

#include <stdio.h>
#include "list.h"
#include "queue.h"
int main()
{
	struct Q::Node node1;
	struct L::Node node2;
	return 0;
}

int main()
{
	struct Q::Node node1;
	struct L::Node node2;
	
	Q::x++;
	L::x++;
	return 0;
}

4、命名空间冲突

 比如这种情况:

#include <stdio.h>
#include "list.h"
#include "queue.h"
int main()
{
	struct Code::Q::Node node1;
	struct Code::L::Node node2;
	
	Code::Q::x++;
	Code::L::x++;
	return 0;
}

C++中多个文件中相同的命名空间会被合并成一个命名空间。这是因为命名空间是C++中用于避免命名冲突的机制,它的作用是将一组相关的函数、类、变量等封装在一个命名空间中,以避免命名冲突和提高代码的可读性。

5、访问命名空间

6、命名空间“std”

#include <iostream>
using namespace std;
int main()
{
	int a = 0;
	cin >> a;
	cout << a << endl;
	return 0;
}
  • 如果不使用 using namespace std;语句,需要在前面加上std::前缀,以指明它们属于std命名空间。
  • 如果使用 using namespace std;语句可以让我们直接使用这些函数和对象,而不需要加上std::前缀。

 四、输入输出

1、定义 

  • 使用cout标准输出对象(控制台)cin标准输入对象(键盘)时,必须包含< iostream >头文件 以及按命名空间使用方法使用std。
  • cout和cin是全局的流对象,endl是特殊的C++符号,表示换行输出,他们都包含在包含<
  • iostream >头文件中。
  • <<是流插入运算符,>>是流提取运算符
#include <iostream>
using namespace std;
int main()
{
	int a;
	cin >> a;
	cout << a << endl;
	return 0;
}

2、自动识别类型 

  • 使用C++输入输出更方便,不需要像printf/scanf输入输出时那样,需要手动控制格式。
  • C++的输入输出可以自动识别变量类型。
#include <iostream>
using namespace std;
int main()
{
	int n = 0;
	cin >> n;
	double* a = (double*)malloc(sizeof(int) * n);
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	for (int i = 0; i < n; i++)
	{
		cout << a[i] << endl;
	}
	return 0;
}

3、格式化输出

 这种情况用cout输出就显得有些麻烦,此时用C语言的printf进行格式化输出就方便许多。

int main()
{
	char name[100] = "Kelly";
	int age = 20;

	cout << "name:" << name << endl;
	cout << "age:" << age << endl;

	printf("name:%s\nage:%d\n", name, age);
	return 0;
}

五、缺省参数

void func(int a = 0)
{
	cout << a << endl;
}
int main()
{
	func();
	return 0;
}

func(666);

1、全缺省 

void Func(int a = 1, int b = 2, int c = 3)
{
	cout << a << " " << b << " " << c << endl;
}
int main()
{
	Func(4, 5, 6);
	Func(4, 5);
	Func(4);
	Func();
	return 0;
}

 输出结果如下:

2、半缺省 

void Func(int a , int b = 2, int c = 3)
{
	cout << a << " " << b << " " << c << endl;
}
int main()
{
	Func(4, 5, 6);
	Func(4, 5);
	Func(4);
	//Func(); 至少传一个值给a
	return 0;
}

void Func(int a = 0, int b , int c = 3)

 注意!

举报

相关推荐

【第一天】

第一天

第一天复习

HCIP第一天

html第一天

Java第一天

第一天作业

出差第一天

javaSE第一天

0 条评论