Tag Archives: namespace

What are namespaces, functors, and iterator classes in C++?

What are namespaces, functors, and iterator classes in C++?
Namespaces reduced name collisions where an application may have various libraries of the same name. The using declaration makes it possible for a namespace without using a scope operator.
There are five types of iterator classes which input iterators, output iterators, forward iterators, and bidirectional iterators. These are usually stored in stored in lists and queues according to some ordering relation as defined in a binary tree.
Functors are a function pointer which holds the address of some pointer.
#include
using namespace std;

class Matcher
{
int target;
public:
Matcher(int m) : target(m) {}
int operator()(int x) { return x == target;}
};

int main() {
Matcher Is5(5);
int n=6;

if (Is5(n)) { // same as if (n == 5) return 0;
cout << "n and Is5 are equal" << endl; } else { cout << "not equal"<

HOW DO YOU START A PROFITABLE TRADING BUSINESS? Read more NOW >>>

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

c++ interview questions on functors, namespace, and using

c++ interview questions on functors, namespace, and using

Namespace:

What problem does the namespace feature solve?
Multiple providers of libraries might use common global identifiers causing a name collision when an application tries to link with two or more such libraries. The namespace feature surrounds a library’s external declarations with a unique namespace that eliminates the potential for those collisions.
namespace [identifier] { namespace-body }
A namespace declaration identifies and assigns a name to a declarative region.
The identifier in a namespace declaration must be unique in the declarative region in which it is used. The identifier is the name of the namespace and is used to reference its members.
What is the use of ‘using’ declaration?
A using declaration makes it possible to use a name from a namespace without the scope operator.
What is an Iterator class?
A class that is used to traverse through the objects maintained by a container class. There are five categories of iterators: input iterators, output iterators, forward iterators, bidirectional iterators, random access. An iterator is an entity that gives access to the contents of a container object without violating encapsulation constraints. Access to the contents is granted on a one-at-a-time basis in order. The order can be storage order (as in lists and queues) or some arbitrary order (as in array indices) or according to some ordering relation (as in an ordered binary tree). The iterator is a construct, which provides an interface that, when called, yields either the next element in the container, or some value denoting the fact that there are no more elements to examine. Iterators hide the details of access to and update of the elements of a container class. Something like a pointer.

Functor:
What is a functor?
Functor means function pointers. The pointer variable that holds the address of some function.
Syntax for declaring a functor is:

return_type (* nameofFunctor)(parameter_type1, 2, 3, ……);

It means nameofFunctor is a functor that can point to functions taking parameter_type1, 2, 3, ….. and returning return_type

Functors are functions with a state. In C++ you can realize them as a class with one or more private members to store the state and with an overloaded operator () to execute the function.

HOW DO YOU START A PROFITABLE TRADING BUSINESS? Read more NOW >>>

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!