Xtreme CPP - Answers - Part I

1) A constructor of a class can not be overloaded.

But a destructor can be overloaded. It should be overloaded unless the class is a trivial one.

The purpose of destructor is to clean up the members of the class. Destructor should release any resources the class has acquired, close the files etc.

Consider this example.

BaseClass *obj = new DerivedClass();

Here when delete is  called for obj, only destructor of BaseClass is called. And this will stop any resources acquired from DerivedClass being released. So it is necessary that the destructor in BaseClass be made virtual.

If destructor is virtual, delete will call DerivedClass destructor and since derived class destructor will call base class destructor, BaseClass destructor will also be called.

2) A default constructor is the one which can be called without any arguments.

It can be a constructor which takes no arguments or where arguments are provided default values.

Whenever a class has no user defined constructors, the compiler generates automatically the default constructor. If the class has no base class, then this constructor is trivial. But if it is a derived class, then it calls default constructor of base class(es).

class A
{
      int m,n;
 };

Here compiler provides a default constructor which looks like
A::A(){}

User can write default constructor in the following two ways


 class A  
 {  
   int m,n;  
 public:  
 A()/*default constructor*/  
 {  
   m=n=0;  
 }  
 } ;  

 OR

 class B  
 {  
    int p,q;  
  public:  
    B(int j=0,int k=0);  
 };  
 B::B(int j,int k)  
 {  
   p = j; q =k;  
 }  
As you can see, the default constructor can have parameters, but they must have default values. 
When a class has parameterized constructors, compiler does not provide a default constructor. 

3)A copy constructor lets you create an object by copying contents of another object. New object will be exact copy of old object.

Copy constructor will also be created by the compiler by default. This will create a shallow copy of object.

But to provide deep copy, you can write your own copy constructor with the following syntax

 A::A(A&obj)  
 {  
    /*write your member copying code here*/  
 }  
Note the reference parameter to the constructor. You should use reference parameter because, copy constructor is also used whenever an object is sent to a function as parameter by value. If we write obj as a value parameter, then the constructor will be called recursively infinite number of times.

4) A reference parameter is funny because it looks like a value parameter but behaves like a pointer parameter.

Let us understand what is a reference variable. Reference variable is another name - an alias of a variable. Once it is defined with initialization, it always refers to same variable.

int a = 10;
int &b=a;

Now b is a reference variable and is an alias of a. So at any point of time, b and a have same value  because they are two names of same memory location.

Now reference parameters - You have seen swap function with value parameters - which does not work, with pointer parameters - which is clumsy and often dangerous. But swap with reference parameters is the best
 void swap(int &a,int &b)  
 {  
   int temp =a; a=b;b=a;  
 }  
It this swap is called as

int x=3,y=4;
swap(x,y);

swap function gets x and y only because a and be will be aliases of x and y. We change a and b, x and y are also changed.  Great!!

If you were to write swap function using pointer parameters, it would look like this
 void swap(int *a,int *b)  
 {  
    int temp = *a; *a = *b; *b = temp;  
 }  
and called as

swap(&x,&y);



Comments

Popular Posts