What is vptr and vtable ?
Filed under: C++ Interview Questions, Infosys Interview Questions
The virtual table is a lookup table of functions used to resolve function calls in a dynamic/late binding manner. The virtual table sometimes goes by other names, such as “vtable”, “virtual function table”, “virtual method table”, or “dispatch table”.
How Virtual Table and Virtual Function Works
class Base
{
public:
virtual void function1() {};
virtual void function2() {};
};
class D1: public Base
{
public:
virtual void function1() {};
};
class D2: public Base
{
public:
virtual void function2() {};
};
Compiler adds a hidden vPtr member to the class, and generates one unique vtable for the class.
At compilation time, when compiler sees the definition of a class with virtual methods, it will build a virtual table (vtable) for the class, which is an array of function pointers to the implementations of all the virtual methods, and add a hidden data member vPtr to the class definition as the FIRST data member.

What is a diamond problem and virtual inheritance
When one class inherits from more than one classes, that is called multiple inheritance. Among multiple inheritance, there is a special case called diamond inheritance. One example is: class ostream and istream both inherits from class ios, and class iostream inherits from both istream and ostream.
In polymorphism, when we let a base-class pointer point to a derived-class object, that pointer actually points to the base-class object within the derived-class object. In diamond inheritance, however, because the derived class contains two duplicated base-class objects, compiler doesn’t know which part the pointer should point to. Therefore it will prompt an error message.
Example:
class Base {};
class Derived1 : public Base {};
class Derived2 : public Base {};
class Multi : public Derived1, public Derived2 {};
The following statement is illegal:
Base * ptr = new Multi;
But the following statements are legal:
Derived1 * ptr1 = new Multi;
Derived2 * ptr2 = new Multi;
because the compiler can locate the Derived1 or Derived2 part of object in Multi object.
To solve this problem, use virtual inheritance, which only allows one sub-object:
class Base {};
class Derived1 : virtual public Base {};
class Derived2 : virtual public Base {};
class Multi : public Derived1, public Derived2 {};
int main()
{ Base * ptr = new Multi; }
What is Map in C++ STL? Different operation on Map with example
Maps are a kind of associative containers that stores elements formed by the combination of a key value and a mapped value.
1 Unique key values: no two elements in the map have keys that compare equal to each other. For a similar associative container allowing for multiple elements with equivalent keys, see multimap.
2 Each element is composed of a key and a mapped value. For a simpler associative container where the element value itself is its key, see set.
3 Elements follow a strict weak ordering at all times. Unordered associative arrays, like unordered_map, are available in implementations following TR1.
template < class Key, class T, class Compare = less
class Allocator = allocator
Where the template parameters have the following meanings:
1 Key: Type of the key values. Each element in a map is uniquely identified by its key value.
2 T: Type of the mapped value. Each element in a map is used to store some data as its mapped value.
3 Compare: Comparison class: A class that takes two arguments of the key type and returns a bool. The expression comp(a,b), where comp is an object of this comparison class and a and b are key values, shall return true if a is to be placed at an earlier position than b in a strict weak ordering operation. This can either be a class implementing a function call operator or a pointer to a function (see constructor for an example). This defaults to less
4 Allocator: Type of the allocator object used to define the storage allocation model. By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.
Operation on Map
1. insert Insert element (public member function)
// map::insert
#include <iostream>
#include <map>
using namespace std;
int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;
pair<map<char,int>::iterator,bool> ret;
// first insert function version (single parameter):
mymap.insert ( pair<char,int>('a',100) );
mymap.insert ( pair<char,int>('z',200) );
ret=mymap.insert (pair<char,int>('z',500) );
if (ret.second==false)
{
cout << "element 'z' already existed";
cout << " with a value of " << ret.first->second << endl;
}
// second insert function version (with hint position):
it=mymap.begin();
mymap.insert (it, pair<char,int>('b',300)); // max efficiency inserting
mymap.insert (it, pair<char,int>('c',400)); // no max efficiency inserting
// third insert function version (range insertion):
map<char,int> anothermap;
anothermap.insert(mymap.begin(),mymap.find('c'));
// showing contents:
cout << "mymap contains:\n";
for ( it=mymap.begin() ; it != mymap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;
cout << "anothermap contains:\n";
for ( it=anothermap.begin() ; it != anothermap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;
return 0;
}
Output:
element 'z' already existed with a value of 200
mymap contains:
a => 100
b => 300
c => 400
z => 200
anothermap contains:
a => 100
b => 300
2. erase Erase elements (public member function)
mymap.erase (it); // erasing by iterator
mymap.erase ('c'); // erasing by key
mymap.erase ( it, mymap.end() ); // erasing by range
3. swap Swap content (public member function)
foo.swap(bar);
4. clear Clear content (public member function)
All the elements in the container are dropped: their destructors are called, and then they are removed from the container, leaving it with a size of 0.
What is set in C++ STL ? List the various operation on set with examples
Set: Sets are a kind of associative containers that stores unique elements, and in which the elements themselves are the keys.
Therefore, the main characteristics of set as an associative container are:
1. Unique element values: no two elements in the set can compare equal to each other. For a similar associative container allowing for multiple equivalent elements, see multiset.
2. The element value is the key itself. For a similar associative container where elements are accessed using a key, but map to a value different than this key, see map.
3. Elements follow a strict weak ordering at all times. Unordered associative arrays, like unordered_set, are available in implementations following TR1.
Creation of set take three parameter
template < class Key, class Compare = less
class Allocator = allocator
Where the template parameters have the following meanings:
1 Key: Key type: type of the elements contained in the container. Each elements in a set is also its key.
2 Compare: Comparison class: A class that takes two arguments of the same type as the container elements and returns a bool. The expression comp(a,b), where comp is an object of this comparison class and a and b are elements of the container, shall return true if a is to be placed at an earlier position than b in a strict weak ordering operation. This can either be a class implementing a function call operator or a pointer to a function (see constructor for an example). This defaults to less
3 Allocator: Type of the allocator object used to define the storage allocation model. By default, the allocator class template for type Key is used, which defines the simplest memory allocation model and is value-independent
Operations:
1. insert:: Insert element (public member function) / set::insert #include#include using namespace std; int main () { set myset; set ::iterator it; pair ::iterator,bool> ret; // set some initial values: for (int i=1; i<=5; i++) myset.insert(i*10); // set: 10 20 30 40 50 ret = myset.insert(20); // no new element inserted if (ret.second==false) it=ret.first; // "it" now points to element 20 myset.insert (it,25); // max efficiency inserting myset.insert (it,24); // max efficiency inserting myset.insert (it,26); // no max efficiency inserting int myints[]= {5,10,15}; // 10 already in set, not inserted myset.insert (myints,myints+3); cout << "myset contains:"; for (it=myset.begin(); it!=myset.end(); it++) cout << " " << *it; cout << endl; return 0; } Output: myset contains: 5 10 15 20 24 25 26 30 40 50 2. erase:: Erase elements (public member function) myset.erase (it); myset.erase (40); it=myset.find (60); myset.erase ( it, myset.end() ); 3. swap:: Swap content (public member function) int myints[]={12,75,10,32,20,25}; set first (myints,myints+3); // 10,12,75 set second (myints+3,myints+6); // 20,25,32 set ::iterator it; first.swap(second); 4. clear All the elements in the set container are dropped: their destructors are called, and then they are removed from the container, leaving it with a size of 0. myset.clear();
