July 24, 2020

Srikaanth

Mindtree Frequently Asked C++ Interview Questions Answers

What Is A Virtual Destructor?

Virtual destructors help in destroying objects without knowing their type. With the help of the virtual function mechanism, the appropriate destructor for the object is invoked. In the case of abstract classes, destructors can also be declared as pure virtual functions. For example, class A derives from class B. Then, on calling the derived class dynamically at the execution time, the destructor will first call the derived class that is class A, and then the base class that is class B.

It is important to note that theVirtual keyword, when used with the destructor, ensures the calling of all the derived and base class destructors and therefore helps in the proper execution and closing of the program.

Can You Explicitly Call A Destructor On A Local Variable?

No, the destructor is called when the block closes in which the local variable was created.
If a destructor is called explicitly twice on the same object, the result ends in an undefined behavior because the local variable gets destroyed when the scope ends.

What Are Shallow And Deep Copies?

A shallow copy is used to copy actual values of the data. It means, if the pointer points to dynamically allocated memory, the new object's pointer in the copy still points to items inside the old objects and the returned object will be left pointing to items that are no longer in scope. A copy of the dynamically allocated objects is created with the help of a deep copy. This is done with the help of an assignment operator, which needs to be overloaded by the copy constructor.

What Do You Understand By Zombie Objects In C++?

In a situation, where an object is created in a class, a constructor fails before its full execution. It is very difficult to ensure about the execution of the constructor whether the constructor would return a value or not. Objects that are no more required for an application are called zombie objects. These zombie objects occupy a space in the memory and wait for their termination.

Should The This Pointer Can Be Used In The Constructor?

We can use the this pointer in the constructor in the initialization list and also in the body. However, there is a feeling that the object is not fully formed so we should not use this pointer. Let's understand the use of the this pointer with the help of the following example. The declared data members of a base class and/or the declared data members of the constructor's own class can be accessed by the constructor {body} and/or a function called from the constructor. This is possible because of the full construction of constructor's body at the time of execution. The preceding example always works.
The override in the derived class is not possible for a function called from the constructor and the {body} of a constructor which is independent of calling the virtual member function by the explicit use of the this pointer.
Please make sure that the initialization of other data member has already been done before passing the data member to another data member's initializer in this object. With the help of some straightforward language rules (independent of the specific compiler that you are using), you can confirm about the determination of initialization of data members.
Without the prior knowledge of these rules, there is no use of passing any data member from the this object.
Mindtree Frequently Asked C++ Interview Questions Answers
Mindtree Frequently Asked C++ Interview Questions Answers

What Do You Understand By A Pure Virtual Member Function?

A virtual function with no body structure is called a pure virtual member function. You can declare a pure virtual member function by adding the notation =0 to the virtual member function declaration. For example, area () is a virtual function, but when it is declared as shown in the following expression, it becomes a pure virtual member function: virtual int area () =0;

To avoid a compilation error, all the classes need to implement pure virtual classes that are derived from the abstract class.

How Can Virtual Functions In C++ Be Implemented?

When a class has at least one virtual function, the compiler builds a table of virtual function pointers for that class. This table, commonly called the v-table, contains an entry for each virtual function in the class. The constructor of the class is used to create this table. Each object of the class in memory includes a variable called the vptr, which points to the class's common vtbl. The _rst (which creates the vtable) is constructed by the base classes after the construction of the derived classes. The derived class constructor overwrites the entries of the vtable, if its constructor overrides any virtual function of the base class. Therefore, you need not call every function from a constructor just to avoid the error prone scenario of calling the Base class constructor instead of the derived class constructor (which fails to set the entries of objects of vtable).

What Do You Understand By Pure Virtual Function? Write About Its Use?

A pure virtual function in a base class must have a matching function in a derived class. A program may not declare an instance of a class that has a pure virtual function. A program may not declare an instance of a derived class if that derived class has not provided an overriding function for each pure virtual function in the base. If you want programmers to override certain functions in a class, such as those that need information customized for a particular installation, you should make these functions pure virtual.

How The Virtual Functions Maintain The Call Up?

The call is maintained through vtable. Each object includes a variable known as vptr, which points to the class's common vtable. It contains an entry for every virtual function. On calling the function, vtable calls the appropriate function using vptr.

Write A Note About Inheritance?

Inheritance is a mechanism that allows the object of one class to utilize the form and model or behavior of another class. A class derived from the base class inherits attributes, functions, or methods that implement the base class to the derived class and also extends the derived class by overriding functions and adding new additional attributes and functionalities.

What Is The Need Of Multiple Inheritance?

