Brocade Communications C++ Interview Questions Answers

Brocade Communications Most Frequently Asked C++ Interview Questions Answers

Why Is That Unsafe To Deal Locate The Memory Using Free( ) If It Has Been Allocated Using New?

This can be explained with the following example

#include
class sample
{
  int *p ;
  public
     sample( )
     {
       p = new int ;
     }

     ~sample( )
     {
        delete p ;
     }
} ;

void main( )
{
  sample *s1 = new sample ;
  free ( s1 ) ;
  sample *s2 = ( sample * ) malloc ( sizeof ( sample ) ) ;
  delete s2 ;
}
The new operator allocates memory and calls the constructor. In the constructor we have allocated memory on heap, which is pointed to by p. If we release the object using the free( ) function the object would die but the memory allocated in the constructor would leak. This is because free( ) being a C library function does not call the destructor where we have deal located the memory.
As against this, if we allocate memory by calling malloc( ) the constructor would not get called. Hence p holds a garbage address. Now if the memory is deal located using delete, the destructor would get called where we have tried to release the memory pointed to by p. Since p contains garbage this may result in a runtime error.
Brocade Communications Most Frequently Asked C++ Interview Questions Answers
Brocade Communications Most Frequently Asked C++ Interview Questions Answers

Can We Distribute Function Templates And Class Templates In Object Libraries?

No! We can compile a function template or a class template into object code (.obj file). The code that contains a call to the function template or the code that creates an object from a class template can get compiled. This is because the compiler merely checks whether the call matches the declaration (in case of function template) and whether the object definition matches class declaration (in case of class template). Since the function template and the class template definitions are not found, the compiler leaves it to the linker to restore this. However, during linking, linker doesn't find the matching definitions for the function call or a matching definition for object creation. In short the expanded versions of templates are not found in the object library. Hence the linker reports error.

Differentiate Between An Inspector And A Mutator ?

An inspector is a member function that returns information about an object's state (information stored in object's data members) without changing the object's state. A mutator is a member function that changes the state of an object. In the class Stack given below we have defined a mutator and an inspector.

class Stack
{
   public
   int pop( ) ;
   int getcount( ) ;
}
In the above example, the function pop( ) removes top element of stack thereby changing the state of an object. So, the function pop( ) is a mutator. The function getcount( ) is an inspector because it simply counts the number of elements in the stack without changing the stack.

Namespaces

The C++ language provides a single global namespace. This can cause problems with global name clashes. For instance, consider these two C++ header files: // file1.h float f ( float, int ) ; class sample { ... } ; // file2.h class sample { ... } ; With these definitions, it is impossible to use both header files in a single program; the sample classes will clash.A namespace is a declarative region that attaches an additional identifier to any names declared inside it. The additional identifier thus avoids the possibility that a name will conflict with names declared elsewhere in the program. It is possible to use the same name in separate namespaces without conflict even if the names appear in the same translation unit. As long as they appear in separate namespaces, each name will be unique because of the addition of the namespace identifier. For example

// file1.h
namespace file1
{
   float f ( float, int ) ;
   class sample { ... } ;
}
// file2.h
namespace file2
{
    class sample { ... } ;
}
Now the class names will not clash because they become file1::sample and file2::sample, respectively.

Declare A Static Function As Virtual?no. The Virtual Function Mechanism Is Used On The Specific Object That Determines Which Virtual Function To Call. Since The Static Functions Are Not Any Way Related To Objects, They Cannot Be Declared As Virtual.

No. The virtual function mechanism is used on the specific object that determines which virtual function to call. Since the static functions are not any way related to objects, they cannot be declared as virtual.

Can User-defined Object Be Declared As Static Data Member Of Another Class?

Yes. The following code shows how to initialize a user-defined object.

#include
class test
{
  int i ;
  public
    test ( int ii = 0 )
    {
      i = ii ;
    }
} ;
class sample
{
  static test s ;
} ;
test sample::s ( 26 ) ;
Here we have initialized the object s by calling the one-argument constructor. We can use the same convention to initialize the object by calling multiple-argument constructor.

What Is A Forward Referencing And When Should It Be Used?

Consider the 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 ) ;
}
This program would not compile. It gives an error that sample is undeclared identifier in the statement friend void fun ( sample, test ) ; of the class 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. Forward referencing is generally required when we make a class or a function as a friend.

What Is 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 ;
}

Can We Use This Pointer In A Class Specific, Operator-overloading Function For New Operator?

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( ).

How To Allocate Memory Dynamically For A Reference?

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.

Post a Comment

Previous Post Next Post