When I am using copy constructor it not making shallow copy

my class car has 2 object B and C.
C is copy of B (C is made using copy constructor). Now when i am changing name[0] (a datamember) of object C, then same change must reflect in object B (due to shallow copy being created) but its not showing that.

link to code : https://ide.codingblocks.com/s/239337

hi @vishaaltiwari1999

shallow copy created when default copy constructor is called
but you have make your ones so it is not calling default constructor
and in your copy constructor you make a mistake so it is giving segmentation fault

to get correct output

  1. first remove your copy constructor (line 19-24)
    then see ouput boths name will change

now add your own copy constructor with some change
modified copy constructor

 car (const car &x)
    {
        int l=strlen(x.name)+1;
        name = new char[l]; 
        strcpy(name,x.name);
        price=x.price;
        model=x.model;
    }

after that deep copy will be created

i hope this help
if you have more doubts regarding this feel free to ask
if your doubt is resolved mark it as resolved from your doubt section inside your course

Modified Code

if your doubt is resolved mark it as resolved from your doubt section inside your course

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.