Q:

How to use namespace in C++?

0

How to use namespace in C++?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

Let us see a namespace “Test”,

namespace Test
{
    class TestObject
    {
    public:
        void DoSomething() {}
    };
    void Func(TestObject) {}
}

Now let see three ways to access the members of the namespace “Test”.

1, Use the fully qualified name:

Test::TestObject test;
test.DoSomething();
Test::Func(test);

2. Use a using-declaration to bring one identifier into scope:

using Test::TestObject;
TestObject test;
test.DoSomething();

3. Use a using directive to bring everything in the namespace into scope:

using namespace Test;
TestObject test;
test.DoSomething();
Func(test);

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Interview Questions and Answers(2022)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What is a member function in C++?... >>
<< What is a namespace in c++?...