C++ Programmer

  Home  C++ Programming  C++ Programmer


“C++ Programmer related Frequently Asked Questions by expert members with professional career as C++ Programmer. These list of interview questions and answers will help you strengthen your technical skills, prepare for the new job interview and quickly revise your concepts”



59 C++ Programmer Questions And Answers

21⟩ Tell us why pure virtual functions are used if they don't have implementation / When does a pure virtual function become useful?

Pure virtual functions are used when it doesn't make sense to provide definition of a virtual function in the base class or a proper definition does not exists in the context of base class. Consider the above example, class SymmetricShape is used as base class for shapes with symmetric structure(Circle, square, equilateral triangle etc). In this case, there exists no proper definition for function draw() in the base class SymmetricShape instead the child classes of SymmetricShape (Cirlce, Square etc) can implement this method and draw proper shape.

 241 views

22⟩ Explain what is 'Copy Constructor' and when it is called?

This is a frequent c++ interview question. Copy constructor is a special constructor of a class which is used to create copy of an object. Compiler will give a default copy constructor if you don't define one. This implicit constructor will copy all the members of source object to target object.

Implicit copy constructors are not recommended, because if the source object contains pointers they will be copied to target object, and it may cause heap corruption when both the objects with pointers referring to the same location does an update to the memory location. In this case its better to define a custom copy constructor and do a deep copy of the object.

class SampleClass{

public:

int* ptr;

SampleClass();

// Copy constructor declaration

SampleClass(SampleClass &obj);

};

SampleClass::SampleClass(){

ptr = new int();

*ptr = 5;

}

// Copy constructor definition

SampleClass::SampleClass(SampleClass &obj){

//create a new object for the pointer

ptr = new int();

// Now manually assign the value

*ptr = *(obj.ptr);

cout<<"Copy constructor...n";

}

 211 views

23⟩ Tell me what are C++ inline functions?

C++ inline functions are special functions, for which the compiler replaces the function call with body/definition of function. Inline functions makes the program execute faster than the normal functions, since the overhead involved in saving current state to stack on the function call is avoided. By giving developer the control of making a function as inline, he can further optimize the code based on application logic. But actually, it's the compiler that decides whether to make a function inline or not regardless of it's declaration. Compiler may choose to make a non inline function inline and vice versa. Declaring a function as inline is in effect a request to the compiler to make it inline, which compiler may ignore. So, please note this point for the interview that, it is upto the compiler to make a function inline or not.

inline int min(int a, int b)

{

return (a < b)? a : b;

}

int main( )

{

cout << "min (20,10): " << min(20,10) << endl;

cout << "min (0,200): " << min(0,200) << endl;

cout << "min (100,1010): " << min(100,1010) << endl;

return 0;

}

If the complier decides to make the function min as inline, then the above code will internally look as if it was written like

int main( )

{

cout << "min (20,10): " << ((20 < 10)? 20 : 10) << endl;

cout << "min (0,200): " << ((0 < 200)? 0 : 200) << endl;

cout << "min (100,1010): " << ((100 < 1010)? 100 : 1010) << endl;

return 0;

}

 235 views

25⟩ Tell me what is a class?

Class defines a datatype, it's type definition of category of thing(s). But a class actually does not define the data, it just specifies the structure of data. To use them you need to create objects out of the class. Class can be considered as a blueprint of a building, you can not stay inside blueprint of building, you need to construct building(s) out of that plan. You can create any number of buildings from the blueprint, similarly you can create any number of objects from a class.

class Vehicle

{

public:

int numberOfTyres;

double engineCapacity;

void drive(){

// code to drive the car

}

};

 239 views

26⟩ Explain me are you allowed to have a static const member function?

A const member function is one which isn’t allowed to modify the members of the object for which it is called. A static member function is one which can’t be called for a specific object.

Thus, the const modifier for a static member function is meaningless, because there is no object associated with the call.

A more detailed explanation of this reason comes from the C programming language. In C, there were no classes and member functions, so all functions were global. A member function call is translated to a global function call. Consider a member function like this:

void foo(int i);

A call like this:

obj.foo(10);

…is translated to this:

foo(&obj, 10);

This means that the member function foo has a hidden first argument of type T*:

void foo(T* const this, int i);

If a member function is const, this is of type const T* const this:

void foo(const T* const this, int i);

Static member functions don’t have such a hidden argument, so there is no this pointer to be const or not.

 222 views

27⟩ Please explain what is difference between shallow copy and deep copy? Which is default?

When you do a shallow copy, all the fields of the source object is copied to target object as it is. That means, if there is a dynamically created field in the source object, shallow copy will copy the same pointer to target object. So you will have two objects with fields that are pointing to same memory location which is not what you usually want.

In case of deep copy, instead of copying the pointer, the object itself is copied to target. In this case if you modify the target object, it will not affect the source. By default copy constructors and assignment operators do shallow copy. To make it as deep copy, you need to create a custom copy constructor and override assignment operator.

 260 views

28⟩ Explain me what do you mean by C++ access specifiers?

Access specifiers are used to define how the members (functions and variables) can be accessed outside the class. There are three access specifiers defined which are public, private, and protected

☛ private:

Members declared as private are accessible only with in the same class and they cannot be accessed outside the class they are declared.

☛ public:

Members declared as public are accessible from any where.

☛ protected:

Members declared as protected can not be accessed from outside the class except a child class. This access specifier has significance in the context of inheritance.

 256 views

31⟩ Tell me when you should use virtual inheritance?

While it’s ideal to avoid virtual inheritance altogether (you should know how your class is going to be used) having a solid understanding of how virtual inheritance works is still important:

So when you have a class (class A) which inherits from 2 parents (B and C), both of which share a parent (class D), as demonstrated below:

#include <iostream>

class D {

public:

void foo() {

std::cout << "Foooooo" << std::endl;

}

};

class C: public D {

};

class B: public D {

};

class A: public B, public C {

};

int main(int argc, const char * argv[]) {

A a;

a.foo();

}

If you don’t use virtual inheritance in this case, you will get two copies of D in class A: one from B and one from C. To fix this you need to change the declarations of classes C and B to be virtual, as follows:

class C: virtual public D {

};

class B: virtual public D {

};

 234 views

32⟩ Explain me what do you mean by translation unit in c++?

We organize our C++ programs into different source files (.cpp, .cxx etc). When you consider a source file, at the preprocessing stage, some extra content may get added to the source code ( for example, the contents of header files included) and some content may get removed ( for example, the part of the code in the #ifdef of #ifndef block which resolve to false/0 based on the symbols defined). This effective content is called a translation unit. In other words, a translation unit consists of

☛ Contents of source file

☛ Plus contents of files included directly or indirectly

☛ Minus source code lines ignored by any conditional pre processing directives ( the lines ignored by #ifdef,#ifndef etc)

 242 views

36⟩ Explain me what is meant by reference variable in C++?

In C++, reference variable allows you create an alias (second name) for an already existing variable. A reference variable can be used to access (read/write) the original data. That means, both the variable and reference variable are attached to same memory location. In effect, if you change the value of a variable using reference variable, both will get changed (because both are attached to same memory location).

 260 views