C++ Part3

Explain the ISA and HASA class relationships. How would you implement each in a class design?

A specialized class “is” a specialization of another class and, therefore, has the ISA relationship with the other class.
This relationship is best implemented by embedding an object of the Salary class in the Employee class.

When is a template a better solution than a base class?

When you are designing a generic class to contain or otherwise manage objects of other types, when the format and behavior of those other types are unimportant to their containment or management, and particularly when those other types are unknown (thus, the generality) to the designer of the container or manager class.

What are the differences between a C++ struct and C++ class?

The default member and base-class access specifies are different.
This is one of the commonly misunderstood aspects of C++. Believe it or not, many programmers think that a C++ struct is just like a C struct, while a C++ class has inheritance, access specifies, member functions, overloaded operators, and so on. Actually, the C++ struct has all the features of the class. The only differences are that a struct defaults to public member access and public base-class inheritance, and a class defaults to the private access specified and private base-class inheritance.

How do you know that your class needs a virtual destructor?

If your class has at least one virtual function, you should make a destructor for this class virtual. This will allow you to delete a dynamic object through a baller to a base class object. If the destructor is non-virtual, then wrong destructor will be invoked during deletion of the dynamic object.

What is the difference between new/delete and malloc/free?

Malloc/free do not know about constructors and destructors. New and delete create and destroy objects, while malloc and free allocate and deallocate memory.

What happens when a function throws an exception that was not specified by an exception specification for this function?

Unexpected() is called, which, by default, will eventually trigger abort().

Can you think of a situation where your program would crash without reaching the breakball, which you set at the beginning of main()?

C++ allows for dynamic initialization of global variables before main() is invoked. It is possible that initialization of global will invoke some function. If this function crashes the crash will occur before main() is entered.

What issue do auto_ptr objects address?

If you use auto_ptr objects you would not have to be concerned with heap objects not being deleted even if the exception is thrown.

Is there any problem with the following:
char *a=NULL; char& p = *a;?

The result is undefined. You should never do this. A reference must always refer to some object.

Posted in C/C++. 3 Comments »

C++ Part1

Is it possible to have Virtual Constructor? If yes, how?If not, Why not possible ?

There is nothing like Virtual Constructor.
The Constructor cant be virtual as the constructor is a code which is responsible for creating a instance of a class and it cant be delegated to any other object by virtual keyword means.

What about Virtual Destructor?

Yes there is a Virtual Destructor. A destructor can be virtual as it is possible as at runtime depending on the type of object baller is balling to , proper destructor will be called.

What is Pure Virtual Function? Why and when it is used ?

The abstract class whose pure virtual method has to be implemented by all the classes which derive on these. Otherwise it would result in a compilation error.
This construct should be used when one wants to ensure that all the derived classes implement the method defined as pure virtual in base class.

What is problem with Runtime type identification?

The run time type identification comes at a cost of performance penalty. Compiler maintains the class.

How Virtual functions call up is maintained?

Through Look up tables added by the compile to every class image. This also leads to performance penalty.

Can inline functions have a recursion?

Syntax wise It is allowed. But then the function is no longer Inline. As the compiler will never know how deep the recursion is at compilation time.

How do you link a C++ program to C functions?

By using the extern “C” linkage specification around the C function declarations.
Programmers should know about mangled function names and type-safe linkages. Then they should explain how the extern “C” linkage specification statement turns that feature off during compilation so that the linker properly links function calls to C functions.

Explain the scope resolution operator?

It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope.

How many ways are there to initialize an int with a constant?

1. int foo = 123;
2. int bar(123);

Posted in C/C++. 7 Comments »

Hot Interview Question – C++ (Constructor)

C++ Interview Question:

1.     Does a class provide default Copy Constructor?
Yes! The default copy constructor is being provided automatically by the compiler if not implemented separately. In this, it puts the code for coping the data members and other variables that stay in stack. If something is being created by malloc or new in the heap, those are not being copied by the default copy constructor provide by the compiler.

2.     What does a default Copy Constructor do?
A default copy constructor is being provided by the compiler, when an attempt to copy an existing object is made. In this case the control goes to the default copy constructor. It generates a new object, and makes the values of data members of the new object which are in the stack, same as the parent object. It doesn’t copy the variables that are created in the heap. Simply speaking, a compiler supplied default copy constructor doesn’t take care of the things in an object, that are being created using malloc/calloc or new.
3.     Can a program have a virtual constructor?
Generally we don’t need an overridable constructor. So constructor should not be declared as virtual. But in a class, we can make a static method, which will call the private/protected constructor and create an object. In that case the constructor is called as virtual constructor.

