Posts

Showing posts from 2022

Advance Interview Ask In C++ Interview #4

  How to stop class inheritance in C++ with condition that object creation should be allowed Answer includes multiple solutions to stop or prevent class inheritance in C++ with condition that object creation of the class should be allowed with C++ program example and explanation. Interview Question: I want to stop a class to be inherited and allow to create an object of the class. Design a solution for this problem statement. Give as many solutions as you can. Answer: We can apply 3 solutions to prevent a class to be inherited in C++ where object creations will be allowed. Solution-1: Use the final keyword (from C++11) In the below example, we’ve made the Unique class final. So, it’ll not be inherited by any class. If you try to inherit it, the compiler will flash an error that “ a final class cannot be used as a base class “. Note that you can create an object of the final class as show in the main() method here. #include <iostream> using namespace std; class Unique final...

Advance Interview Ask In C++ Interview #3

  How to delete array of objects in C++? Proof by C++ code for proper deletion Answer includes how to delete array of objects in C++ created dynamically with C++ code example with proof. In other words, delete array of pointers to objects in c++. Interview Question: Write C++ code to create an array of objects using new keyword and delete these objects. Also, proof that all the objects are deleted properly. Answer: We know that when we create an object of a class dynamically from heap memory using new keyword, then we must delete it explicitly to avoid memory leaks in C++ programs after we are done with its operations. Let’s take below class Pen as an example. class Pen { public: Pen() { cout << "Constructor..." <<endl; } ~Pen() { cout << "Destructor...!" <<endl; } void write(){ cout << "Writing...!" <<endl; } }; Below is an example, how we create an object ...

Advance Interview Ask In C++ Interview #2

  What is virtual destructor in C++? Why do we need virtual destructor? Answer includes what is virtual destructor in C++ with example and use of it or why do we need virtual destructor in C++ object oriented programming. Note that in an interview, if it asked about virtual destructors then don’t forget to answer when to use virtual destructor in C++ class. Also, note that there is no  concept of virtual constructor in C++ programming. Answer:  Virtual destructors maintains the hierarchy of calling destructors from derived to base class. A class must have a virtual destructor if we hit below criteria. Derived class object using a pointer to base class and deleting base class pointer. For example Base * p = new Derived (); // up casting Delete P; If we don’t make base class destructor virtual, it will not call derived class destructor on above mentioned criteria. For example class Base{ public: ~Base(){cout<<"Base class destructor..."<<"\n";} //not virt...

Advance Interview Ask In C++ Interview #1

  A good interview question based on a C++ polymorphism in oops C++ Technical Interview Question on polymorphism in oops Write a complete class stating function overriding feature in C++ Show the function call in main program. Explain the concept of function overriding. Take example of drawing multiple shapes e.g. circle and rectangle etc. Interviewer Intent: You know the concept and syntax of function overriding in C++ you can write class and program. If you follow best coding practice and the things you care when write the code. NOTE:  As per question criteria, we will write code using interface, but, to describe the concept a basic example will be covered first. Answer: Function overriding concept is run time polymorphism in oops, in which functions get resolved on run time using VTABLE (Virtual table) by compiler. If we have definition /declaration of a function in base class and want to have a definition of same function name in derive class and want to call derived funct...