Fiserv Most Frequently Asked Latest C++ Interview Questions Answers

What Is A Copy Constructor And When Is It Called?

A copy constructor is a method that accepts an object of the same class and copies it's data members to the object on the left part of assignement

class Point2D{ int x; int y;
public int color;
protected bool pinned;
public Point2D() : x(0),y(0){} //default (no argument)constructor
public Point2D( const Point2D & );
};
Point2D::Point2D( const Point2D & p )
{
this->x = p.x; this->y = p.y;
this->color = p.color;
this->pinned = p.pinned;
}
main()
{
Point2D MyPoint;
MyPoint.color  = 345;
Point2D AnotherPoint = Point2D( MyPoint);
// now AnotherPoint has color = 345

What Do You Mean By Inheritance?

Inheritance is the process of creating new classes, called derived classes, from existing classes or base classes. The derived class inherits all the capabilities of the base class, but can add embellishments and refinements of its own.

Can We Define A Constructor As Virtual In C++?

No, we cannot define constructors as virtual because constructors have the same name as their classes and no two constructors of base-derived classes can have the same name. So, when we initialize an object of the base or derived class with the help of virtual constructors, the base constructor is invoked instead of the derived constructor. Therefore, it is not possible to define a constructor as virtual.

Can We Declare A Base-class Destructor As Virtual?

Yes, we can declare a base-class destructor as virtual that makes all derived-class destructors as virtual even if they do not have the same name as base-destructor. The problem arises when the derived class's pointer refers to a base class. For this reason, the base class destructor should be declared as virtual so that the appropriate destructor is called on calling the delete method of the base class object.

How Does A Copy Constructor Differs From An Overloaded Assignment Operator?

A copy constructor uses the value of an argument to construct a new object. We can use an overload assignment operator to assign the value of an existing object of the same class to another existing object in that class.

Define The Process Of Error-handling In Case Of Constructor Failure?

If the constructor does not have the return statement, then it indicates failure in handling the error by throwing an exception.
Fiserv Most Frequently Asked Latest C++ Interview Questions Answers
Fiserv Most Frequently Asked Latest C++ Interview Questions Answers

What Is A Default Constructor?

A zero-argument constructor or a constructor in which all the arguments have default values is called a default constructor.
For example
A Al; // default constructor called.

Define The Process Of Handling In Case Of Destructor Failure?

In order to handle a failed destructor, you need to write a message to a log file; however, do not throw an exception. There is a rule in C++ that exception cannot be thrown from a destructor, which is called when the process of "stack unwinding" occurs in other exceptions. For example, if someone says throw waste files(), the stack frames between the throw waste files() and the catch (waste files) will get popped. This is known as stack unwinding. It is the process of destroying all the local objects related to those stack frames and calling destructors in case of throwing of an exception by one of those destructors. For example, if an object named Bar is thrown, then the C++ runtime system is in a neutral situation means either to avoid the Bar and end up in the catch (waste files) or ignore the function Foo and look for a catch (Bar) handler. It will call in the terminate () process to end the program.

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.

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(

Post a Comment

Previous Post Next Post