Hitachi Most Frequently Asked Latest 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.

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.
Hitachi Most Frequently Asked Latest C++ Interview Questions Answers
Hitachi Most Frequently Asked Latest C++ Interview Questions Answers

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.

Post a Comment

Previous Post Next Post