April 23, 2019

Srikaanth

RELX Group Frequently Asked C++ Interview Questions

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

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.

Describe Inheritance And Non-inheritance Of A Derived Class?

Using inheritance, you can derive all the data members and all the other common functions of the base class and still retain the functionality of the common methods of the base class. For example, the animal class with eat, sleep and breathe methods comprises the base class because they are common to all animals. Now, a new class Elephant with a method trumpet is derived from the class animal. With the help of inheritance, the Elephant class inherits the trumpet method and still retains the eat, sleep, and breathe methods from the base class, animal.

Therefore, you can add new methods to an existing class with the retaining of common methods. There are few methods that are not inherited on creating a derived class which includes constructors, destructors, and assignment operator methods of their base classes.

Write A Note About The Virtual Member Function?

A virtual function is a member function of the base class and relies on a specific object to determine which implementation of the function is called. However, a virtual function can be declared a friend of another class. If a function is declared virtual in a base class, you can still access it directly using the :: operator. Note that if you do not override a virtual member function in a derived class, a call to that function uses the function implementation defined in the base class.

Should The Member Functions Which Are Made Public In The Base Class Be Hidden?

As the public member functions of the base class are necessary to implement the public interface of the class, the member functions which are made public in the base class should never be hidden. When you are designing a class, make the derived data members private because a private member of the derived class is accessed through the protected or public member functions of the base class.

Can Circle Be Called An Ellipse?

Yes, a circle can be called an ellipse. Let's understand this concept with the help of an example, if ellipse has a member function named as setsize with the widthQ of the object as x and its height() as y. There are two kinds of relationships that exist between a circle and an ellipse
Circle and ellipse can be made as two different classes
In this case, ellipse can be derived from AsymmetricShape class and has a member function named as setSize(x,y). On the contrary, circle can be derived from SymmetricalShape class and has a member function named as setSize(size). Therefore, they both are unrelated in its member functions as well as in their derivations.

 Circle and ellipse can be derived from a base class
In this case, circle and ellipse both can be inherited from the class Oval because class Oval can only have setSize(size) member function which sets the height() and widthQto the size of the object.

Write About All The Implicit Member Functions Of A Class?

All the implicit member functions of a class are listed as follows
a. Default constructor —Defines a constructor with no parameters.
b. Copy constructors —Initializes the value from an existing object of a class to a new, instantiated object of that same class.
c. Assignment operator—-Returns a value.
d. Default destructor—Runs automatically when an object is being destroyed.
e. Address operator —Takes one operand and returns the memory address of a variable or function.

Subscribe to get more Posts :