C++面向对象(1):防卫式声明
C++ Header Guards
- Description:
- 例一:定义同名函数
- 例二:重复调用头文件
- 防卫式声明:
Description:
C++ 中的条件 Header Guards(防卫式声明)是条件编译指令,有助于避免由于程序员的错误多次定义相同的函数或变量时出现的错误。 根据 C++,当一个函数或一个变量被多次定义时,它会产生一个错误。
例一:定义同名函数
代码:
#include <iostream>
using namespace std;void complex() {cout<<"this number is complex number!"<<endl;return;
}void complex() {cout<<"this number is also complex number!"<<endl;return;
}
int main() {complex();return 0;
}
Output:
E:\TSWorld程序\complex.cpp:10:6: error: 'void complex()' previously defined here
分析:
定义了两个同名函数,所以会报错。
例二:重复调用头文件
Program 1:fruit.h
// C++ program to create fruit class
// file named as "fruit.h"
#include <iostream>
#include <cstring>
using namespace std;// Fruit Class
class Fruit {string name;string color;public:void input() {name = "apple";color = "red";}void display() {cout<< name <<" color is " << color <<endl;}
};
Program 2: apple.h
// C++ program
// file named as "apple.h"
#include <iostream>
#include <cstring>
#include "fruit.h"
using namespace std;class apple {Fruit a;
public:void apple_input() {a.input();}void apple_display() {a.display();}
};
Program 3: main.cpp
// C++ program
// file named as "main.cpp"
# include "fruit.h"
# include "apple.h"
# include <iostream>
using namespace std;int main() {apple a;a.apple_input();a.apple_display();return 0;
}
Output:
In file included from E:\TSWorld程序\main.cpp:3:0:
E:\TSWorld程序\fruit.h:10:7: error: previous definition of 'class Fruit'class Fruit {
分析:
main.cpp 和 apple.h 都调用的 fruit.h,重复调用了头文件。
防卫式声明:
Program 1:fruit.h
// C++ program to create fruit class
// file named as "fruit.h"
#ifndef _FRUIT_
#define _FRUIT_#include <iostream>
#include <cstring>
using namespace std;// Fruit Class
class Fruit {string name;string color;public:void input() {name = "apple";color = "red";}void display() {cout<< name <<" color is " << color <<endl;}
};#endif
Program 2: apple.h
// C++ program
// file named as "apple.h"
#ifndef _APPLE_
#define _APPLE_#include <iostream>
#include <cstring>
#include "fruit.h"
using namespace std;class apple {Fruit a;
public:void apple_input() {a.input();}void apple_display() {a.display();}
};#endif
Program 3: main.cpp
// C++ program
// file named as "main.cpp"
# include "fruit.h"
# include "apple.h"
# include <iostream>
using namespace std;int main() {apple a;a.apple_input();a.apple_display();return 0;
}
Output:
apple color is red
C++面向对象(1):防卫式声明相关推荐
- c语言头文件和源文件_C语言头文件防卫式声明
C语言一般提供三种预处理功能:宏处理.文件包含.条件编译.头文件防卫式申明中会用到条件编译中 #ifndef.#define.#endif 的用法.所以,首先价绍下条件编译. 1 条件编译 一般情况下 ...
- C++头文件的防卫式声明(为了防止多次include)
C++的某个头文件通常需要被多个文件include,为了防止同一个头文件被包含多次导致了重复定义,需要在头文件中加上以防卫式声明,例如对于头文件的"complex.h",其防卫式声 ...
- 头文件防卫式声明/引用/static/extern/
文章目录 1.头文件防卫式声明 2.引用 2.1 引用的本质 2.2 引用与指针的区别(特别的const指针?) 2.3引用传递和引用返回 3.static 3.1 静态局部变量 3.2 全局变量 3 ...
- C++中的防卫式声明
防卫式声明(guard)的写法及其作用. 先按住不说 guard 的写法,我们先谈谈为什么需要在头文件开头加上防卫式声明. 假如你现在在撸一个项目,该项目需要包含众多头文件: #include< ...
- c++之防卫式声明和模板
文章目录 防卫式声明 书写格式 模板 函数模板 类模板 防卫式声明 在C++中头文件最好都进行防卫式声明,这是头文件比较规范的一种写法:这样就能够防止同一个头文件被包含多次从而,导致重复定义. 书写格 ...
- c++头文件中的防卫式声明
c++关于头文件中的防卫式声明 防止由于同一个头文件被包含多次,从而导致重复定义 在写头文件时,有两种方法用来进行防卫式声明: 1.宏定义方法: #ifndef FILENAME #define FI ...
- c++头文件防卫式声明
c++头文件防卫式声明 实例: 添加两个头文件head1.h,head2.h 在两个头文件中分别定义两个全局变量: int g_globalh1 = 8; int g_globalh2 = 5; 主程 ...
- C++ 局部变量及初始化 auto 头文件防卫式声明 引用 常量
cpp文件 project3 #include<iostream> #include"head1.h" #include"head2.h" usin ...
- 【深入理解C++】头文件防卫式声明
文章目录 1.extern "C" 的作用 2.__cplusplus 的作用 3.防止头文件被重复包含 3.1 #ifndef...#define...#endif 3.2 #p ...
最新文章
- 【MaxCompute】学习笔记常用查询sql
- Android开发之判断任意View上拉的方法
- centos7.2下编译安装git
- wx.checkjsapi是写在config里面吗_用Python写一个程序,解密游戏内抽奖的秘密
- lucene 索引中文档的属性建立与不建立带来的影响总结
- html 图片的缩略图,纯CSS制作缩略图片
- 非线性光纤光学_片上光学频率梳:可产生光子微波,应用于卫星通信和5G网络!...
- 分布式系统与海量数据处理
- iphone怎么找本地音乐_为什么iPhone会发出蜂鸣声或振动? 这是找出方法
- Python - 爬虫 - 调用高德API案例
- 2021华为杯数学建模D题完整思路
- Android禁用系统更新
- 【线性代数】1.2矩阵的行列式与克莱姆法则
- 机器学习:k近邻算法(KNN)介绍
- 查询python答案的app_2020知到APP大数据分析的python基础最新免费答案查询
- 用计算机找终身伴侣,五个经典问题决定你是否找到终身伴侣
- 详解DBSCAN聚类
- QVGA、WVGA、VGA、WQVGA、SQVGA等几种手机分辨率扫盲
- Redux源码分析--Enhancer
- 思迈特软件Smartbi:大数据分析平台的搭建和BI平台的区别