Using the Namespace Keyword to Create a Namespace in C++ - C++ Statement

C++ examples for Statement:namespace

Description

Using the Namespace Keyword to Create a Namespace in C++

Demo Code

#include <iostream>

using namespace std;

namespace Work//from w  ww. j  ava  2 s  .  c o  m
{
    int myVal;

    class Info
    {
    public:
        string CompanyName;
        string Position;
    };

    void DoStuff()
    {
        cout << "Doing some work!" << endl;
    }
}

namespace Play
{
    int myVal;

    class Info
    {
    public:
        string FullName;
        string Hobby;
    };

    void DoStuff()
    {
        cout << "Having fun!" << endl;
    }
}

int main()
{
    // Work stuff
    Work::myVal = 7;
    Work::Info WorkInformation;
    WorkInformation.CompanyName = "Spaceley Sprockets";
    WorkInformation.Position = "Worker";
    Work::DoStuff();

    // Play stuff
    Play::myVal = 13;
    Play::Info PlayInformation;
    PlayInformation.FullName = "g Jetson";
    PlayInformation.Hobby = "Playing with the dog";
    Play::DoStuff();

    return 0;
}

Result


Related Tutorials