当前位置: 首页 > news >正文

C++ .h文件类的调用

demo1只有类的情况下调用

下面写一个util.h 文件里面

// 定义宏防止编译器重复编译
#ifndef TEST_H
#define TEST_H
class Test{
public:void sum(int a, int b);int num(int a, int b);bool number();};
#endif // TEST_H

调用的时候首先要引入这个头文件 #include "util.h"

cpp 里面实现

#include <iostream>
#include <string>
#include "util.h"
using namespace std;// 实现Test类的sum成员函数
void Test::sum(int a,int b){cout << "The sum of " << a << " and " << b << " is: " << a + b << std::endl;
}
// 实现Test类的num成员函数
int Test::num(int a, int b) {return a * b;
}
// 实现Test类的number成员函数
bool Test::number() {return true;
}int main() {int a =3;int b =6;Test test;test.sum(a,b);int result = test.num(a,b);cout<< "num result" << result <<endl;bool isNumber = test.number();cout<< "number result" << isNumber <<endl;return 0;
}

demo2 有namespace的情况下调用

util.h 文件

// 定义宏防止编译器重复编译
#ifndef TEST_H
#define TEST_H
namespace common::comm::com {
class Test{
public:void sum(int a, int b);int num(int a, int b);bool number();};
}
#endif  

cpp 里面实现,这里不使用using namespace

#include <iostream>
#include <string>
#include "util.h"
using namespace std;// 实现Test类的sum成员函数
void common::comm::com::Test::sum(int a,int b){cout << "The sum of " << a << " and " << b << " is: " << a + b << std::endl;
}
// 实现Test类的num成员函数
int common::comm::com::Test::num(int a, int b) {return a * b;
}
// 实现Test类的number成员函数
bool common::comm::com::Test::number() {return true;
}int main() {int a =3;int b =6;common::comm::com::Test test;test.sum(a,b);int result = test.num(a,b);cout<< "num result" << result <<endl;bool isNumber = test.number();cout<< "number result" << isNumber <<endl;return 0;
}

使用using namespace

namespace common::comm::com {
// 实现Test类的sum成员函数
void Test::sum(int a,int b){cout << "The sum of " << a << " and " << b << " is: " << a + b << std::endl;
}
// 实现Test类的num成员函数
int Test::num(int a, int b) {return a * b;
}
// 实现Test类的number成员函数
bool Test::number() {return true;
}
}
int main() {int a =3;int b =6;using namespace common::comm::com;Test test;test.sum(a,b);int result = test.num(a,b);cout<< "num result" << result <<endl;bool isNumber = test.number();cout<< "number result" << isNumber <<endl;return 0;
}

实际用不用,根据个人习惯即可,不使用using namespace在每次调用时都写出完整的命名空间路径

把util.h 文件修改一层一层的

// 定义宏防止编译器重复编译
#ifndef TEST_H
#define TEST_H
namespace common{namespace comm{namespace com{class Test{public:void sum(int a, int b);int num(int a, int b);bool number();};}}
}
#endif  

实现里面的方法效果也是一样的

#include <iostream>
#include <string>
#include "util.h"
using namespace std;namespace common::comm::com {
// 实现Test类的sum成员函数
void Test::sum(int a,int b){cout << "The sum of " << a << " and " << b << " is: " << a + b << std::endl;
}
// 实现Test类的num成员函数
int Test::num(int a, int b) {return a * b;
}
// 实现Test类的number成员函数
bool Test::number() {return true;
}
}
int main() {int a =3;int b =6;using namespace common::comm::com;Test test;test.sum(a,b);int result = test.num(a,b);cout<< "num result" << result <<endl;bool isNumber = test.number();cout<< "number result" << isNumber <<endl;return 0;
}

或者

#include <iostream>
#include <string>
#include "util.h"
using namespace std;namespace common {namespace comm {namespace com {// 实现Test类的sum成员函数void Test::sum(int a, int b) {std::cout << "The sum of " << a << " and " << b << " is: " << a + b << std::endl;}int Test::num(int a, int b) {return a * b;}bool Test::number() {return true; // 示例返回true}} // 结束命名空间 com} // 结束命名空间 comm
} // 结束命名空间 common
int main() {int a =3;int b =6;using namespace common::comm::com;Test test;test.sum(a,b);int result = test.num(a,b);cout<< "num result" << result <<endl;bool isNumber = test.number();cout<< "number result" << isNumber <<endl;return 0;
}

http://www.lryc.cn/news/297006.html

相关文章:

  • C语言:分支与循环
  • 【linux系统体验】-archlinux折腾日记
  • 常用数字处理格式校验
  • 2024.1.26力扣每日一题——边权重均等查询
  • C语言操作符超详细总结
  • 【Java八股面试系列】JVM-内存区域
  • 计划任务功能优化,应用商店上架软件超过100款,1Panel开源面板v1.9.6发布
  • 蓝桥杯(Web大学组)2023省赛真题3:收集帛书碎片
  • 使用QT编写一个简单QQ登录界面
  • TryHackMe-Net Sec Challenge练习
  • 面试 JavaScript 框架八股文十问十答第五期
  • [职场] 如何通过运营面试_1 #笔记#媒体#经验分享
  • CTFshow web(命令执行 41-44)
  • XML介绍和基本语法
  • Android:Android Studio安装及环境配置
  • 力扣刷题之旅:进阶篇(三)
  • 代码随想录 Leetcode55. 跳跃游戏
  • Go Context -- 管理请求的上下文信息
  • springboot170图书电子商务网站的设计与实现
  • 设计模式(结构型模式)适配器模式
  • 计算机网络基本知识(二)
  • 158基于matlab的用于分析弧齿锥齿轮啮合轨迹的程序
  • C#中的浅度和深度复制(C#如何复制一个对象)
  • 2.6日学习打卡----初学RabbitMQ(一)
  • Rust语言之集合
  • 有道论文翻译接口,python版和lua版
  • java大数据hadoop2.9.2 Flume安装操作
  • 环境配置:Ubuntu18.04 ROS Melodic安装
  • 2024.2.7-8 寒假训练记录(21)
  • C++ pair 的使用