|
CPP FAQ
    
1. What is a copy constructor? Explain with examples.
* Copy constructor is a constructor function with the same name as the class used to make deep copy of objects.
There are 3 important places where a copy constructor is called.
· When an object is created from another object of the same type
· When an object is passed by value as a parameter to a function
· When an object is returned from a function
2. What is polymorphism? What is the significance of polymorphism in terms of classes?
* Polymorphism enables similar methods and operators to have a different behavior. Polymorphism can be achieved in C++ with the help of function and operator overloading. Base class object’s pointer can be used to invoke methods in the derived class.
3. What are virtual functions?
* When derived class overrides the base class method by redefining the same function, then if client wants to access derived class method through base class pointer, then we must define the base class method as virtual.
The main advantage of virtual function is dynamic linking. During the runtime, the methods can be changed.
4. What is the difference between an interface and abstract class?
* An abstract class is a special kind of class that cannot be instantiated. However, other classes can be derived from this class. This enables all derived classes to have same hierarchies or standards.
* Interface is not a class. It provides definition of methods without body. As is the case with abstract class, this also enables all derived classes to have same hierarchies or standards.
5. Explain the differences in an interface and an abstract class, and reasons to use one or the other.
* Interface is a contract between a consumer and a provider, and should remain fixed throughout the life of the application because changes to an interface require changes to both consumer and provider code. An interface only contains methods; no fields, and cannot be instantiated.
* An abstract class should contain basic functionality that may be inherited or overridden by derived / child classes. An abstract class may have methods and fields, but some of the methods may be abstract (must be implemented by derived classes) while others are virtual and provide a default implementation that may be overridden in derived classes. Abstract classes may house common functionality that is shared in all derived classes.
* The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class.
6. What is the difference between new () and malloc () methods?
* Operator new automatically calculates the size of the object that it constructs. Conversely, with malloc (), the programmer has to specify explicitly the number of bytes that have to be allocated. In addition, malloc () returns void *, which has to be explicitly cast to the desired type. This is both tedious and dangerous. Operator new returns a pointer to the desired type, so no explicit typecast is required.
* new and delete automatically construct and destroy objects. malloc() and free(), on the other hand, merely allocate and deallocate raw memory from the heap. delete can be overloaded, free cannot. delete invokes the destructor of the object to be deallocated, free does not do this.
In summary, here are the differences.
1. Operator new constructs an object (calls constructor of object), malloc does not.
2. Operator new is an operator, malloc is a function.
3. Operator new can be overloaded, malloc cannot be overloaded.
4. Operator new throws an exception if there is not enough memory, malloc returns a NULL.
5. Operator new[] requires to specify the number of objects to allocate, malloc requires to specify the total number of bytes to allocate.
6. malloc() returns void *, which has to be explicitly cast to the desired type but new returns the proper type.
7. Operator new/new[] must be matched with operator delete/delete[] to deallocate memory, malloc() must be matched with free() to deallocate memory.
8. The new/delete couple does not have a realloc alternative that is available when malloc/free pair is used. realloc is used to resize the length of an array or a memory block dynamically.
7. What are virtual constructors and virtual destructors?
* There are no virtual constructors in C++. Virtual destructors are required to avoid memory leaks while deleting the base class pointer when derived class object is created using base class pointers.
* The order of execution of destructor in an inherited class during a clean up is like this.
1. Derived class destructor
2. Base class destructor
#include "iostream.h"
class Base
{
public:
Base(){ cout<<"Constructor: Base";}
~Base(){ cout<<"Destructor : Base";}
};
class Derived: public Base
{
//Doing a lot of jobs by extending the functionality
public:
Derived(){ cout<<"Constructor: Derived";}
~Derived(){ cout<<"Destructor : Derived";}
};
void main()
{
Base *Var = new Derived();
delete Var;
}
|
Try executing this code; you'll see the difference. To our observation, the constructors are getting called in the proper order. But to the dread of a programmer of a large project, the destructor of the derived class was not called at all.
This is where the virtual mechanism comes into our rescue. By making the Base class Destructor virtual, both the destructors will be called in order. The following is the corrected sample.
#include "iostream.h"
class Base
{
public:
Base(){ cout<<"Constructor: Base";}
virtual ~Base(){ cout<<"Destructor : Base";}
};
class Derived: public Base
{
//Doing a lot of jobs by extending the functionality
public:
Derived(){ cout<<"Constructor: Derived";}
~Derived(){ cout<<"Destructor : Derived";}
};
void main()
{
Base *Var = new Derived();
delete Var;
}
|
8. What is pure virtual function?
* When you define only function prototype in a base class without implementation and do the complete implementation in derived class. This base class is called abstract class and client won't able to instantiate an object using this base class.
|