Shallow and Deep Copy

I 'm not understand the code and my code not run

#include
#include
#include
using namespace std;

class Car{

     private :

      int price;

      public :

       int model_no;

       char *name;

        // Constructor 

        Car(){


        	  // override the default Constructor
              name = NULL;

        	  cout << "Making a car .." << endl;
        }
 // Constructor with parameters -Parametrised Constructor
    
        Car(int p,int mn,char *n){

        	  price = p;
        	  model_no = mn;

        	  int l = strlen(n);
        	  name = new char[l+1]; // dynamically declare  name 
        	  
        	  strcpy(name,n);

        }

}
// Deep Copy Cosntructor
Car(Car &X){
cout << “Making a copy of Car”;
price = X.price;
model_no = X.model_no;
int l = strlen(X.name);
name = new char[l+1]; // Copy contain original array

             strcpy(name,X.name);

             }
             
             
                 void setName(char *n){

                 	 if(name==NULL){

                 	 	  name  = new char [strlen(n)+1]; // new dynamic array declare
                 	 	  strcpy(name,n);

                 	 	  }

                 	 	    else {
                              // Delete the old name (array) allocate the new name (array)

                 	 	    } 
                 	 }
       
                 
      void start(){

      	    cout << "Grrr... starting the car " << name << endl;

      	  }

      	    
      	     void setPrice(int p){

      	     	    if(p>1000){

      	     	    	  price  =p;
      	     	    }
      	     	    else {

      	     	    	   price = 1000;
      	     	    }
      	     }

      	     int getPrice(){

      	     	  return price;
      	     }  
          void print(){

          	    cout << name << endl;
          	    cout << model_no << endl;
          	    cout << price << endl;
          	    cout << endl;
          }

      int main(){

      	       Car C;

      	        C.setPrice(1500);
      	        C.setName("Nano");
      	        C.model_no = 1001;

      	           C.print();

      	           Car D(100,200,"BMW");

      	           Car E(D);
      	           E.name[0] = 'G';

      	           D.print();
      	           E.print();

Hello @faizan_1402,

Shallow copy is one where you share the same bucket of resources.
Deep copy is when you create an another copy of that bucket and use that copied bucket rather than the actual bucket.

The same happens in the code.
When you pass the variables by value, then you create a deep copy i.e. a separate variable is created and it contains the same value but considered as a copy.
While, when you pass by reference, then there are different identifiers(i.e. names) but they are sharing the same value. Thus, making any change in the value using any one of the two identifier will affect both.

Please, share your code using the online IDE.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.