4.     When does a programmer need to implement his own copy constructor?
Though compiler automatically provides the default copy constructor, sometime a programmer needs to implement his own copy constructor. We can take up a case here:

In the default constructor some memory allocation has been done for few data members and hence those will be created in the heap. In the destructor corresponding de-allocation code is there. Now if an attempt to copy an object is made, it won’t call the default constructor but it will call the default copy constructor which will copy the data member variable from stack and copy the pointers but won’t allocate any new memory space for the new copied object. So same pointers will exist in both the parent and copied object. This will not only create a great ambiguity but runtime error will occur when attempts will be made to delete both the objects. First object will get deleted properly. When it will try to delete the other object, the common pointer won’t exist and runtime error will come. Even if user stops the application it will try to delete all the objects in the memory and runtime error will occur. In a single word, copy constructor is needed to be implemented independently, when the parent object has some allocated memory in heap for some of its data member and that gets deleted by destructor. Copy constructor should consist that much of code for memory allocation for the newly created object.Apart from this, programmer can implement his own copy constructor to copy any other things, which he wants specifically.

 5.      Can a copy constructor accept an object of the same class as parameter, instead of reference of the object?
No. It is specified in the definition of the copy constructor itself. It should generate an error if a programmer specifies a copy constructor with a first argument that is an object and not a reference.

Logically thinking, if we can consider the same as copy constructor also, big confusion will come. If we pass an object as a function parameter, by value, it will get copied first and then be passed to the function. At the time of getting copied it should call the copy constructor. So in this case, if we consider the above described constructor as copy constructor, at the very beginning of the function call, it wil attempt to copy the object and hence again call the same function and hence will go towards an infinite loop. This will probably be ended up with an unexpected stack overflow.

It will give compiler error if an object of the same class is being passed to a constructor, but it doesn’t give error if more than one different objects are being passed to the constructor . In this case it doesn’t treat it as copy constructor, but a normal overloaded constructor.

6.     What is the return parameter of a constructor and why?

Constructor in never being called directly. It is being called automatically by the compiler when an object in being created (or copied). Hence it can’t return any parameter. Logically thinking, if it would have been made to return a parameter, how do we put the code to create an object?

7.     Are the “default constructor” and “constructor with default parameter” same?
Default constructor is a constructor, which can be called with no argument. So a constructor with all the parameters as default argument can be called as default constructor. A constructor with one or more default parameters (but not all the parameters) can be called “constructor with default parameter” but that won’t be the default constructor.

If a constructor with no argument and a constructor with all default arguments are being implemented then object-creation will generate an ambiguity regarding which constructor is to be called.

The Hot C++ Interview Question Asked

The Hot C++ Interview Question: 

How do you rank your C++ skills on a scale of 1 to 10?

This is often the first question you will hear on an interview for a C++ contract. You will be tempted to rate yourself high, and you should. This is your chance to convince the client that you are just what he is looking for–an assertive and knowledgeable professional who will be productive either working on a team or on your own. Naturally, though, you should be able to support the ranking you gave yourself by doing well on the interview.

Q1. Is there anything you can do in C++ that you cannot do in C?

A1. No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C.

Q2. What is the difference between C++ structure and C++ class?

A2. The default access level assigned to members of struct is public while the default access level assigned to a class is private.

Q3. What is encapsulation? A3. Encapsulation is welding of code and data together into objects.

Q4. What is inheritance?

A4. Inheritance is a mechanism through which a subclass inherits the properties and behavior of its superclass.

Q5. What is polymorphism?

A5. In Greek this means “many shapes.” As a consequence of inheritance and virtual functions, a single task (for example, drawing a geometrical shape) can be implemented using the same name (like draw()) and implemented differently (via virtual functions) as each type in object hierarchy requires(circle.draw() or rectangle.draw()). Later, when a polymorphic object (whose type is not known at compile time) executes the draw() virtual function, the correct implementation is chosen and executed at run time.

Q6. What would you say if you saw “delete this” while reviewing your peer’s code?

A6. You should never do this. Since compiler does not know whether the object was allocated on the stack or on the heap, “delete this” could cause a disaster.

Q7. What is the difference between public, protected, and private members of a class?

A7. Private members are accessible only by members and friends of the class. Protected members are accessible by members and friends of the class and by members and friends of derived classes. Public members are accessible by everyone.

Q8. What is the difference between non-virtual and virtual functions?

A8. The behavior of a non-virtual function is known at compile time while the behavior of a virtual function is not known until the run time.

Q9. What is a pure virtual function?

A9. “A pure virtual function is a function declared in a base class that has no definition relative to the base.”

Q10. What is an abstract base class?

A10. It is a class that has one or more pure virtual functions.