Write about the issues related to the runtime type identification.
A Runtime type identification or RTTI is a collection of technologies that allows you to identify an item’s type at runtime, which is very useful when you deal with runtime polymorphism.
The dynamic cast, typeid operator, and the type_info structure are the three components of RTTI.
Runtime polymorphism has added these items in C++, but that utility comes at a cost.
The dynamic_cast operator is more flexible. For using the dynamic_cast, the instantiation of an object is must. Furthermore, dynamic_cast works only with pointers and references to polymorphic class objects; there must be atleast one virtual function somewhere in the class hierarchy.
Explain polymorphism with the help of an example
Polymorphism is defined as the exhibition of a unique behavior or response by different objects when responding to the same message. Pointers to base class objects can also be used to point to derived class objects (although you cannot reference members in the derived class that are not in the base class when you use a base class pointer).
For Example:
Teacher and Doctor classes derived from the base Professional class exhibits different behavior when a member function is called for one of their objects through a pointer or reference to the base Professional class. The Doctor class in our example assumes the existence of only one kind of doctor. In reality, a Professional class can employ doctors from various streams: cardiologist, dentist, eye specialist. Doctors can have different abilities and react differently in different situations. The company would recruit the number of doctors according to its requirements. For example, if there is a requirement of only one cardiologist, then the company would appoint only one cardiologist.
Describe the setting up of my member functions to avoid overriding by the derived class.
To avoid overriding of the member functions by the derived class, the leaf method is used. With the help of the leaf method, it is possible to leave the code unaligned at the time of execution by simply adding a comment next to the method. This method is easy, fast, and inexpensive to use.
Which should be more useful: the protected and public virtuals?
Public virtuals are more preferable than protected virtuals. Public virtuals permit a program to directly modify the values of data members. It can be accessible to any function that is within the scope of the structure. It translates the implementation for the user of the class. It is the most common, convenient, and easiest way to implement by any of the programs.
On the other hand, protected virtual functions are used for hiding some methods or data members from the outside class making it consistent and symmetrical in its approach. It is condition specific, so not commonly used.
Write about the relationship between the private and protected inheritance.
a When you derive a base class using protected access specifier, the private members of the base class become protected members of the derived class (the private members of the base class are not available to the derived class). Protected inheritance is a good technique to use when you derive a class based on a class that was not written for inheritance because it converts public members of the base class, which you might not want to expose to the protected members of the derived class.
Note:: that in case of private inheritance, only members of a base class can access private members of that base class. While in the case of protected inheritance, both members of its base class as well as its derived class can access the protected members of that base class.
Write about private inheritance.
When you use private inheritance, the public and protected members of the base class become private members of the derived class. In fact, private inheritance implements a has-a relationship more than a is-a relationship.
Write about pure virtual member function.
You can leave a base class method unimplemented if you make it a pure virtual method. A class’s member function declaration includes an equal sign followed by a zero (=0).
This means that the base class is an abstract base class and the class designer intends the class to be used only as a base class. The base class may or may not provide a function body for the pure virtual function. In either case, a program that uses this class may not directly declare any objects of an abstract base class. If the program declares an object of a class directly or indirectly derived from the abstract base class, the pure virtual function must be overridden explicitly.
Let’s take an example. There is an animal class and you need to make the breathe function in the animal class as a pure virtual function:
#include <iostream.h> using namespace std; class animal { public: virtual void breathe () = 0 // 0 means it is "Pure Virtual" };
Now, the animal class is an abstract class and you cannot create objects of this class. You can only use it as a base class. To create a non-abstract derived class from this class, you must override the breathe function, as follows:
#include <iostream.h> #include <conio.h> class animal { public: virtual void breathe () = 0; }; class fish: public animal { public: void breathe (); }; void fish::breathe() { cout << "Building_" << endl; } int main() { fish bigfish; bigfish.breathe(); getch(); return 0; }
In the preceding code snippet, the animal and fish classes each have a different version of a function named breathe. That means that pointer->breathe calls animal::breathe when pointer points to an animal object, but it calls fish::breathe when pointer points to a fish object.
Write about abstract base classes.
An abstract base class is a class definition that is always a base class for other classes to be derived from. No specific objects of the base class are declared by the program. A C++ abstract base class is one that has a pure virtual function, a protected constructor, or a protected destructor.
At the design level, you create a pure virtual method by using = 0 in place of a method body. This notation specifies that the member function is a pure virtual function. This means that the base class is an abstract base class, and the class designer intends the class to be used only as a base class. The base class may or may not provide a function body for the pure virtual function. In either case, a program that uses this class may not directly declare any objects of the abstract base class. If the program declares an object of a class directly or indirectly derived from the abstract base class, the pure virtual function must be overridden explicitly.
Memory Allocation interview Questions and Answers List
- How new/delete differs from malloc()/free?
- How the delete operator differs from the delete[]operator?
- How a new operator differs from the operator new?
- Explain the term memory alignment.
- Explain the concept of dynamic allocation of memory.
- In the expression, char *c = (char *)malloc(20); followed by free(r); explain the role of mallocQ operator in allocating the number of bytes?
- Is it possible to use the free operator with the new allocated pointers and the delete operatorQ with the
malloc allocated pointers? - Can a new be used in place of old mallocQ? If yes, why?
- Is it possible to use a new for the reallocation of pointers ?
- Is there any need for checking of NULL after executing p = new Animal ()?
- Can a NULL be checked before deleting the p variable froi the expression, Delete p; and not if (p != NULL) ?
- Enlist the processes involved in deleting a variable.
- On throwing an exception by the Animal constructor in p = new AnimalQ, can memory leak occur?
- What is the use of [] in the delete statement of the following expression:
- What would happen on forgetting [], while deallocating an array through new?
- Can [] be dropped on deletion of some built-in type array (char, int etc)?
- Write about the retrieval of n number of objects during the process of delete[]p?
- Is it possible for a member function to use delete this?
- Discuss the effects occur, after an exception thrown by a member function is unspecified by an exception specification.
- What is the role of copy constructor in copying of thrown objects?
- How can an improvement in the quality of software be done by try/catch/throw?
- Explain the process of catching a variable.