The multiple inheritance allows you to define a new class that inherits the characteristics of several base classes which are not related to each other. The vehicle class encapsulates the data and behavior that describe vehicles along with other information, such as date of purchase, life, and maintenance schedules. Classes that support specific kinds of vehicles, such as trucks, airplanes, and cars are also derived from the vehicle class. The asset class encapsulates the data and behavior of the organization's assets, including acquiring date, and depreciation schedule data for accounting purposes. A company car, being both a vehicle and an asset, is represented by a class that derives from both base classes.

Can We Use This Pointer Inside Static Member Function?

No! The this pointer cannot be used inside a static member function. This is because a static member function is never called through an object.

What Is Strstream?

strstream is a type of input/output stream that works with the memory. It allows using section of the memory as a stream object. These streams provide the classes that can be used for storing the stream of bytes into memory. For example, we can store integers, floats and strings as a stream of bytes. There are several classes that implement this in-memory formatting. The class ostrstream derived from ostream is used when output is to be sent to memory, the class istrstream derived from istream is used when input is taken from memory and strstream class derived from iostream is used for memory objects that do both input and output.

When The Constructor Of A Base Class Calls A Virtual Function, Why Doesn't The Override Function Of The Derived Class Gets Called?

While building an object of a derived class first the constructor of the base class and then the constructor of the derived class gets called. The object is said an immature object at the stage when the constructor of base class is called. This object will be called a matured object after the execution of the constructor of the derived class. Thus, if we call a virtual function when an object is still immature, obviously, the virtual function of the base class would get called. This is illustrated in the following example.

#include

class base
{
   protected
     int i ;
     public
       base ( int ii = 0 )
       {
           i = ii ;
           show( ) ;
       }

       virtual void show( )
       {
            cout << "base's show( )" << endl ;
       }
} ;

class derived : public base
{
      private
        int j ;
      public
        derived ( int ii, int jj = 0 ) : base ( ii )
        {
           j = jj ;
           show( ) ;
        }
       void show( )
       {
           cout << "derived's show( )" << endl ;
       }
} ;
                   
void main( )
{
   derived dobj ( 20, 5 ) ;
}
The output of this program would be

base's show( )
derived's show(

Can I Have A Reference As A Data Member Of A Class? If Yes, Then How Do I Initialise It?

Yes, we can have a reference as a data member of a class. A reference as a data member of a class is initialised in the initialisation list of the constructor. This is shown in following program.

#include
class sample
{
   private
     int& i ;
     public
       sample ( int& ii ) : i ( ii )
       {
       }
       void show( )
       {
         cout << i << endl ;
       }
} ;

void main( )
{
  int j = 10 ;
  sample s ( j ) ;
  s.show( ) ;
}

Here, i refers to a variable j allocated on the stack. A point to note here is that we cannot bind a reference to an object passed to the constructor as a value. If we do so, then the reference i would refer to the function parameter (i.e. parameter ii in the constructor), which would disappear as soon as the function returns, thereby creating a situation of dangling reference.

Why Does The Following Code Fail?

#include<iostream.>
#include<string.h>
Class Sample
{
Private :char *str ;
Public : Sample ( Char *s )
{
Strcpy ( Str, S ) ;
}
~sample( )
{
Delete Str ;
}
} ;
Void Main( )
{
Sample S1 ( "abc" ) ;
}

Here, through the destructor we are trying to deal locate memory, which has been allocated statically. To remove an exception, add following statement to the constructor.

sample ( char *s )
{
   str = new char[strlen(s) + 1] ;
   strcpy ( str, s ) ;
}
Here, first we have allocated memory of required size, which then would get deal located through the destructor.

How Can You Say That A Template Is Better Than A Base Class?

In case of designing a generic data type to manage objects of other data types which are unknown to their container class, a template is preferred over a base class. A template can contain more than one data type parameter, making it possible to build parameterized data types of considerable complexity. The class template also allows the declaration of more than one object in the same program.

Explain The Concept Of Multiple Inheritance (virtual Inheritance). Write About Its Advantages And Disadvantages?

Multiple inheritance is defined as the ability of a derived class to inherit the characteristics of more than one base class. For example, you might have a class zoo which inherits various animal classes. The advantage of multiple inheritance is that you can create a single derived class from several base classes. It helps in understanding the logic behind these complex relationships.

The multiple inheritance is not preferred by the programmers because with multiple base classes, there are chances of conflicts while calling a method in the sub class that exists in both the base classes. For example, if you have a class named zoo that inherits both the Anaconda and Zebra classes, then each base class might define a different version of the method walk, which creates a conflicting situation.

Subscribe to get more Posts :