May 10, 2019

Srikaanth

Kaspersky Lab Asked C++ Interview Questions Answers

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

Subscribe to get more Posts :