My Pages

Thursday, July 16, 2020

FAQ: Can a Copy Constructor have more than one parameters?


Ans: YES.

A Copy Constructor can have additional parameters, however, the first parameter must always be a reference to the object being initialized, and the subsequent parameters must have default arguments defined for them.

Here is an example:

       

    //Copy Constructor with more than one arguments
    MyArray (const MyArray &a, int s=0){
        int i;
        cout<<"Copy Constructor with additional parameter, called! for obj at address:" << &*this <<"\n";
        ptr = new int[a.size];
        
        for(i=0;i < a.size;i++)
            ptr[i]=a.ptr[i];
    }

       
 

The additional param int s=0 is perfectly fine, and it is up to the user how he wants to make a use of the additional one. The compiler will not give any error or warning with this copy constructor.

Here is the entire program code usage:

       
#include < iostream>

using namespace std;

class MyArray{
    int *ptr;
    int size;
    
    public:
    MyArray(){
        cout << "Parameterless Constructor called! for obj at address: "<<&*this <<"\n";
    }
    MyArray(int s){
        cout << "Parameterized Constructor called! for obj at address: "<<&*this<<", size: " << s<<"\n";
        ptr = new int[s];
        this->size = s;
    }
    //Copy Constructor with more than one arguments
    MyArray (const MyArray &a, int s=0){
        int i;
        cout<<"Copy Constructor with additional parameter, called! for obj at address:" << &*this <<"\n";
        ptr = new int[a.size];
        
        for(i=0;i < a.size;i++)
            ptr[i]=a.ptr[i];
    }
    ~MyArray(){
        cout << "Destructor called! for obj at address:" << &*this <<"\n";
        delete [] ptr;
    }
    
    //set and set MyArray Elements
    void setArrayElement(int i, int j){
        if(i>=0 && i < size){
            ptr[i] = j;
        }
    }
    
    int getArrayElement(int i){
        return ptr[i];
    }
    
};

int main()
{
    MyArray arrObj(10);
    
    int i;
    
    for (i=0;i< 10;i++) 
        arrObj.setArrayElement(i,i);
        
    cout << "Elements in first arrObj: ";
    for (i=9;i>=0;i--) 
         cout<< arrObj.getArrayElement(i) <<" ";

    cout<<"\n";
    
    //Create another object with the existing arrObj obj
    //by doing this our Copy Constructor will be called.
    
    MyArray newArrObj(arrObj); //invokes copy Constructor
    
    //see the contents of newArrObj
    cout <<"Elements in first newArrObj: ";
    for (i=9;i>=0;i--)
        cout<< newArrObj.getArrayElement(i) <<" ";
        
    cout <<"\n";
        
    return 0;
}


 

The output of the program:


       
Parameterized Constructor called! for obj at address: 0x7fff5640a8f0, size: 10                                        
Elements in first arrObj: 9 8 7 6 5 4 3 2 1 0                                                                         
Copy Constructor with additional parameter, called! for obj at address:0x7fff5640a900                                 
Elements in first newArrObj: 9 8 7 6 5 4 3 2 1 0                                                                      
Destructor called! for obj at address:0x7fff5640a900                                                                  
Destructor called! for obj at address:0x7fff5640a8f0