Demo -Destructors

Here is an example of a class with a destructor:

Person.hpp

#ifndef Person_hpp #define Person_hpp #include <string> using namespace std; class Person { public: Person(string name, int age); ~Person(); void introduce(); private: string name; int age; }; #endif /* Person_hpp */

Person.cpp

#include <iostream> using namespace std; #include "Person.hpp" Person::Person(string name, int age) { this->name = name; this->age = age; } Person::~Person() { cout << "Destroying " << name << endl; } void Person::introduce() { cout << "Hi, my name is " << name << " and I am " << age << " years old." << endl; }

Main.cpp (Driver)

#include <iostream> #include <string> #include "Person.hpp" using namespace std; int main() { // This dynamically creates an object of the Person class // using the new keyword and assigns its address // to the ptrPersonOne pointer. Person* ptrPersonOne = new Person("Alice", 25); Person personTwo("Kevin", 30); // This calls the introduce() member function on the // object pointed to by ptrPersonOne using the arrow operator (->). ptrPersonOne->introduce(); personTwo.introduce(); // deallocates the memory assigned to the object pointed to // by ptrPersonOne using the delete keyword. // The destructor is called before the memory is freed. delete ptrPersonOne; return 0; }

In this example, the Person class has a constructor that takes a name and age as arguments, and a destructor that prints a message when the object is destroyed. The introduce function simply prints out the name and age of the person.

In the main function, two Person objects are created: ptrPersonOne1 on the heap using new, and personTwo on the stack. The introduce function is called for both objects to print their information. Then, ptrPersonOne is explicitly deleted using the delete operator, which calls the destructor of the Person object and prints the corresponding message.

Using destructors is important for ensuring that resources are properly cleaned up when an object is no longer needed. This can help avoid memory leaks and other errors in C++ programs.

The given code demonstrates the usage of classes, constructors, destructors, and object creation in C++. Here's an explanation of the code:

  1. #include <iostream>: This line includes the necessary header file for input/output operations in C++.

  2. #include <string>: This line includes the header file for the string class in C++.

  3. using namespace std;: This line allows the use of standard C++ functions and objects without specifying the namespace.

  4. class Person { ... };: This defines a class named "Person" with private member variables name and age, and public member functions Person(), ~Person(), and introduce().

  5. Person::Person(string name, int age) { ... }: This is the constructor definition for the Person class. It initializes the name and age member variables of the object using the provided arguments.

  6. Person::~Person() { ... }: This is the destructor definition for the Person class. It is called when an object of the class is destroyed, and it prints a message indicating the destruction of the object.

  7. void Person::introduce() { ... }: This is the definition of the introduce() member function of the Person class. It prints a message introducing the person with their name and age.

  8. int main() { ... }: The program's entry point, the main function.

  9. Person* ptrPersonOne = new Person("Alice", 25);: This dynamically creates an object of the Person class using the new keyword and assigns its address to the ptrPersonOne pointer. The constructor is called with the provided arguments to initialize the object.

  10. Person personTwo("Kevin", 30);: This creates an object of the Person class named personTwo using the constructor with the provided arguments.

  11. ptrPersonOne->introduce();: This calls the introduce() member function on the object pointed to by ptrPersonOne using the arrow operator (->).

  12. personTwo.introduce();: This calls the introduce() member function on the personTwo object using the dot operator (.).

  13. delete ptrPersonOne;: This deallocates the memory assigned to the object pointed to by ptrPersonOne using the delete keyword. The destructor is called before the memory is freed.

  14. return 0;: The main function finishes, and the program terminates with a return value of 0.

 

 

2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark