February 16, 2019

Srikaanth

Red Hat Frequently Asked C++ Interview Questions Answers

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.
Red Hat Frequently Asked C++ Interview Questions Answers
Red Hat Frequently Asked C++ Interview Questions Answers

Write Code To Make An Object Work Like A 2-d Array?: Take a look at the following program.

#include
class emp
{
  public
    int a[3][3] ;
    emp( )
    {
      int c = 1 ;
      for ( int i = 0 ; i <= 2 ; i++ )
      {
         for ( int j = 0 ; j <= 2 ; j++ )
         {
           a[i][j] = c ;
           c++ ;
         }
       }
     }
     int* operator[] ( int i )
     {
       return a[i] ;
     }
} ;
void main( )
{
  emp e ;
  cout << e[0][1] ;
}
The class emp has an overloaded operator [ ] function. It takes one argument an integer representing an array index and returns an int pointer. The statement cout << e[0][1] ; would get converted into a call to the overloaded [ ] function as e.operator[ ] ( 0 ). 0 would get collected in i. The function would return a[i] that represents the base address of the zeroeth row. Next the statement would get expanded as base address of zeroeth row[1] that can be further expanded as *( base address + 1 ). This gives us a value in zeroth row and first column.

What Are Formatting Flags In Ios Class?

The ios class contains formatting flags that help users to format the stream data. Formatting flags are a set of enum definitions. There are two types of formatting flags:On/Off flagsFlags that work in-group The On/Off flags are turned on using the setf( ) function and are turned off using the unsetf( )function. To set the On/Off flags, the one argument setf( ) function is used. The flags working in groups are set through the two-argument setf( ) function. For example, to left justify a string we can set the flag as,

cout.setf ( ios::left );
cout << "KICIT Nagpur";
To remove the left justification for subsequent output we can say,
cout.unsetf ( ios::left );
The flags that can be set/unset include skipws, showbase, showpoint, uppercase, showpos, unitbufand stdio. The flags that work in a group can have only one of these flags set at a time.

What Is The Purpose Of Ios::basefield In The Following Statement?

cout.setf ( Ios::hex, Ios::basefield );

This is an example of formatting flags that work in a group. There is a flag for each numbering system (base) like decimal, octal and hexadecimal. Collectively, these flags are referred to as basefield and are specified by ios::basefield flag. We can have only one of these flags on at a time. If we set the hex flag as setf ( ios::hex ) then we will set the hex bit but we won't clear the dec bit resulting in undefined behavior. The solution is to call setf( ) as setf ( ios::hex, ios::basefield ). This call first clears all the bits and then sets the hex bit.

Why Do We Separate Interface From Implementation?

The interface is visible to the user of the class and consists of public members, which are usually member functions. The class user reads and modifies values in data representation by calling public member functions. The interface is generic in that it is not bound to any particular implementation.

The implementation of a class, which consists of private data members and private member functions, is essentially hidden from the program. The implementation defines the details of how the class implements the behavior of the abstract base type. The class author should be able to change the implementation without affecting the program.

How Can You Differentiate Between Inheritance And Implementation In C++?

With the help of abstract base classes, we can differentiate between interface and implementation in C++.

Write About Abstract Base Classes?

An abstract base class is a class definition that is always a base class for other classes to be derived from. No specific objects of the base class are declared by the program. A C++ abstract base class is one that has a pure virtual function, a protected constructor, or a protected destructor.

At the design level, you create a pure virtual method by using = 0 in place of a method body. This notation specifies that the member function is a pure virtual function. This means that the base class is an abstract base class, and the class designer intends the class to be used only as a base class. The base class may or may not provide a function body for the pure virtual function. In either case, a program that uses this class may not directly declare any objects of the abstract base class. If the program declares an object of a class directly or indirectly derived from the abstract base class, the pure virtual function must be overridden explicitly.

Which Should Be More Useful: The Protected And Public Virtuals?

Public virtuals are more preferable than protected virtuals. Public virtuals permit a program to directly modify the values of data members. It can be accessible to any function that is within the scope of the structure. It translates the implementation for the user of the class. It is the most common, convenient, and easiest way to implement by any of the programs.

On the other hand, protected virtual functions are used for hiding some methods or data members from the outside class making it consistent and symmetrical in its approach. It is condition specific, so not commonly used.

Describe The Setting Up Of My Member Functions To Avoid Overriding By The Derived Class?

To avoid overriding of the member functions by the derived class, the leaf method is used. With the help of the leaf method, it is possible to leave the code unaligned at the time of execution by simply adding a comment next to the method. This method is easy, fast, and inexpensive to use.

How Do C++ Struct Differs From The C++ Class?

C++ defines structures and classes almost identically. To declare a class, you use the class keyword in place of the struct keyword. The only other differences are related to the default access specifiers. The members of a structure have public access by default; whereas, the member of a class has private access by default. The concept of encapsulation is supported by the class only and not the structure.

Write About The Use Of The Virtual Destructor?

When an object is declared with the new operator and the pointer type is that of a base class with a non-virtual destructor, the base destructor executes instead of the derived destructor. When a base class destructor is virtual, the compiler calls the correct destructor function irrespective of the type of the pointer.

If the base class needs no custom destruction, you must still provide a virtual destructor (with an empty block) to permit the proper destructor calls for dynamically allocated objects. Moreover, by making the destructor of the base class as virtual, you can invoke the destructors of both the base and derived classes in the reverse order.

Write A Note On Encapsulation?

Encapsulation is an object-oriented design approach that closely binds the implementation of a class to its data representation, logically hides the details of the implementation from users of the data type, and provides a public interface to the data type's behavior. It implies an implementation, which is hidden from the class user, and an interface, which is visible to the class user. It helps you to handle large scale programming tasks.The idea behind encapsulation is to take a complex system that demands a lot of attention and to turn it into an object that handles all its work internally and can easily form a concept.

What Are The Effects After Calling The Delete This Operator ?

It is difficult for the compiler to know whether an object is allocated on a stack or a heap. On invoking the delete this operator, the destructor is called twice on the object related to the current context. Firstly implicitly and secondly explicitly. It results in undefined behavior and error-prone conditions. The delete this operator can only work on three conditions

 An Instantiation of a local variable should not take place, after calling the delete this destructor.
 The this pointer should not be used after calling the delete this operator.
 The object of a derived class should be allocated and initialized with the new operator

Can We Get The Value Of Ios Format Flags?

Yes! The ios::flags( ) member function gives the value format flags. This function takes no arguments and returns a long ( typedefed to fmtflags) that contains the current format flags.

Is There Any Function That Can Skip Certain Number Of Characters Present In The Input Stream?

Yes! This can be done using cin::ignore( ) function. The prototype of this function is as shown below

istream& ignore ( int n = 1, int d =EOF );
Sometimes it happens that some extra characters are left in the input stream while taking the input such as, the '\n' (Enter) character. This extra character is then passed to the next input and may pose problem.

To get rid of such extra characters the cin::ignore( ) function is used. This is equivalent to fflush ( stdin ) used in C language. This function ignores the first n characters (if present) in the input stream, stops if delimiter d is encountered.

When Should Overload New Operator On A Global Basis Or A Class Basis?

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.

Subscribe to get more Posts :