Deep copy
#include <iostream>
#include <cstring>
using namespace std;
class student{
int age;
char*name;
public:
student(int age,char*name){
this->age=age;
//Shallow copying
//this->name=name;
//Deep COppy
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
}
void display(){
cout<<name<<" "<<age<<endl;
}
};
int main() {
char name[]="abcd";
student s1(20,name);
s1.display();
name[3]='e';
student s2(24,name);
s2.display();
s1.display();
}
Comments
Post a Comment