Is there any difference between the library files 'iostream' and 'iostream.h' ?
Ans: 'iostream' contains a set of templatized I/O classes which support both narrow and wide
characters. 'iostream.h' on the other hand is limited to char exclusively. The C++ standard
specification of iostream's interface was changed in many aspects. As a result, the interfaces and
implementation of 'iostream' differ from 'iostream.h'. 'iostream' components are declared in std
namespace whereas 'iostream.h' components are declared in the global scope. Because of these
substantial differences, we cannot use the two libraries in one program. As a rule, use 'iostream'
in new code and stick to 'iostream.h' in legacy code that is incompatible with the new 'iostream'
library.
Why creating array of references is not possible?
Ans: The array name always refers or points to the zeroeth element. If array is of references then
the array name would point to the zeroeth element which happens to be a reference. Creating
pointer to a reference is not valid. So, creating array of references too is not possible.
How do I call a virtual function of a class using a pointer to a function ?
Ans :
#include <iostream.h>
class Cvirtual
{
public :
virtual float vfun( )
{
cout << "from vfun" << endl ;
return 7.03f ;
}
} ;
void main( )
{
Cvirtual obj ;
int **p = ( int ** ) &obj ;
float ( *pf1 ) ( ) ;
pf1 = ( float ( * ) ( ) ) **p ;
float f = ( *pf1 ) ( ) ;
cout << "return val = " << f << endl ;
}
In the above program class Cvirtual consists of a virtual function vfun( ). In variable p we have
stored the address of an object of class Cvirtual. While doing so, we have type casted the
address of obj to int **, because obj holds a hidden data member called vptr, which in turn holds
the address of virtual function vfun( ). In pf1, a pointer to a function, we are collecting the
address of the virtual function vfun( ). Thus the value returned by vfun( ) would then get collected
in f.
Can we apply delete on this pointer inside a member function?
Ans : Yes! If the member function of a class is called using a pointer to an object, which is
allocated dynamically, the object would get deleted. But if the member function were called using
the object, which is allocated statically, then a runtime error would occur. This is because we
cannot call delete on statically allocated objects. This is illustrated in the following example.
class sample
{
private :
int i;
public:
void fun()
{
delete this;
}
};
void main()
{
sample *s = new sample;
s -> fun( ) ; // no error
sample s1 ;
s1.fun() ; // would throw a runtime error
}
Can we have a reference to an array?
Ans: Yes, we can have a reference to an array.
int a[ ] = { 8, 2, 12, 9 } ;
int ( &r ) [ 4 ] = a ; // reference to an array
Here, r is a reference to an array of four elements. We can even print the elements of array with
the help of reference. This is shown in the following code segment:
for ( int i = 0 ; i < 4 ; i++ )
cout << r [i] << endl ;
How do I use operators .* and ->* in a program?
Ans: The following code snippet demonstrates the use of .* and ->* operators.
#include <iostream.h>
class sample
{
Public:
int i;
void fun()
{
cout << "fun" << endl ;
}
};
void ( sample::*pf )( ) = &sample::fun;
int sample::*pdm = &sample::i ;
void main( )
{
sample s ;
sample *p = new sample ;
( s .* pf )( ) ;
( p ->* pf )( ) ;
s .* pdm = 1 ;
p ->* pdm = 2 ;
cout << s .* pdm << endl ;
cout << p ->* pdm << endl ;
}
In the above program pf is a pointer to a function fun( ) of class sample, and pdm is a pointer to
a data member i of the same class sample. The object s of the class sample is created statically.
Next, p is a pointer to an object created dynamically. The using the operator .* and ->* the
member functions are called and also the public data member is accessed.
What happens when we add an int value to a user defined type of object?
Ans: Whenever an int value is added to an object of user defined type, the object would search
for an overloaded operator int(). This operator must be defined in such a way that it always
returns an int value. However, we need not specify the return type as on doing so the compiler
flashes an error.
#include <iostream.h>
class sample
{
int i ;
public :
sample ( )
{
i = 10 ;
}
operator int()
{
return this -> i ;
}
};
void main( )
{
sample s ;
int i ;
i = s + 10 ;
cout << i ;
}
In the above program on adding 10 to an object s, the value of i would become 20.
Top
Can we pass the this pointer to an overloaded operator new( ) function in a user defined
class?
Ans: No. The this pointer is never passed to the an overloaded operator new( ) function. This is
because the operator new( ) function gets called even before the constructor is called. Since an
object of the class gets created only through the constructor it is just not possible to pass the this
pointer to operator new( ). This is demonstrated in the following program.
#include <iostream.h>
#include <malloc.h>
class sample
{
private :
int i ;
public :
sample()
{
cout << endl <<"constructor called";
}
void * operator new (size_t s)
{
cout << "Reached new";
void *p = malloc (s);
return p;
}
};
void main()
{
sample *p = new sample;
delete p;
}
The output of the program is
Reached new
Reached constructor
From the output it is clear that the operator new( ) is called first and then the constructor of the
class is called.
Can we allocate memory dynamically for a reference?
Ans: No! It is not possible to allocate memory dynamically for a reference. This is because, when
we create a reference, it gets tied with some variable of its type. Now, if we try to allocate memory
dynamically for a reference, it is not possible to mention that to which variable the reference
would get tied.
When should I overload new operator on a global basis or a class basis?
Ans: We overload operator new in our program, when we want to initialize a data item or a class
object at the same place where it has been allocated memory. The following example shows how
to overload new operator on global basis.
#include <iostream.h>
#include <malloc.h>
void * operator new ( size_t s )
{
void *q = malloc ( s ) ;
return q ;
void main( )
{
int *p = new int ;
*p = 25 ;
cout << *p ;
When the operator new is overloaded on global basis it becomes impossible to initialize the data
members of a class as different classes may have different types of data members. The following
example shows how to overload new operator on class by class basis.
#include <iostream.h>
#include <malloc.h>
class sample
{
int i ;
public :
void* operator new ( size_t s, int ii )
{
sample *q = ( sample * ) malloc ( s ) ;
q -> i = ii ;
return q ;
};
class sample1
{
float f ;
public :
void* operator new ( size_t s, float ff )
{
sample1 *q = ( sample1 * ) malloc ( s ) ;
q -> f = ff ;
return q ;
};
void main( )
{
sample *s = new ( 7 ) sample ;
sample1 *s1 = new ( 5.6f ) sample1 ;
Overloading the operator new on class by class basis makes it possible to allocate memory for
an object and initialize its data members at the same place.
How would you define a pointer to a data member of the type pointer to pointer?
Ans: The following program demonstrates this...
#include <iostream.h>
class sample
{
public :
sample ( int **pp )
{
p = pp ;
int **p ;
};
int **sample::*ptr = &sample::p ;
void main( )
{
int i = 9 ;
int *pi = &i ;
sample s ( &pi ) ;
cout << ** ( s.*ptr ) ;
Here, ptr is the pointer to data member p of class sample, which in turn is a pointer pointing to
an int.
How do I write a code to catch multiple types of exceptions in one single catch block?
Ans: The following program demonstrates the use of a single catch block to catch multiple
exceptions.
#include <iostream.h>
class test
{
};
class sample
{
public :
void fun1( )
{
throw 99 ;
}
void fun2( )
{
throw 3.14f ;
}
void fun3( )
{
throw "error" ;
}
void fun4( )
{
throw test( ) ;
};
void main( )
{
try
{
sample s ;
s.fun4( ) ;
s.fun1( ) ;
s.fun2( ) ;
s.fun3( ) ;
}
catch ( ... )
{
cout << "strange" ;
Here, different types of exceptions are thrown by the member functions of the class sample.
While catching the exception instead of four different catch blocks we can as well define one
single catch block. Note the syntax for defining the catch block, where we have used three dots
(...) in the formal parameter list. This indicates that any thrown exception should get caught in the
same catch block. When the exception is thrown from the fun4( ) control reaches the catch
block, ignoring the rest of the calls.
Can we return an error value from the constructor of a class?
Ans: No. We cannot return any error value from the constructor, as the constructor doesn't have
any return type. However, by throwing an exception we can pass value to catch block. This is
shown in the following example:
#include <iostream.h>
class sample
{
public :
sample ( int i )
{
if ( i == 0 )
throw "error" ;
};
void main( )
{
try
{
sample s ( 0 ) ;
}
catch ( char * str )
{
cout << str ;
In this program, the statement throw "error" ; would throw an exception when an object s of the
class sample would get created. The string error would get collected by the catch block.
How do I define the member function of a template class, which has to be defined outside
the template class. The function receives an object of its own class as a parameter and
returns the value of the same type.
Ans: The following example shows how we can define such a function.
sample <T> sample<T>::fun ( sample s )
{
// code
Here, the first sample<T> indicates the return type of the function and the next sample<T> is
used for the scope of function.
Can we allocate memory dynamically for a reference?
Ans: No, it is not possible to allocate memory dynamically for a reference. A reference is
initialized at the time of creation. Trying to allocate memory dynamically for a reference creates a
problem in initializing it. Thus, the compiler does not allow us to dynamically allocate the memory
for references.
How name mangling can be prevented?
Ans: To avoid name mangling the function should be declared with an extern "C" attribute.
Functions declared as extern "C" are treated as C-style functions. Hence the compiler does not
mangle them. The following code snippet shows how to declare such a function.
#include <iostream.h>
extern "C" void display( )
{
cout << "See the effect of C in C++ " ;
void main( )
{
display( ) ;
Can we use this pointer in a class specific, operator overloading function for new
operator?
Ans: No! The this pointer is never passed to the overloaded operator new( ) member function
because this function gets called before the object is created. Hence there is no question of the
this pointer getting passed to operator new( ).
What is a const_cast?
Ans: The const_cast is used to convert a const to a non-const. This is shown in the following
program.
#include <iostream.h>
void main( )
{
const int a = 0 ;
int *ptr = ( int * ) &a ; // one way
ptr = const_cast <int *> ( &a ) ; // better way
Here, the address of the const variable a is assigned to the pointer to a non-const variable. The
const_cast is also used when we want to change the data members of a class inside the const
member functions. The following code snippet shows how to do this.
class sample
{
private :
int data ;
public :
void fun( ) const
{
( const_cast <sample *> ( this ) ) -> data = 70 ;
};
Using a smart pointer we can make an object appear like a pointer.
If a class overloads the operator -> then any object of that class can appear like a pointer when
the operator -> ( ) is called. The following program illustrates this.
#include <iostream.h>
class test
{
public :
void fun( )
{
cout << "fun of smart pointer" ;
};
class smartpointer
{
test t ;
public :
test* operator ->( )
{
return &t ;
};
void main( )
{
smartpointer sp ;
sp -> fun( ) ;
The beauty of overloading operator -> is that even though sp is an object we can make it work
like a pointer. The operator -> ( ) returns the address of the object of the type test. Using this
address of the test object the function fun( ) of the class test gets called. Thus even though
fun( ) is not a member of smartpointer class we can still call it using sp.
How to access the VTABLE using the this pointer?
Ans : The following program illustrates this.
#include <iostream.h>
class sample
{
public :
virtual void _cdecl fun ( int q )
{
cout << "fun " << q ;
void g( )
{
int *p = ( int * ) this ; // address of the object
p = ( int * ) *p ; // address of the VTABLE
p = ( int * ) *p ; // address of the 1st virtual function
void ( _cdecl *pfun ) ( sample * const, int ) ;
pfun = ( void ( _cdecl * ) ( sample * const, int ) ) p ;
( *pfun ) ( this, 25 ) ;
};
void main( )
{
sample s ;
s.g( ) ;
In this program fun( ) is a virtual function. Hence when an object of this class would be created
the class would get loaded in memory. Also the VTABLE of this class would be created and its
address would be stored in the first two bytes of the object. When the function g( ) is called from
main( ) using the object s, the this pointer in the g( ) function would get filled with the address of
s. We have typecast the address of the object (stored in this pointer) into an int pointer using the
statement int *p = ( int * ) this ; This is because we want to extract the first two bytes of the
object, since they contain the address of the VTABLE. To extract the address we have written the
statement, p = ( int * ) *p ;. Now p would contain the address of VTABLE. To obtain the address
of the first virtual function in the VTABLE we need to extract its address from the VTABLE. Since
fun( ) is the first virtual function its address would be in the first two bytes of the VTABLE. Hence
we have extracted the address of the first two bytes by writing the statement, p = ( int * ) *p ;.
Now p contains the address of the first virtual function, which is fun( ) in our case. To call the
function we have to store the current value of p in a pointer to function pfun, which is declared as
void ( _cdecl *pfun ) ( sample * const, int ) ;
Here void is the return type of the function fun( ). _cdecl is a calling convention and sample
*const, int are the arguments passed to the function. The first argument passed as sample *
const is hidden (which is the this pointer) and hence is not shown in the function definition.
Why is it necessary to use a reference in the argument to the copy constructor?
Ans : If we pass the copy constructor the argument by value, its copy would get constructed
using the copy constructor. This means the copy constructor would call itself to make this copy.
This process would go on and on until the compiler runs out of memory. This can be explained
with the help of following example:
class sample
{
int i ;
public :
sample ( sample p )
{
i = p.i ;
};
void main( )
{
sample s ;
sample s1 ( s ) ;
While executing the statement sample s1 ( s ), the copy constructor would get called. As the
copy construct here accepts a value, the value of s would be passed which would get collected in
p. We can think of this statement as sample p = s. Here p is getting created and initialized.
Means again the copy constructor would get called. This would result into recursive calls.Hence
we must use a reference as an argument in a copy constructor.
Can objects read and write themselves?
Ans: Yes! This can be explained with the help of following example:
How do I change a data member of the const object?
Ans: To change the data members of the const object the data members are declared as
mutable in the class.
What is an array of pointers to member functions and what is its limitation?
Ans: The following program shows how to create an array of pointers to member functions:
#include <iostream.h>
class sample
{
public :
void fun1( )
{
cout << "fun1" << endl ;
}
void fun2( )
{
cout << "fun2" << endl ;
void fun3( )
{
cout << "fun3" << endl ;
};
void ( sample::*pfun [ 3 ] ) ( ) = {
&sample::fun1,
&sample::fun2,
&sample::fun3
};
void main( )
{
sample s ;
for ( int i = 0 ; i <= 2 ; i++ )
( s.*pfun [ i ] ) ( ) ;
Here, pfun is an array of pointers to member functions which holds the address of the functions
fun1( ), fun2( ) and fun3( ). The limitation of the array of pointers to member functions is that we
cannot store the addresses of the different type of functions in it. All the functions whose
addresses it contains should have the same prototype.