#include<iostream>
#include<string.h>
using namespace std;
class Student{
int age;
public:
char *name;
Student(int age,char *name){
this->age = age;
this->name = new char[strlen(name) + 1];
strcpy(this->name,name);
}
void display(){
cout<<this->age<<" "<<this->name<<endl;
}
};
int main(){
char name[] = "abcd";
Student s1(20,name);
Student s2(s1);
s2.name[0] = 'e';
//s2.name = "abdd";
cout<<&name<<endl;
cout<<&s1.name<<endl;
cout<<&s2.name<<endl;
s1.display();
s2.display();
}
OUTPUT :-
0x61ff0b
0x61ff04
0x61fefc
20 ebcd
20 ebcd
Why the addresses of s1.name and s2.name are different and still changes in s2.name is affecting s1.name??