Namespace and Templates in C++
Namespace:-
- There is a team development in C++ for creating a single application.
- Due to this team development different team members may use a same identifier for the function or any data types.
- Due to this same name compiler generates ambiguity error.
- To remove this error we have to use the concept ‘namespace’.
- ‘namespace’ is a work area or a declarative region that attaches an additional identifier to any names declared inside that appropriate namespace.
- In C++, there is default namespace as std and we are going to provide user defined namespaces under this global namespace.
- We cannot create a local namespace.
- While creating a namespace we have to provide name of that namespace.
- But in C++ we can also provide un-named namespace.
- Un-named namespace is used for global static definition.
- Un-named namespace is only unique inside a current file.
- When we write any global definition that can be easily accessible from outside the file.
- When we declare that definition inside a named namespace it cannot be accessible outside the current file.
- This is valid, because, when we want to access that global member from a named namespace we can use a scope resolution (::) operator.
- But un-named namespace does not have any name means we cannot use scope resolution (::) operator.
e.g.
namespace demo1
{
void fun()
{
cout<<”fun of demo1 namespace.”;
}
}
namespace demo2
{
void fun()
{
cout<<”fun of demo2 namespace.”;
}
}
int main()
{
using namespace demo1; // we can write it anywhere
fun(); // fun of demo1 namespace.
demo1::fun(); // fun of demo2 namespace.
using namespace demo2;
fun(); // fun of demo2 namespace.
demo1::fun(); // fun of demo1 namespace.
return 0;
}
Example of un-named namespace:-
namespace
{
int global;
class demo
{
// code
};
}
- In this case, ‘global’ variable cannot be accessible outside current file by ‘extern’ keyword, because, it is inside the un-named namespace and outside a file it does not know name of that namespace.
- Another important point about namespace is, namespace must be a global namespace, and we cannot create a local namespace, local means inside the class or inside a function.
Generic programming:-
- When we write a function it works for a specific data type.
- If we want to pass some other data type to that function then that function fails to execute.
- If we have 10 types of data types then we have to provide 10 different definitions for same function.
- To avoid this redundancy C++ provides generic programming concept by using templates.
- In C++, we can provide templates in 2 formats,
1) Class templates
2) Function templates
- Generally templates are used, if some algorithms work correctly for different data types.
e.g.
Searching and sorting algorithms,
Swapping functions
Finding a maximum element from a given range.
Function templates:-
template <class T> void
swap(T &a,T &b)
{
T temp;
temp=a;
a=b;
b=temp;
}
int main()
{
int x=10,y=20;
swap(10,20);
char a=’l’,b=’m’;
swap(a,b);
}
- Function template means writing a function which works for different data types.
- In this example, swap function is considered as a function template.
- When we call this function it is considered as instantiating the template.
- When we call this function template for n different data types then compiler generates n different object codes for that function.
- Means from programmer point of view it is a generic programming but internal source code is generated for different data types.
- E.g. for function templates are as follows,
1) Swapping of 2 elements
2) Arithmetic calculations of 2 elements
3) Finding maximum elements
4) Searching particular element
Class template:-
template <class T> class stack
{
private:
int size;
T *p;
int top;
public:
stack();
void push(T);
T pop();
~stack();
};
template <class T> void
stack<T>::push(T val)
{
assert(top<size)
p[++top]=val;
}
template<class T> T
stack<T>::pop()
{
assert(top>-1);
return(p(top–));
}
template <class T> stack
<T>::stack()
{
size=10;
p=new T[size];
top=-1;
}
template <class T> stack
<T>::~stack()
{
if(p)
delete[] p;
}
void main()
{
stack <int> st1;
st1.push(10);
st1.push(20);
cout<<st1.pop();
stack <char> st2;
st2.push(‘A’);
st2.push(‘B’);
cout<<st2.pop();
}
For more reading about technology news in singapore and seo to online marketing do view more about other pages.