Tag Archives: iterator

Why we are so confused on c++ virtual function, iterator, and a functor

Virtual
The base type pointer, calls the method but the proper virtual function at runtime is called (dynamically).
Declare a function as virtual in base class, then override it in each derived class, just like redefining, new function must have same signature.
Dynamic binding chooses proper function to call of the virtual address table at runtime, but it only occurs off pointer handles.
A virtual-table is created for every class having one or more virtual functions. The
virtual table is usually implemented as an array of function pointers where each function pointer corresponds to the various implementations of the virtual function by the derived classes.

When an object of this class is created, it contains a virtual-pointer (v-pointer) which points to the start of v-table. Whenever a virtual function is invoked on this object, the runtime system resolves it by following the v-pointer to the v-table to invoke the appropriate function.
Virtual functions are used to extend polymorphic behaviour. VPT:- every class has its own virtual function pointer table. It consist of its virtual functions in an organized way which helps compiler to determine which function to call at run time.

What is pure virtual function? or what is abstract class?
When you define only function prototype in a base class without implementation and do the complete implementation in derived class. This base class is called abstract class and client won’t able to instantiate an object using this base class.
You can make a pure virtual function or abstract class this way..
class Boo
{
void foo() = 0;
}
Boo MyBoo; // compilation error

How virtual functions are implemented C++?

What is Virtual Destructor?
Using virtual destructors, you can destroy objects without knowing their type – the correct destructor for the object is invoked using the virtual function mechanism. Note that destructors can also be declared as pure virtual functions for abstract classes.
Namespaces
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.
A using declaration makes it possible to use a name from a namespace without the scope operator.

Iterators:
Iterator 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.
Functors
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.

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 iterator and arrays

Iterator
3.Question: What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator.
Answer: .An internal iterator is implemented with member functions of the class that has items to step through. .An external iterator is implemented as a separate class that can be “attach” to the object that has items to step through. .An external iterator has the advantage that many difference iterators can be active simultaneously on the same object.

Array:
Write a function to add an array of numbers.
what kind of numbers, real/integer

int add_Num(int arr[], int n ){

int sum = 0;
int j = 0;
while (j < n ){ sum+=arr[j]; j++; } return sum; } merging two arrays (second one being larger than the elements in it) into one without using 3rd array. what all errors is possible if u write past the array bounds? memory corruption, call stack corruption. In unix, scope is limited to these 2 inside a process. In RTOS, the corruption can span across multiple tasks/processes you inadvertently overwrite some portion of memory that some pointer or a variable is pointing to when you write past array bounds 4.Question: Why are arrays usually processed with for loop? Answer: The real power of arrays comes from their facility of using an index variable to traverse the array, accessing each element with the same expression a[i]. All the is needed to make this work is a iterated statement in which the variable i serves as a counter, incrementing from 0 to a.length -1. That is exactly what a loop does.

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!