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 explicit Constructor in C++ Give an example?
Filed under: C++ Interview Questions, Infosys Interview Questions
The explicit keyword in C++ is used to declare explicit constructors. Explicit constructors are simply constructors that cannot take part in an implicit conversion. Consider the following example
CODE 1 will work
#include<iostream.h>
class A
{
int data;
public:
A(int a):data(a)
{
cout<<"A::Construcor...\n";
cout<<"value of data :="<<data<<endl;
};
};
int main()
{
A a1 = 37;
return (0);
}
./a.out
A::Construcor...
value of data :=37
CODE 2 will not work
#include<iostream.h>
class A
{
int data;
public:
explicit A(int a):data(a)
{
cout<<"A::Construcor...\n";
cout<<"value of data :="<<data<<endl;
};
};
int main()
{
A a1 = 37;
return (0);
}
We will get the below Compiler Error:
---------------------------------------
explicit1.cc: In function `int main()':
explicit1.cc:18: error: conversion from `int' to non-scalar type `A' requested
Another Example of explicit contructor
CODE 1 will work:
#include <iostream>
using std::cout;
using std::endl;
class complexNumbers {
double real, img;
public:
complexNumbers() : real(0), img(0) { }
complexNumbers(const complexNumbers& c) { real = c.real; img = c.img; }
complexNumbers( double r, double i = 0.0) { real = r; img = i; }
friend void display(complexNumbers cx);
};
void display(complexNumbers cx){
cout<<"Real Part: "<<cx.real<<" Imag Part: "<<cx.img<<endl;
}
int main() {
complexNumbers one(1);
display(one);
display(300);
return 0;
}
CODE 2 will not work:
#include <iostream>
using std::cout;
using std::endl;
class complexNumbers {
double real, img;
public:
complexNumbers() : real(0), img(0) { }
complexNumbers(const complexNumbers& c) { real = c.real; img = c.img; }
explicit complexNumbers( double r, double i = 0.0) { real = r; img = i; }
friend void display(complexNumbers cx);
};
void display(complexNumbers cx){
cout<<"Real Part: "<<cx.real<<" Imag Part: "<<cx.img<<endl;
}
int main() {
complexNumbers one(1);
display(one);
complexNumbers two =2;
display(200);
return 0;
}
Consider the following statement:
explicit complexNumbers( double r, double i = 0.0) { real = r; img = i; } Here, we are forcing the compiler not to do an implicit conversion for this piece of code. Now, if the programmer puts a line containing an implicit conversation, the compiler returns back with an error:
bash-3.2$ g++ -g -o hello test2.cpp
test2.cpp: In function ‘int main()’:
test2.cpp:22: error: conversion from ‘int’ to non-scalar type ‘complexNumbers’ requested
test2.cpp:23: error: conversion from ‘int’ to non-scalar type ‘complexNumbers’ requested
So instead of using display(200) C++ Programmmer will have to use display(complexNumbers(200))
How will you recognize the magnet & magnetic material & non-magnetic material?
Drag one piece of material over another. There is no attractive force in the middle portion of the magnet. OR
Get a piece of thread and tie up with the one bar and check for poles. If it iron bar then it moves freely and if it is magnetic bar then it fix in one direction according to poles.
Cow Puzzles
A man sold two cows for Rs. 210 at a total profit of 5 %. He sold one cow at a loss of 10% and another at a profit of 10%. What is the price of each cow?
