Most Tricky C++ Interview Questions - Part 3


If you want  to move one step beyond the basic C++ questions and rock your dream interview, then I have brought an exquisite  list of the most tricky and interesting C++ interview questions that every enthusiast must know.

This will surely help to enhance your skills and boost your confidence in this language.

Now, if you are a beginner in C++ ,you might want to first understand the basic concepts of C++ programming language and then jump right back to this article to understand the content better.

Note : This article is third part of the series - Most Tricky C++ Interview Questions.

So let's get started with our exquisite list of some tricky C++ interview questions that I bet you wont wanna miss.


1. How is function overriding implemented in C++ ?

When the base class and derived class have member functions with exactly the same name, same return-type, and same arguments list (same function signature), then it is said to be function overriding.

In C++, overriding is achieved using virtual function. Declare the function to be overridden as virtual in base class( which automatically makes the function in derived class virtual too).

If we don't use virtual function then the compiler does early binding for the function and overriding does not occur. 

Virtual function is compulsory in C++ if you want to do late/dynamic binding.

Now, how does C++ implement late binding?

The compiler maintains two things to serve this purpose:

a. vtable : A table of function pointers, maintained per class (static member).

b. vptr : A pointer to vtable, maintained for each object (instance member) .


Function Overriding

                                 

As you can see the vtable contains function pointer for each virtual function which tells the version which should be called for object of this class.

So for virtual functions, compiler does late binding and at runtime it goes to the vtable of the object (via vptr) and checks the corresponding function pointer to find the right version to be called.

For non virtual functions, complier does early binding and directly checks the class type to decide the version.


2. What is deep copy and shallow copy in C++ ? 

Object created by copying data from another object is called as copy of an object.

Consider the below class which we want to copy. Base object is copied to create new Copied object.



Copying can be of two types :


Shallow Copy  :

Creating an object by copying data of all member variables of another object as it is


Shallow copy

In shallow copy, we directly copy the values of members of class . ptr in Base points to an external memory object(int variable).

If we create shallow copy of this object then the value of ptr will be copied as it is and new object will also point to the same memory location as pointed by Base object.


Deep Copy :

Creating an object by copying data of another object along with the values of memory resources which reside outside the object but handled by the object. 

Deep Copy

In deep copy, we copy the values by going deep inside the pointer variables. So we don't directly copy the value of ptr(address of Pointed object) but we go through its link and copy value of Pointed object to create a new integer variable in heap with same value as Pointed object of Base.


3. What is virtual destructor in C++ ?

Destructor is a member function of a class which is called just before when an object is about to be destroyed.

It is denoted by (tilde) symbol.

Consider this code snippet where Derived class extends Base class :


class Base{
    public:
    	~Base(){
    		cout<<"Base destructor called";
    	}
};

class Derived : public Base{
    public:
    	~Derived(){
    		cout<<"Derived destructor called";
    	}
};

int main(){	
	Base *obj = new Derived;
	delete obj;
}

Output :
Base destructor called

'obj' is a pointer of type Base* which points to an object of type Derived.

In case you dont know about delete operator, delete is used to free the memory dynamically allocated using new keyword(similar to what free does in C)

We got the output as "Base destructor called ".Why ? 
This is because in case of delete operator, the compiler does early binding. So it checks at compile time that obj is of type Base*.Therefore at runtime Base destructor will be called. But we want to call the destructor of Derived before Base to properly release the resources.
To reslove this, we make the destructor virtual in class Base. So compiler will see it and decide to do late binding(at runtime).
When we call delete *obj, then it checks the type of object pointed by *obj, thus calling constructor of Derived followed by that of Base.

class Base{
    public:
    	virtual ~Base(){
    		cout<<"Base destructor called\n";
    	}
};

class Derived : public Base{
	public:
    	~Derived(){
    		cout<<"Derived destructor called\n";
    	}
};

int main(){
	Base *obj = new Derived;
	delete obj;
}

Output :
Derived destructor called
Base destructor called

4. What is Diamond problem in C++ ?

See the diagram below to understand the diamond problem.



Diamond problem


Class B and C extends class A and class D extends both class B and class C ( multiple inheritance). 
Class A has a member int var which is inherited by both B and C, so when D inherits from B and C there will be two copies of 'var' in class D and this causes ambiguity. Also note that constructor and destructor of A will be called two times when we create object of class D. This situation is referred to as diamond problem as this is a diamond structure.

Now, if you create object of class D there will be no error.
But if we try to access 'var' member in D , then there arises ambiguity and thus causes error in the program.

So if we write

int main(){
	D object;			// no error
	cout<<object.a; 	// causes error
}

What's the solution to diamond problem ?
Use virtual inheritance (using virtual keyword) as shown below.

class A{
	public:
		int var;
};

class B : virtual public A{ };

class C : virtual public A{ };

class D : public B, public C{ };

int main(){ }
Now, there will be only one copy of 'var' for a D object and also the constructor and destructor of A will only be called once for an object of class D.


5. How will you convert a primitive type to class type object and vice versa in C++ ?

Type conversion between class type and primitive type cannot be performed directly. That will lead to error.

a. Primitive type to class type ( Copy int value in class object)


class CodingWithArt{
	int a;
};

int main(){
	CodingWithArt obj = 5; 		//error
}

This code will not compile as direct conversion is not allowed.

Define a constructor as below to perform type conversion.

class CodingWithArt{
	int a;
	public:
		CodingWithArt( int var ){
			a = var;
		}

};

int main(){
	CodingWithArt obj(5);

	obj = 20;		// similar to obj(20), thus calls the constructor
}

This program works just fine because this syntax is valid in C++ and obj = 20 is similar as obj(20) and thus this directly calls constructor of the class.

b. Class type to primitive type


class CodingWithArt{
	int a;
	public:
		CodingWithArt( int var ){
			a = var;
		}

};

int main(){	
	CodingWithArt obj(5);
	int var = obj; 		// error 
}

This code will not compile as direct conversion is not allowed.

Use casting operator to perform type conversion.

class CodingWithArt{
	int a;
	public:
		CodingWithArt( int var ){
			a = var;
		}

		operator int(){		// Casting operator for int
			return a;
		}

};

int main(){	
	CodingWithArt obj(5);
	int var = obj; 		// calls the casting operator and actually performs var = a
	cout<<var;			// prints "5"
}

Casting operator is used to convert a class type to primitive type. We define casting operator in class CodingWithArt for type 'int'. 
Thus the statement "var = obj" calls the casting operator which returns the value of integer member variable 'a' and this gets assigned to 'var'.


This brings us to the end of Part-3 of this series. Also check out the other parts in the same series to find more challenging C++ interview questions :

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

Happy learning!!

1 comments:

Click here for comments
Selena_
admin
October 28, 2020 at 12:22 PM ×

Really helpful.Thank u so much...u r doing great work

Reply
avatar