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;
}

相关推荐

  1. C++ .h文件调用

    2024-02-09 14:22:01       52 阅读
  2. kotlin调用文件

    2024-02-09 14:22:01       27 阅读
  3. C调用C++中

    2024-02-09 14:22:01       33 阅读
  4. 2312d,d语言调用C++

    2024-02-09 14:22:01       64 阅读
  5. springboot一个接口多个实现调用方式

    2024-02-09 14:22:01       59 阅读
  6. Flask实现异步调用sqlalchemy模型

    2024-02-09 14:22:01       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-09 14:22:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-09 14:22:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-09 14:22:01       82 阅读
  4. Python语言-面向对象

    2024-02-09 14:22:01       91 阅读

热门阅读

  1. 机器学习原理到Python代码实现之PolynomialRegression

    2024-02-09 14:22:01       42 阅读
  2. List 差集

    2024-02-09 14:22:01       39 阅读
  3. 侵入式智能指针和非侵入式智能指针

    2024-02-09 14:22:01       44 阅读
  4. 动态规划C语言

    2024-02-09 14:22:01       39 阅读
  5. Jetpack Room使用

    2024-02-09 14:22:01       48 阅读
  6. 开源软件:引领技术创新与商业模式转型

    2024-02-09 14:22:01       50 阅读
  7. 2024年GPT如何发展?

    2024-02-09 14:22:01       50 阅读
  8. 开源软件的未来发展趋势与应对新挑战和机遇

    2024-02-09 14:22:01       59 阅读
  9. 问题 | 开源软件的影响力

    2024-02-09 14:22:01       42 阅读
  10. Git学习

    Git学习

    2024-02-09 14:22:01      50 阅读