...
Here is an example of a deep copy:
Code Block | ||
---|---|---|
| ||
#include <iostream> #include <string> using namespace std; class Person { public: Person(const string& name, int age); Person(const Person& other); // Copy constructor ~Person(); void introduce(); void setID(int newID); // Setter for id private: string name; int age; int* id; // Pointer to dynamically allocated memory }; Person::Person(const string& name, int age) { this->name = name; this->age = age; this->id = new int(12345); // Allocate memory for id } Person::Person(const Person& other) { this->name = other.name; this->age = other.age; this->id = new int(*other.id); // Allocate new memory for id and copy data } Person::~Person() { delete id; // Deallocate memory for id } void Person::introduce() { cout << "Hi, my name is " << name << " and I am " << age << " years old. My ID is " << *id << "." << endl; } void Person::setID(int newID) { *id = newID; // Set the value of id } int main() { Person p1personOne("Alice", 25); Person p2personTwo = p1personOne; // Use copy constructor to create new object p2 p1personOne.introduce(); p2personTwo.introduce(); cout << "Now lets change the ID of personTwo (pointer)" << endl; // Change ID of p1 using the setter *p1.id = personOne.setID(54321); p1personOne.introduce(); p2personTwo.introduce(); return 0; } |
...
The code defines a Person
class that represents a person with a name, age, and ID. It demonstrates the use of a copy constructor and dynamic memory allocation.
In the Person
class, there are member functions including constructors, a destructor, an introduce
function, and a setter function for the ID. The ID is stored as a pointer to dynamically allocated memory.
The constructor Person::Person(const string& name, int age)
initializes a Person
object with a given name and age. It also allocates memory for the ID and assigns it a default value of 12345.
The copy constructor Person::Person(const Person& other)
creates a new Person
object by copying the values of the name and age from another Person
object. It allocates new memory for the ID and copies the value of the ID from the other object.
The destructor Person::~Person()
deallocates the memory for the ID using the delete
operator.
The introduce
function prints out the person's name, age, and ID.
The setID
function allows for changing the value of the ID by assigning a new value to the memory location pointed by id
.
In the main
function, two Person
objects, p1
personOne
and p2
personTwo
, are created using the default constructor and . personTwo
is initialized using the copy constructor, respectively. Both objects are introduced using the introduce
function, and the output shows that they have the same name, age, and IDwhich creates a new object with the same values as personOne
. The introduce
function is called for both objects to display their information.
Then, the id
ID of p1
personOne
is changed to 54321. This change does not affect p2
, because the two objects have separate memory spaces for their id
susing the setID
function. This demonstrates that each object has its own separate ID value stored in dynamically allocated memory. Finally, the introduce
function is called again for both objects to display their updated information.
The purpose of the code is to showcase the use of a copy constructor and dynamic memory allocation in C++.
Overall, deep copying is an important technique for working with objects that have dynamically allocated memory, and can help avoid bugs and memory leaks in C++ programs.