C++ Programming

  Home  Computer Programming  C++ Programming


“Learn C++ Programming by C++ Interview Questions and Answers”



120 C++ Programming Questions And Answers

41⟩ What is public, protected, private in C++?

* Public, protected and private are three access specifiers in C++.

* Public data members and member functions are accessible outside the class.

* Protected data members and member functions are only available to derived classes.

* Private data members and member functions can’t be accessed outside the class. However there is an exception can be using friend classes.

 170 views

44⟩ Tell how to check whether a linked list is circular.

Create two pointers, each set to the start of the list. Update each as follows:

while (pointer1) {

pointer1 = pointer1->next;

pointer2 = pointer2->next; if (pointer2) pointer2=pointer2->next;

if (pointer1 == pointer2) {

print ("circularn");

}

}

OK, why does this work?

If a list is circular, at some point pointer2 will wrap around and be either at the item just before pointer1, or the item before that. Either way, it’s either 1 or 2 jumps until they meet.

 130 views

45⟩ What is virtual constructors/destructors?

Answer1

Virtual destructors:

If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is called on the object.

There is a simple solution to this problem declare a virtual base-class destructor.

This makes all derived-class destructors virtual even though they don’t have the same name as the base-class destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator to a base-class pointer to a derived-class object, the destructor for the appropriate class is called. Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error.

Answer2

Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching the pointer type) is called on the object.

There is a simple solution to this problem – declare a virtual base-class destructor. This makes all derived-class destructors virtual even though they don’t have the same name as the base-class destructor. Now, if the object in the hierarchy is destroyed explicitly by applying the delete operator to a base-class pointer to a derived-class object, the destructor for the appropriate class is called.

 137 views

46⟩ What is the difference between an ARRAY and a LIST?

Answer1

Array is collection of homogeneous elements.

List is collection of heterogeneous elements.

For Array memory allocated is static and continuous.

For List memory allocated is dynamic and Random.

Array: User need not have to keep in track of next memory allocation.

List: User has to keep in Track of next location where memory is allocated.

Answer2

Array uses direct access of stored members, list uses sequencial access for members.

//With Array you have direct access to memory position 5

Object x = a[5]; // x takes directly a reference to 5th element of array

//With the list you have to cross all previous nodes in order to get the 5th node:

list mylist;

list::iterator it;

for( it = list.begin() ; it != list.end() ; it++ )

{

if( i==5)

{

x = *it;

break;

}

i++;

}

 130 views

47⟩ 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> function_declaration; template <typename indetifier> function_declaration;

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.

 143 views

49⟩ Define a constructor - What it is and how it might be called (2 methods).

Answer1

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:

1) Implicitly: automatically by complier when an object is created.

2) Calling the constructors explicitly is possible, but it makes the code unverifiable.

Answer2

class Point2D{

int x; int y;

public Point2D() : x(0) , y(0) {} //default (no argument) constructor

};

main(){

Point2D MyPoint; // Implicit Constructor call. In order to allocate memory on stack, the default constructor is implicitly called.

Point2D * pPoint = new Point2D(); // Explicit Constructor call. In order to allocate memory on HEAP we call the default constructor.

 122 views

50⟩ You have two pairs new() and delete() and another pair alloc() and free(). Explain differences between eg. new() and malloc()

Answer1

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.

 138 views

51⟩ 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.

 196 views

52⟩ 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.

 128 views

54⟩ 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

 153 views

55⟩ What is a C++ object?

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

 150 views

56⟩ What is Boyce Codd Normal form in C++?

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

 156 views

57⟩ 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.

 145 views

59⟩ What is virtual class and friend class?

Friend classes are used when two or more classes are designed to work together and need access to each other's implementation in ways that the rest of the world shouldn't be allowed to have. In other words, they help keep private things private. For instance, it may be desirable for class DatabaseCursor to have more privilege to the internals of class Database than main() has.

 147 views