Pages

Friday, November 13, 2009

C++ questions: Part 5






C++ Q1:Name the operators that cannot be overloaded.
Answer:sizeof . .* .-> :: ?:


C++ Q2: What is a node class?
Answer: A node class is a class that,
Ø relies on the base class for services and implementation,
Ø provides a wider interface to te users than its base class,
Ø relies primarily on virtual functions in its public interface
Ø depends on all its direct and indirect base class
Ø can be understood only in the context of the base class
Ø can be used as base for further derivation
Ø can be used to create objects.
A node class is a class that has added new services or functionality beyond the services inherited from its base class.

C++ Q3: What is an orthogonal base class?
Answer: If two base classes have no overlapping methods or data they are said to be independent of, or orthogonal to each other. Orthogonal in the sense means that two classes operate in different dimensions and do not interfere with each other in any way. The same derived class may inherit such classes with no difficulty.

C++ Q4: What is a container class? What are the types of container classes?
Answer: A container class is a class that is used to hold objects in memory or external storage. A container class acts as a generic holder. A container class has a predefined behavior and a well-known interface. A container class is a supporting class whose purpose is to hide the topology used for maintaining the list of objects in memory. When a container class contains a group of mixed objects, the container is called a heterogeneous container; when the container is holding a group of objects that are all the same, the container is called a homogeneous container.

C++ Q5: What is a protocol class?
Answer: An abstract class is a protocol class if:
Ø it neither contains nor inherits from classes that contain member data, non-virtual functions, or private (or protected) members of any kind.
Ø it has a non-inline virtual destructor defined with an empty implementation,
Ø all member functions other than the destructor including inherited functions, are declared pure virtual functions and left undefined.

C++ Q6: What is a mixin class?
Answer: A class that provides some but not all of the implementation for a virtual base class is often called mixin. Derivation done just for the purpose of redefining the virtual functions in the base classes is often called mixin inheritance. Mixin classes typically don't share common bases.


C++ Q7: What is a concrete class?

Answer: A concrete class is used to define a useful object that can be instantiated as an automatic variable on the program stack. The implementation of a concrete class is defined. The concrete class is not intended to be a base class and no attempt to minimize dependency on other classes in the implementation or behavior of the class.

C++ Q8: What is the handle class?
Answer: A handle is a class that maintains a pointer to an object that is programmatically accessible through the public interface of the handle class.

Explanation:
In case of abstract classes, unless one manipulates the objects of these classes through pointers and references, the benefits of the virtual functions are lost. User code may become dependent on details of implementation classes because an abstract type cannot be allocated statistically or on the stack without its size being known. Using pointers or references implies that the burden of memory management falls on the user. Another limitation of abstract class object is of fixed size. Classes however are used to represent concepts that require varying amounts of storage to implement them.
A popular technique for dealing with these issues is to separate what is used as a single object in two parts: a handle providing the user interface and a representation holding all or most of the object's state. The connection between the handle and the representation is typically a pointer in the handle. Often, handles have a bit more data than the simple representation pointer, but not much more. Hence the layout of the handle is typically stable, even when the representation changes and also that handles are small enough to move around relatively freely so that the user needn’t use the pointers and the references.

C++ Q9: What is an action class?
Answer: The simplest and most obvious way to specify an action in C++ is to write a function. However, if the action has to be delayed, has to be transmitted 'elsewhere' before being performed, requires its own data, has to be combined with other actions, etc then it often becomes attractive to provide the action in the form of a class that can execute the desired action and provide other services as well. Manipulators used with iostreams is an obvious example.

Explanation:
A common form of action class is a simple class containing just one virtual function.
class Action
{
public:
virtual int do_it( int )=0;
virtual ~Action( );
}
Given this, we can write code say a member that can store actions for later execution without using pointers to functions, without knowing anything about the objects involved, and without even knowing the name of the operation it invokes. For example:
class write_file : public Action
{
File& f;
public:
int do_it(int)
{
return fwrite( ).suceed( );
}
};
class error_message: public Action
{
response_box db(message.cstr( ),"Continue","Cancel","Retry");
switch (db.getresponse( ))
{
case 0: return 0;
case 1: abort();
case 2: current_operation.redo( );return 1;
}
};
A user of the Action class will be completely isolated from any knowledge of derived classes such as write_file and error_message.

C++ Q10: When can you tell that a memory leak will occur?

Answer: A memory leak occurs when a program loses the ability to free a block of dynamically allocated memory.



C++ Q11: What is a parameterized type?

Answer: A template is a parameterized construct or type containing generic code that can use or manipulate any type. It is called parameterized because an actual type is a parameter of the code body. Polymorphism may be achieved through parameterized types. This type of polymorphism is called parameteric polymorphism. Parameteric polymorphism is the mechanism by which the same code is used on different types passed as parameters.



C++Q12: Differentiate between a deep copy and a shallow copy?

Answer: Deep copy involves using the contents of one object to create another instance of the same class. In a deep copy, the two objects may contain the same information but the target object will have its own buffers and resources. The destruction of either object will not affect the remaining object. The overloaded assignment operator would create a deep copy of objects.

Shallow copy involves copying the contents of one object into another instance of the same class thus creating a mirror image. Owing to straight copying of references and pointers, the two objects will share the same externally contained contents of the other object to be unpredictable.



Explanation:

Using a copy constructor we simply copy the data values member by member. This method of copying is called shallow copy. If the object is a simple class, comprised of built in types and no pointers this would be acceptable. This function would use the values and the objects and its behavior would not be altered with a shallow copy, only the addresses of pointers that are members are copied and not the value the address is pointing to. The data values of the object would then be inadvertently altered by the function. When the function goes out of scope, the copy of the object with all its data is popped off the stack.

If the object has any pointers a deep copy needs to be executed. With the deep copy of an object, memory is allocated for the object in free store and the elements pointed to are copied. A deep copy is used for objects that are returned from a function.


Noye: The default copy constructor and assignment operator make shallow copies. 

C++ Q13: What is an opaque pointer?

Answer: A pointer is said to be opaque if the definition of the type to which it points to is not included in the current translation unit. A translation unit is the result of merging an implementation file with all its headers and header files.




No comments: