本文介绍: 实际用不用,根据个人习惯即可,不使用using namespace在每次调用时都写出完整的命名空间路径。调用的时候首先要引入这个头文件 #include “util.h”cpp 里面实现,这里不使用using namespace。使用using namespace。把util.h 文件修改一层一层的。下面写一个util.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;
}

原文地址:https://blog.csdn.net/qq_33210042/article/details/136077369

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_68287.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注