Created
October 12, 2023 05:04
-
-
Save gcs-abdulwahab/1f42027e03349f43cfeb4fa104038bc8 to your computer and use it in GitHub Desktop.
with Deep Copy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Person { | |
private : | |
int x; | |
int *ip; | |
public: | |
Person(int x, int y) { | |
this->x = x; | |
ip = new int(y); | |
} | |
void display() { | |
cout << "( " << x << " , " << *ip << " )"<<endl; | |
} | |
Person() { | |
this->x = 0; | |
ip = NULL; | |
} | |
void setY(int y){ | |
*ip = y; | |
} | |
Person( Person &obj){ | |
x = obj.x; | |
ip = new int(*obj.ip); | |
} | |
}; | |
int main() { | |
Person p1(2, 3); | |
p1.display(); | |
Person p2(p1 ); | |
p2.display(); | |
p1.setY(40); | |
p1.display(); | |
p2.display(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment