Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagecpp
#include <iostream>
#include <string>
using namespace std;

class Person {
public:
    // 2 argument constructor
    Person(const string& name, int age);
    // Copy constructor
    Person(const Person& other);
    // Copy constructorDestructor
    ~Person();
    // Functions
    void introduce();
    void setID(int newID); // Setter for id
private:
    string name;
    int age;
    int* id; // Pointer to dynamically allocated memory
    // it creates a new dynamic memory allocation (heap)
    // and copies the value from the original Person.
    int* id;
};

Person::Person(const string& name, int age) {
    this->name = name;
    this->age = age;
    // Allocate memory for id and dynamically allocates memory
    // for id and initializes it with 12345.
    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// Allocate - creates a new dynamic memory allocation (heap)
    // and copies the value from the original Person.
    this->id = new int(*other.id);
}

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
    *id = newID;
}

int main() {
    Person personOne("AliceKevin", 2539);
  
 Person personTwo = personOne;  // Use copy constructor to create new object p2 personTwo
    Person personTwo = personOne;
    
    personOne.introduce();
    personTwo.introduce();
    
    cout << endl;
    cout << "Now lets change the ID of personTwo (pointer)" << endl;
    
    // Change ID of p1personOne using the setter
    personOne.setID(54321);
    
    personOne.introduce();
    personTwo.introduce();
    
    return 0;
}

...

...

This C++ code defines a Person class that represents a person with a with attributes for 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 id.

  • name and age are straightforward - a string for the name and an int for the age.

  • id is a pointer to an integer, which is dynamically allocated on the heap.

The Person class defines the following:

  1. A constructor Person::Person(const string& name, int age): This initializes a Person object with a

...

  1. name and age

...

  1. , and dynamically allocates memory for

...

  1. id and

...

  1. initializes it with 12345.

...

  1. A copy constructor Person::Person(const Person& other)

...

  1. : This is invoked when an existing Person object is copied. It initializes a new Person object

...

  1. with the same name and age

...

  1. as the original. For id, it creates a new dynamic memory allocation (heap) and copies the value

...

  1. from the

...

  1. original Person.

...

  1. A destructor Person::~Person(): This is invoked when a Person object is destroyed (goes out of scope or is deleted). It deallocates the

...

  1. dynamic memory assigned to id to prevent memory leaks.

  2. A introduce function void Person::introduce(): This prints the name, age, and

...

  1. id of the Person.

...

  1. A setter function void Person::setID(int newID): This sets the value of

...

  1. id for the Person.

In the main() function, two Person objects, personOne and personTwo, are created. personTwo is initialized using the copy constructor, which 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 of personOne is changed using 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:

  • Person personOne("Kevin", 39) creates a Person object named personOne.

  • Person personTwo = personOne uses the copy constructor to create a new Person object personTwo that's a copy of personOne.

  • Then, it prints the information of personOne and personTwo, showing that both have the same name, age, and id.

  • It changes the id of personOne and prints the information again. You'll see that personOne's id has changed, but personTwo's id remains the same, demonstrating that the copy constructor made a separate copy of the id. This concept is often referred to as deep copying in C++.