...
Here is an example of a deep copy:
Person.h - This is the header file where the class declaration will be placed.
Code Block | ||
---|---|---|
| ||
// Person.h #ifndef PERSON_H #define PERSON_H #include <iostream> #include <string> using namespace std; class Person { public: // 2 argument constructor Person(const std::string& name, int age); // Copy constructor Person(const Person& other); // Assignment operator Person& operator=(const Person& other); // Destructor ~Person(); // Functions void introduce(); void setID(int newID); // Setter for id private: string name; int age; int* id; // Pointer to dynamically allocated memory }; #endif // it creates a new dynamic memory allocation (heap) // and copies the value from the original Person. int* id; };PERSON_H |
Person.cpp - This is the source file where the class definitions (implementation) will be placed.
Code Block | ||
---|---|---|
| ||
// Person.cpp #include "Person.h" Person::Person(const std::string& name, int age) {: name(name), age(age) { this->name = name; id = this->age = age; new int(12345); // Allocate memory for id } and dynamically allocates memory // for id and initializes it with 12345. this->id = new int(12345); } Person::Person Person::Person(const Person& other) : name(other.name), age(other.age) { id = new int(*other.id); // Allocate memory and copy id } Person& Person::operator=(const Person& other) { if (this->name != other.name;&other) { // Avoid self-assignment this->agename = other.agename; // Allocate - creates a new dynamic memory allocation (heap) age = other.age; delete id; // andDeallocate copiesexisting theid valuememory from the original Person. this->idid = new int(*other.id); // Allocate new memory and copy id } return *this; } 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 } |
main.cpp - The file to use the
Person
class.
Code Block | ||
---|---|---|
| ||
// main.cpp #include "Person.h" *id = newID; } int main() { Person personOne("Kevin", 39); Person personTwo = personOne; // Use copy constructor to create new object personTwo Person personTwo = personOne; personOne.introduce(); personTwo.introduce(); cout << endl; cout << "Now lets change the ID of personTwopersonOne (pointer)" << endl; personOne.setID(54321); // Change ID of personOne using the setter personOne.setID(54321); .introduce(); personTwo.introduce(); // personTwo will have the original ID // Demonstrating assignment operator Person personOne.introduce(); personTwopersonThree("Alice", 25); personThree = personOne; // Use assignment operator personThree.introduce(); // personThree will have the same ID as personOne return 0; } |
This C++ code defines a Person
class with attributes for name
, age
, and id
.
name
andage
are straightforward - astring
for the name and anint
for the age.id
is a pointer to an integer, which is dynamically allocated on the heap.
The Person
class defines the following:
...
A constructor Person::Person(const string& name, int age)
: This initializes a Person
object with a name
and age
, and dynamically allocates memory for id
and initializes it with 12345.
...
A copy constructor Person::Person(const Person& other)
: This is invoked when an existing Person
object is copied. It initializes a new Person
object with the same name
and age
as the original. For id
, it creates a new dynamic memory allocation (heap) and copies the value from the original Person
.
...
A destructor Person::~Person()
: This is invoked when a Person
object is destroyed (goes out of scope or is deleted). It deallocates the dynamic memory assigned to id
to prevent memory leaks.
...
A introduce
function void Person::introduce()
: This prints the name
, age
, and id
of the Person
.
...
In the main()
function:
Person personOne("Kevin", 39)
creates aPerson
object namedpersonOne
.Person personTwo = personOne
uses the copy constructor to create a newPerson
objectpersonTwo
that's a copy ofpersonOne
.Then, it prints the information of
personOne
andpersonTwo
, showing that both have the samename
,age
, andid
.It changes the
id
ofpersonOne
and prints the information again. You'll see thatpersonOne
'sid
has changed, butpersonTwo
'sid
remains the same, demonstrating that the copy constructor made a separate copy of theid
. This concept is often referred to as deep copying in C++.