优秀的编程知识分享平台

网站首页 > 技术文章 正文

C++头文件和命名空间(c++头文件命名规则)

nanyue 2024-08-16 19:59:46 技术文章 11 ℃

小伙伴们,今天小易又要开始讲C++啦,上次我们了解了输入与输出,这次我们来看一下输出程序的另一部分,头文件和命名空间啦。记住老规矩,开始新的程序编写时记得注释掉之前的代码哦!

好了,废话不多说先上干货。

1.C/C++各种头文件

注:"//"代表注释

#include <assert.h> //设定插入点

#include <ctype.h> //字符处理

#include <errno.h> //定义错误码

#include <float.h> //浮点数处理

#include <fstream.h> //文件输入/输出

#include <iomanip.h> //参数化输入/输出

#include <iostream.h> //数据流输入/输出

#include <limits.h> //定义各种数据类型最值常量

#include <locale.h> //定义本地化函数

#include <math.h> //定义数学函数

#include <stdio.h> //定义输入/输出函数

#include <stdlib.h> //定义杂项函数及内存分配函数#include <string.h> //字符串处理

#include <strstrea.h> //基于数组的输入/输出

#include <time.h> //定义关于时间的函数#include <wchar.h> //宽字符处理及输入/输出

#include <wctype.h> //宽字符分类//////////////////////////////////////////////////////////////////////////标准 C++ (同上的不再注释)

#include <algorithm> //STL 通用算法

#include <bitset> //STL 位集容器

#include <cctype>#include <cerrno>

#include <clocale>

#include <cmath>

#include <complex> //复数类#include <cstdio>#include <cstdlib>

#include <cstring>#include <ctime>

#include <deque> //STL 双端队列容器

#include <exception> //异常处理类

#include <fstream>

#include <functional> //STL 定义运算函数(代替运算符)

#include <limits>#include <list> //STL 线性列表容器#include <map> //STL 映射容器

#include <iomanip>#include <ios> //基本输入/输出支持#include <iosfwd> //输入/输出系统使用的前置声明

#include <iostream>

#include <istream> //基本输入流

#include <ostream> //基本输出流

#include <queue> //STL 队列容器

#include <set> //STL 集合容器#include <sstream> //基于字符串的流

#include <stack> //STL 堆栈容器

#include <stdexcept> //标准异常类

#include <streambuf> //底层输入/输出支持

#include <string> //字符串类

#include <utility> //STL 通用模板类

#include <vector> //STL 动态数组容器

#include <cwchar>

#include <cwctype>using namespace std;//////////////////////////////////////////////////////////////////////////C99 增加

#include <complex.h> //复数处理

#include <fenv.h> //浮点环境

#include <inttypes.h> //整数格式转换

#include <stdbool.h> //布尔环境

#include <stdint.h> //整型环境

#include <tgmath.h> //通用类型数学宏

2.命名空间

一个命名空间的定义包含两部分:首先是关键字namespace,随后是命名空间的名字。在命名空间名字后面是一系列由花括号括起来的声明和定义。只要能出现在全局作用域中的声明就能置于命名空间内,主要包括:类、变量(及其初始化操作)、函数(及其定义)、模板和其它命名空间。命名空间结束后无须分号,这一点与块类似。和其它名字一样,命名空间的名字也必须在定义它的作用域内保持唯一。命名空间既可以定义在全局作用域内,也可以定义在其它命名空间中,但是不能定义在函数或类的内部。命名空间作用域后面无须分号。

命名空间相当于一个容器,它里面包含了逻辑结构上互相关联的一组类、模板、函数等。也就是说如果某些“对象”在逻辑上有关系,我们就可以将它们放到一个命名空间里用以和外界进行区分。命名空间一个显著的特点是命名空间内的变量(类等)名可以和命名空间以外的重名。这可以用来将不同人写的代码进行整合。

class A //声明A类

{

public:

void funl();//声明A类中的funl函数

private:

int i;

};

void A::funl() //定义A类中的funl函数

{…………}

class B //声明B类{

public:

void funl(); //B类中也有funl函数

void fun2();

};

void B::funl() //定义B类中的funl函数

{ …………}

在文件中定义了两个类,在这两个类中可以有同名的函数。在引用时,为了区别,应该加上类名作为限定:这样不会发生混淆。

在文件中可以定义全局变量(global variable),它的作用域是整个程序。如果在文件A中定义了一个变量a:

int a=3;

在文件B中可以再定义一个变量a:

int a=5;

在分别对文件A和文件B进行编译时不会有问题。但是,如果一个程序包括文件A和文件B,那么在进行连接时,会报告出错,因为在同一个程序中有两个同名的变量,认为是对变量的重复定义。

可以通过extern声明同一程序中的两个文件中的同名变量是同一个变量。如果在文件B中有以下声明:

extem int a;

表示文件B中的变量a是在其他文件中已定义的变量。由于有此声明,在程序编译和连接后,文件A的变量a的作用域扩展到了文件B。如果在文件B中不再对a赋值,则在文件B中用以下语句输出的是文件A中变量a的值:

cout<<a; //得到a的值为3

3.命名冲突

首先,若程序员A在头文件headerl.h中定义了类Student和函数fun。

#include <string>
#include <cmath>using namespace std;
class Student //声明Student类
{ 
public:
 Student(int n,string nam,int a)
 { 
 num=n;
 name=nam;
 age=a;
 } 
 void get_data();
private: 
 int num; 
 string name; 
 int age; 
};
void Student::get_data() //成员函数定义
{ 
 cout
 cout<<num<<" "<<name<<" "<<age<<endl; 
}
double fun(double a,double b)//定义全局函数(即外部函数)
{ 
 
 return sqrt(a+b);
}//在main函数所在的文件中包含头文件headerl.h:
#include <iostream>
using namespace std; 
#include "header1.h" //注意要用双引号,因为文件一般是放在用用户目录中的
int main()
{
 Student stud1(101,"Wang",18); //定义类对象studl 
 stud1.get_data(); 
 cout<<fun(5,3)<<endl; 
 return 0; 
}
---------------------------------------------------
程序能正常运行,输出为 101 Wang 18
2.82843

如果程序员乙写了头文件header2.h,在其中除了定义其他类以外,还定义了类Student和函数fun,但其内容与头文件headerl.h中的Student和函数fun有所不同

#include <string>
#include <cmath>
using namespace std;
class Student //声明Student类
{
public:
 Student(
 Student(int n,string nam,char s) //参数与headerl中的student不同 { 
 num=n;
 name=nam;
 sex=s;
 } 
 void get_data();
private: 
 int num; 
 string name; 
 char sex; 
};
};//此项与headerl不同
void Student::get_data() //成员函数定义
{ 
 cout<<num<<" "<<name<<" "<<sex<<endl; 
}
double fun(double a,double b) //定义全局函数 
{ 
 
 return sqrt(a-b);
} //返回值与headerl中的fun函数不同
//头文件2中可能还有其他内容

Tags:

最近发表
标签列表