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

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.

Post a Comment

Previous Post Next Post