Explain the need for “Virtual Destructor”.
Dec 9th
In case of inheritance, objects should be destructed exactly the opposite way of their construction. If virtual keyword is not added before base class destructor declaration, then derived class destructor will not at all be called. Hence there will be memory leakage if allocated for derived class members while constructing the object.
Difference between a “assignment operator” and a “copy constructor”
Dec 9th
Copy constructor is called every time a copy of an object is made. When you pass an object by value, either into a function or as a function’s return value, a temporary copy of that object is made.
Assignment operator is called whenever you assign to an object. Assignment operator must check to see if the right-hand side of the assignment operator is the object itself. It executes only the two sides are not equal
What are the types of STL containers?
Dec 9th
deque hash_map hash_multimap hash_multiset hash_set list map multimap multiset set vector
Difference between “vector” and “array”?
Dec 9th
Vector and Array List are very similar. Both of them represent a ‘grow able array’, where you access to the elements in it through an index. Array List it’s part of the Java Collection Framework, and has been added with version 1.2, while Vector it’s an object that is present since the first version of the JDK. Vector, anyway, has been retrofitted to implement the List interface. The main difference is that Vector it’s a synchronized object, while Array List it’s not.While the iterator that are returned by both classes are fail-fast (they cleanly thrown a ConcurrentModificationException when the original More >
Explain “passing by value”, “passing by pointer” and “passing by reference”
Dec 9th
There is major difference between these three are when we want to avoid making the copy of variable and we want to change value of actual argument on calling function. There are we use passing by pointer, passing the reference. We can not perform arithmetic operation on reference.
Have you heard of “mutable” keyword?
Dec 9th
The mutable keyword can only be applied to non-static and non-const data members of a class. If a data member is declared mutable, then it is legal to assign a value to this data member from a const member function.
SEE FOLLOWING CODE :- ******************************************** class Mutable { private : int m_iNonMutVar; mutable int m_iMutVar; public: Mutable(); void TryChange() const; }; Mutable::Mutable():m_iNonMutVar(10),m_iMutVar(20) {}; void Mutable::TryChange() const { m_iNonMutVar = 100; // THis will give ERROR m_iMutVar = 200; // This will WORK coz it is mutable }
In c++ have a default constructor?
Dec 9th
Yes C++ does have a default constructor provided by the compiler. In this case all the members of the class are initialized to null values. These values act as the default values. For eg: My Class me; In the above case since the object is not initialized to any value so the default constructor will be called which will initialize the class with the default values.
What is the use of virtual destructor?
Dec 9th
virtual destructor is very useful….everyone should use that……if there is no any strong reason for not using virtual destructor….like…One class having two char variable………..so it’s size is two byte……..if u use virtual destructor it’s size will be 6 bytes….4 byte for virtual ptr….Now if this class have 1 millions objects…so 4 magabyte memory will be lost…where all ptr do the same thing…..
Whether there will be a default contructor provided by the compiler in above case ?
Dec 9th
yes, if the designer of the class donot define any constructor in the class. then the compiler provides the default constructor in the class.
