Create your own namespace to include function - C++ Statement

C++ examples for Statement:namespace

Description

Create your own namespace to include function

Demo Code

#include <string>
#include <iostream>

using std::string;

int print_count = 0;

namespace print1 {
  // Function prototype
  void print(const string& s);
}

namespace print2 {
  // Function prototype
  void print(const string& s);
}

void print1::print(const string& s) {
  std::cout << s << " (Namespace name is print1.) " << std::endl;
}

void print2::print(const string& s) {
  std::cout << s << " (Namespace name is print2.) " << std::endl;
}


void print_that(const string& s)
{
  print2::print(s);//from   w ww  . j  a va  2 s  . c o m
}

void print_this(const string& s)
{
  print1::print(s);
}


int main(){
  print_this("This");
  print_that("That");

  std::cout << "The print() function has been called a total of "      // ADDED
            << print_count << " times. " << std::endl;                 // ADDED
}

Result


Related Tutorials