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;
}
相关推荐
埃菲尔铁塔_CV算法6 分钟前
基于 TOF 图像高频信息恢复 RGB 图像的原理、应用与实现
人工智能·深度学习·数码相机·算法·目标检测·计算机视觉
N_NAN_N22 分钟前
类图+案例+代码详解:软件设计模式----原型模式
java·设计模式·原型模式
佛祖保佑永不宕机26 分钟前
maven引入本地jar包
java·maven·jar
默默coding的程序猿1 小时前
3.前端和后端参数不一致,后端接不到数据的解决方案
java·前端·spring·ssm·springboot·idea·springcloud
虾球xz1 小时前
CppCon 2018 学习:EFFECTIVE REPLACEMENT OF DYNAMIC POLYMORPHISM WITH std::variant
开发语言·c++·学习
在未来等你1 小时前
JVM调优实战 Day 15:云原生环境下的JVM配置
java·jvm·性能优化·虚拟机·调优
NAGNIP1 小时前
一文搞懂FlashAttention怎么提升速度的?
人工智能·算法
funnycoffee1231 小时前
Huawei 6730 Switch software upgrade example版本升级
java·前端·华为
Java初学者小白1 小时前
秋招Day15 - Redis - 缓存设计
java·数据库·redis·缓存
缘来是庄1 小时前
设计模式之组合模式
java·设计模式·组合模式