October 28, 2018

Srikaanth

Globant Most Frequently Asked C++ Interview Questions Answers

What Is A Template In C++?

Templates allow to create generic functions that admit any data type as parameters and return value without having to overload the function with all the possible data types. Until certain point they fulfill the functionality of a macro. Its prototype is any of the two following ones

template <class indetifier> functiondeclaration; template <typename indetifier> functiondeclaration;

The only difference between both prototypes is the use of keyword class or typename, its use is indistinct since both expressions have exactly the same meaning and behave exactly the same way.

Define A Constructor - What It Is And How It Might Be Called (2 Methods).

constructor is a member function of the class, with the name of the function being the same as the class name. It also specifies how the object should be initialized.
Ways of calling constructor

Implicitly: automatically by complier when an object is created.
Calling the constructors explicitly is possible, but it makes the code unverifiable.
Globant Most Frequently Asked C++ Interview Questions Answers
Globant Most Frequently Asked C++ Interview Questions Answers

You Have Two Pairs: New() And Delete() And Another Pair : Alloc() And Free(). Explain Differences Between Eg. New() And Malloc()

"new and delete" are preprocessors while "malloc() and free()" are functions, [we dont use brackets will calling new or delete].
no need of allocate the memory while using "new" but in "malloc()" we have to use "sizeof()".
"new" will initlize the new memory to 0 but "malloc()" gives random value in the new alloted memory location [better to use calloc()]

Write A Program That Will Convert An Integer Pointer To An Integer And Vice-versa.?

The following program demonstrates this.

#include<stdio.h>
#include<iosream>
#include<conio.h>
void main( )
{
    int i = 65000 ;
    int *iptr = reinterpret_cast ( i ) ;
    cout << endl << iptr ;
    iptr++ ;
    cout << endl << iptr ;
    i = reinterpret_cast ( iptr ) ;
    cout << endl << i ;
    i++ ;
    cout << endl << i ;
}

What Is Meant By Const_cast?

The const_cast is used to convert a const to a non-const. This is shown in the following program

#include
void main( )
{
     const int a = 0  ;
     int *ptr = ( int * ) &a ; //one way
     ptr = const_cast_ ( &a ) ; //better way
}
Here, the address of the const variable a is assigned to the pointer to a non-const variable. The const_cast is also used when we want to change the data members of a class inside the const member functions. The following code snippet shows this

class sample
{
    private
    int data;
    public
      void func( ) const
      {
        (const_cast (this))->data = 70 ;
      }
};

What Is The Difference Between Class And Structure In C++?

Structure: Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public. Class: Class is a successor of Structure. By default all the members inside the class are private.

What Is Rtti In C++?

Runtime type identification (RTTI) lets you find the dynamic type of an object when you have only a pointer or a reference to the base type. RTTI is the official way in standard C++ to discover the type of an object and to convert the type of a pointer or reference (that is, dynamic typing). The need came from practical experience with C++. RTTI replaces many homegrown versions with a solid, consistent approach.

What Is Encapsulation In C++?

Packaging an object's variables within its methods is called encapsulation.

What Is A C++ Object?

Object is a software bundle of variables and related methods. Objects have state and behavior.

Describe Private, Protected And Public - The Differences And Give Examples.

class Point2D{ int x; int y;
public int color;
protected bool pinned;
public Point2D():x(0),y(0){} //default(no argument) constructor
};
Point2D MyPoint;
You cannot directly access private data members when they are declared (implicitly) private

MyPoint.x = 5;          // Compiler will issue a compile ERROR
//Nor yoy can see them
int xdim = MyPoint.x;  // Compiler will issue a compile ERROR
On the other hand, you can assign and read the public data members

MyPoint.color = 255;      //no problem
int col = MyPoint.color; // no problem
With protected data members you can read them but not write them

bool isPinned = MyPoint.pinned; // no problem.

Stl Containers - What Are The Types Of Stl Containers?

There are 3 types of STL containers

Adaptive containers like queue, stack.
Associative containers like set, map.
Sequence containers like vector, deque.

Rtti - What Is Rtti In C++?

RTTI stands for "Run Time Type Identification". In an inheritance hierarchy, we can find out the exact type of the objet of which it is member. It can be done by using

dynamic id operator.
typecast operator.

Assignment Operator - What Is The Diffrence Between A "assignment Operator" And A "copy Constructor"?

In assignment operator, you are assigning a value to an existing object. But in copy constructor, you are creating a new object and then assigning a value to that object.
For example

complex cl,c2;
cl=c2;           //this is assignment
complex c3=c2;  //copy constructor.

Subscribe to get more Posts :