C++ Templating and Namespaces

Key Word(s): C++, Polymorphism, Namespaces




Key: :large_orange_diamond: - Code Example :large_blue_diamond: - Code Exercise :red_circle: - Code Warning
Previous: C++ Polymorphism


Templating


Function Template

The general form of a template function definition is as follows:

template <class T> ret_type func_name(parameter_list) {
   // body of function
} 
template <typename T> ret_type func_name(parameter_list) {
   // body of function
} 

Class Template

template <class type> class class_name {
   // body of class
}

Function Template Example

:large_orange_diamond: Function Template

// function template
#include <iostream>
using namespace std;

template <class T>
T getMax(T a, T b) {
  T result;
  result = (a > b) ? a : b;
  return (result);
}

:large_orange_diamond: Function Template Main

int main () {
  int i = 5, j = 6, k;
  long l = 10, m = 5, n;

  k = getMax<int>(i,j);
  n = getMax<long>(l,m);

  cout << "Int Max: " << k << endl;
  cout << "Long Max: " << n << endl;
  return 0;
}

:large_orange_diamond: Function Template Output

Int Max: 6
Long Max: 10


Class Template Example

:large_orange_diamond: Class Template

template <class T>
class mypair {
  private:
    T values[2];
  public:
    mypair(T first, T second){
      values[0] = first;
      values[1] = second;
    }
};

:large_orange_diamond: Class Template Usage

// int class
mypair<int> myobject(115, 36);

// double class 
mypair<double> myfloats (3.0, 2.18); 

:large_orange_diamond: Class Template Example

// class templates
#include <iostream>
using namespace std;

template <class T>
class mypair {
  private:
    T a, b;
  public:
    // contructor
    mypair(T first, T second){
      a = first;
      b = second;
    }

    // class method declaration
    T getMax();
};

// class method implementation
template <class T>
T mypair<T>::getMax(){
  T retval;
  retval = (a > b) ? a : b;
  return retval;
}

int main () {
  mypair<int> myobject(100, 75);
  cout << myobject.getMax() << endl;
  return 0;
}


Namespaces

Namespace Format:

namespace identifier {
  //entities
}

Scope Operator ::

Recall the scope operator :::

Example:

myNameSpace::a 
myNameSpace::b

Namespace Examples

:large_orange_diamond: Namespace Example

// namespaces
#include <iostream>
using namespace std;

namespace first {
  int var = 5;
}

namespace second {
  double var = 3.1416;
}

int main () {
  cout << first::var << endl;
  cout << second::var << endl;
  return 0;
}


The using Keyword

:large_orange_diamond: using Example 1

#include <iostream>
using namespace std;

namespace first {
  int x = 5;
  int y = 10;
}

namespace second {
  double x = 3.1416;
  double y = 2.7183;
}

int main(){
  using first::x;
  using second::y;

  cout << x << endl;
  cout << y << endl;

  cout << first::y << endl;
  cout << second::x << endl;
  return 0;
}

:large_orange_diamond: using Example 2

#include <iostream>
using namespace std;

namespace first {
  int x = 5;
  int y = 10;
}

namespace second {
  double x = 3.1416;
  double y = 2.7183;
}

int main(){
  using namespace first;

  cout << x << endl;
  cout << y << endl;

  cout << second::x << endl;
  cout << second::y << endl;
  return 0;
}


C++ Standard Template Library (STL)

The C++ STL is a powerful set of C++ template classes to provide general-purpose classes and functions with templates that implement many popular and commonly used algorithms and data structures like vectors, lists, queues, and stacks.

:large_orange_diamond: STL Vector Example

#include <iostream>
#include <vector>
using namespace std;

int main(){
   // create a vector to store int
   vector<int> vec; 
   int i;

   // display the original size of vec
   cout << "vector size = " << vec.size() << endl;

   // push 5 values into the vector
   for (i = 0; i < 5; i++) {
      vec.push_back(i);
   }

   // display extended size of vec
   cout << "extended vector size = " << vec.size() << endl;

   // access 5 values from the vector
   for (i = 0; i < 5; i++) {
      cout << "value of vec [" << i << "] = " << vec[i] << endl;
   }

   // use iterator to access the values
   vector<int>::iterator v = vec.begin();
   while (v != vec.end()) {
      cout << "value of v = " << *v << endl;
      v++;
   }

   return 0;
}

:large_orange_diamond: STL Vector Example Output

vector size = 0
extended vector size = 5
value of vec [0] = 0
value of vec [1] = 1
value of vec [2] = 2
value of vec [3] = 3
value of vec [4] = 4
value of v = 0
value of v = 1
value of v = 2
value of v = 3
value of v = 4

Learn about the Standard Template Library: C++ STL Tutorial