Google

Frequently asked C++ Interview Questions and Answers

1.) “new and delete” are preprocessors while “malloc() and free()” are functions. [we dont use brackets will calling new or delete].


2.) no need of allocate the memory while using “new” but in “malloc()” we have to use “sizeof()”.


3.) “new” will initlize the new memory to 0 but “malloc()” gives random value in the new alloted memory location [better to use calloc()]





Answer2
new() allocates continous space for the object instace


malloc() allocates distributed space.


new() is castless, meaning that allocates memory for this specific type,


malloc(), calloc() allocate space for void * that is cated to the specific class type pointer.




What is the difference between class and structure?


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?


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?



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



Explain term POLIMORPHISM and give an example using eg. SHAPE object: If I have a base class SHAPE, how would I define DRAW methods for two objects CIRCLE and SQUARE


Answer1
POLYMORPHISM : A phenomenon which enables an object to react differently to the same function call.

in C++ it is attained by using a keyword virtual


Example
public class SHAPE
{
public virtual void SHAPE::DRAW()=0;
}
Note here the function DRAW() is pure virtual which means the sub classes must implement the DRAW() method and SHAPE cannot be instatiated


public class CIRCLE::public SHAPE

{

public void CIRCLE::DRAW()

{

// TODO drawing circle

}

}

public class SQUARE::public SHAPE

{

public void SQUARE::DRAW()

{

// TODO drawing square

}

}

now from the user class the calls would be like

globally

SHAPE *newShape;


When user action is to draw

public void MENU::OnClickDrawCircle(){

newShape = new CIRCLE();

}


public void MENU::OnClickDrawCircle(){

newShape = new SQUARE();

}


the when user actually draws

public void CANVAS::OnMouseOperations(){

newShape->DRAW();

}



Answer2

class SHAPE{

public virtual Draw() = 0; //abstract class with a pure virtual method

};


class CIRCLE{

public int r;

public virtual Draw() { this->drawCircle(0,0,r); }

};


class SQURE

public int a;

public virtual Draw() { this->drawRectangular(0,0,a,a); }

};


Each object is driven down from SHAPE implementing Draw() function in its own way.





What is an object?



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




How can you tell what shell you are running on UNIX system?



You can do the Echo $RANDOM. It will return a undefined variable if you are from the C-Shell, just a return prompt if you are from the Bourne shell, and a 5 digit random numbers if you are from the Korn shell. You could also do a ps -l and look for the shell with the highest PID.




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 x_dim = 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: MyPoint.pinned = true; // Compiler will issue a compile ERROR


bool isPinned = MyPoint.pinned; // no problem






What is namespace?


Namespaces allow us to group a set of global classes, objects and/or functions under a name. To say it somehow, they serve to split the global scope in sub-scopes known as namespaces.
The form to use namespaces is:
namespace identifier { namespace-body }
Where identifier is any valid identifier and namespace-body is the set of classes, objects and functions that are included within the namespace. For example:
namespace general { int a, b; } In this case, a and b are normal variables integrated within the general namespace. In order to access to these variables from outside the namespace we have to use the scope operator ::. For example, to access the previous variables we would have to put:
general::a general::b
The functionality of namespaces is specially useful in case that there is a possibility that a global object or function can have the same name than another one, causing a redefinition error.




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.





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 is Boyce Codd Normal form?


A relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F+ of the form a-> , where a and b is a subset of R, at least one of the following holds:
* a- > b is a trivial functional dependency (b is a subset of a)
* a is a superkey for schema R