February 16, 2019

Srikaanth

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

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.

How To Give An Alternate Name To A Namespace?

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 ;

Define A Pointer To A Data Member Of The Type Pointer To Pointer?

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 ) ;
}

Using A Smart Pointer Can We Iterate Through A Container?

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.

Is It Possible For The Objects To Read And Write Themselves?

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.

Subscribe to get more Posts :