C++ Class Constructor Destructor Example

5 min read Jul 01, 2024
C++ Class Constructor Destructor Example

C++ Class Constructor and Destructor Example

This article provides a comprehensive example of how constructors and destructors work in C++ classes.

What are Constructors and Destructors?

Constructors are special member functions that are automatically called when an object of a class is created. They initialize the object's data members.

Destructors are also special member functions that are automatically called when an object goes out of scope. They are used to clean up resources allocated by the object, such as releasing memory or closing files.

Example: A Simple Book Class

Let's create a simple class representing a book with the following data members:

  • title: A string to store the book's title
  • author: A string to store the author's name
  • pages: An integer to store the number of pages
#include 
#include 

using namespace std;

class Book {
public:
  // Constructor
  Book(string title, string author, int pages) {
    this->title = title;
    this->author = author;
    this->pages = pages;
    cout << "Book '" << title << "' created." << endl;
  }

  // Destructor
  ~Book() {
    cout << "Book '" << title << "' destroyed." << endl;
  }

  // Other member functions
  void display() {
    cout << "Title: " << title << endl;
    cout << "Author: " << author << endl;
    cout << "Pages: " << pages << endl;
  }

private:
  string title;
  string author;
  int pages;
};

In this example:

  1. Constructor:

    • The Book(string title, string author, int pages) constructor takes three parameters: title, author, and pages. It initializes the corresponding data members of the object using the this pointer.
    • The constructor also prints a message indicating that the book is created.
  2. Destructor:

    • The ~Book() destructor has no parameters.
    • It prints a message indicating that the book is destroyed.

Using the Book Class

int main() {
  // Create a Book object
  Book book1("The Hitchhiker's Guide to the Galaxy", "Douglas Adams", 224);

  // Display the book details
  book1.display();

  // Another book object
  Book book2("The Lord of the Rings", "J.R.R. Tolkien", 1178);
  book2.display();

  // book1 and book2 go out of scope here
  // Destructors will be called automatically

  return 0;
}

Output:

Book 'The Hitchhiker's Guide to the Galaxy' created.
Title: The Hitchhiker's Guide to the Galaxy
Author: Douglas Adams
Pages: 224
Book 'The Lord of the Rings' created.
Title: The Lord of the Rings
Author: J.R.R. Tolkien
Pages: 1178
Book 'The Lord of the Rings' destroyed.
Book 'The Hitchhiker's Guide to the Galaxy' destroyed.

Explanation:

  • Two Book objects, book1 and book2, are created using the constructor.
  • The display() member function is called to show the details of each book.
  • When book1 and book2 go out of scope at the end of the main() function, their respective destructors are automatically called, printing the destruction messages.

Key Points

  • Constructors are essential for initializing class objects.
  • Destructors are necessary for releasing resources acquired by an object.
  • Constructors and destructors are called automatically during object creation and destruction.
  • The this pointer is used to access the object's data members within member functions.

This example demonstrates the basic usage of constructors and destructors in C++. These concepts are fundamental for working with objects and managing resources effectively in C++ programs.

Featured Posts