(Paper) C++ Programming tips &Technical Interview Questions Source code examples Set-5

Paper : C++ Programming tips &Technical Interview Questions Source code examples Set-5

41.What is forward referencing and when should it be used?
Ans: Forward referencing is generally required when we make a class or a function as a friend.
Consider following program:
                             class test
                             {
                             public:
                             friend void fun ( sample, test ) ;
                             } ;

                             class sample
                             {
                             public:
                             friend void fun ( sample, test ) ;
                             } ;

                             void fun ( sample s, test t )
                             {
                             // code
                             }

                             void main( )
                             {
                             sample s ;
                             test t ;
                             fun ( s, t ) ;
                             }
On compiling this program it gives error on the following statement of test class. It gives an error that sample is undeclared identifier. friend void fun ( sample, test ) ;
This is so because the class sample is defined below the class test and we are using it before its definition. To overcome this error we need to give forward reference of the class sample before the definition of class test. The following statement is the forward reference of class sample.
class sample ;
-------------------------------------------------------------------------------------------------
42.How would you give an alternate name to a namespace?

Ans: An alternate name given to namespace is called a namespace-alias. namespace-alias is generally used to save the typing effort when the names of namespaces are very long or complex. The following syntax is used to give an alias to a namespace.
namespace myname = my_old_very_long_name ;

-------------------------------------------------------------------------------------------------
43.Using a smart pointer can we iterate through a container?

Ans: Yes. A container is a collection of elements or objects. It helps to properly organize and store the data. Stacks, linked lists, arrays are examples of containers. Following program shows how to iterate through a container using a smart pointer.
     #include

     class smartpointer
     {
     private :
     int *p ; // ordinary pointer
     public :
     smartpointer ( int n )
     {
     p = new int [ n ] ;
     int *t = p ;
     for ( int i = 0 ; i <= 9 ; i++ )
     *t++ = i * i ;
     }

     int* operator ++ ( int )
     {
     return p++ ;
     }

     int operator * ( )
     {
     return *p ;
     }
     } ;

     void main( )
     {
     smartpointer sp ( 10 ) ;

     for ( int i = 0 ; i <= 9 ; i++ )
     cout << *sp++ << endl ;
     }
Here, sp is a smart pointer. When we say *sp, the operator * ( ) function gets called. It returns the integer being pointed to by p. When we say sp++ the operator ++ ( ) function gets called. It increments p to point to the next element in the array and then returns the address of this new location.  
.H> .H>

44.Can objects read and write themselves?

Ans: Yes!  This can be explained with the help of following example:

     #include
     #include

     class employee
     {
     private :
     char name [ 20 ] ;
     int age ;
     float salary ;
     public :
     void getdata( )
     {
     cout << "Enter name, age and salary of employee : " ;
     cin >> name >> age >> salary ;
     }

     void store( )
     {
     ofstream file ;
     file.open ( "EMPLOYEE.DAT", ios::app | ios::binary ) ;
     file.write ( ( char * ) this, sizeof ( *this ) ) ;
     file.close( ) ;
     }

     void retrieve ( int n )
     {
     ifstream file ;
     file.open ( "EMPLOYEE.DAT", ios::binary ) ;
     file.seekg ( n * sizeof ( employee ) ) ;
     file.read ( ( char * ) this, sizeof ( *this ) ) ;
     file.close( ) ;
     }

     void show( )
     {
     cout << "Name : " << name
     << endl << "Age : " << age
     << endl << "Salary :" << salary << endl ;
     }
     } ;

     void main( )
     {
     employee e [ 5 ] ;

     for ( int i = 0 ; i <= 4 ; i++ )
     {
     e [ i ].getdata( ) ;
     e [ i ].store( ) ;
     }

     for ( i = 0 ; i <= 4 ; i++ )
     {
     e [ i ].retrieve ( i ) ;
     e [ i ].show( ) ;
     }
     }
Here, employee is the class whose objects can write and read themselves. The getdata( ) function has been used to get the data of employee and store it in the data members name, age and salary. The store( ) function is used to write an object to the file. In this function a file has been opened in append mode and each time data of current object has been stored after the last record (if any) in the file.Function retrieve( ) is used to get the data of a particular employee from the file. This retrieved data has been stored in the data members name, age and salary. Here this has been used to store data since it contains the address of the current object. The function show( ) has been used to display the data of employee.
-------------------------------------------------------------------------------------------------
45.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.
.H>
.H>

46.Virtual Multiple Inheritance:
A class b is defined having member variable i.  Suppose two classes d1 and d2 are derived from class b and a class multiple is derived from both d1 and d2. If variable i is accessed from a member function of multiple then it gives error as 'member is ambiguous'. To avoid this error derive classes d1 and d2 with modifier virtual as shown in the following program.
     #include
     class b
     {
     public :
     int i ;
     public :
     fun( )
     {
     i = 0 ;
     }
     } ;
     class d1 : virtual public b
     {
     public :
     fun( )
     {
     i = 1 ;
     }
     } ;
     class d2 : virtual public b
     {
     public :
     fun( )
     {
     i = 2 ;
     }
     } ;
     class multiple : public d1, public d2
     {
     public :
     fun( )
     {
     i = 10 ;
     }
     } ;
     void main( )
     {
     multiple d ;
     d.fun( ) ;
     cout << d.i ;
     }
-------------------------------------------------------------------------------------------------
47.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( ).
-------------------------------------------------------------------------------------------------
48.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.
.H>

49.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
     #include

     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
     #include

     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.
-------------------------------------------------------------------------------------------------
50.How would you define a pointer to a data member of the type pointer to pointer?

     Ans: The following program demonstrates this...
     #include

     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 ( π ) ;
     cout << ** ( s.*ptr ) ;
     }



comments powered by Disqus