C, C++, MFC  

Chapter 10: Classes and Objects: Construction and Destruction

Chapter 9 introduced classes as blueprints for objects. This chapter details the crucial lifecycle events of an object: its creation using constructors and its cleanup using destructors.

1. Constructors: Object Initialization

A constructor is a special member function that is automatically called when an object of the class is created. Its primary purpose is to initialize the object's data members.

Rules for Constructors

  1. The constructor must have the same name as the class.

  2. It has no return type (not even void).

The Default Constructor

A constructor that takes no arguments is the default constructor. If you don't define one, the C++ compiler automatically generates a basic one.

class Point {
private:
    int x;
    int y;

public:
    // Default Constructor
    Point() {
        x = 0; // Initialize data members
        y = 0;
        std::cout << "Point created at (0, 0)." << std::endl;
    }
    // ...
};

int main() {
    Point p1; // Calls the default constructor
    return 0;
}

Parameterized Constructors

Constructors can take arguments, allowing you to initialize an object with specific values upon creation.

class Point {
    // ... private members ...

public:
    // Parameterized Constructor
    Point(int initial_x, int initial_y) {
        x = initial_x;
        y = initial_y;
        std::cout << "Point created at (" << x << ", " << y << ")." << std::endl;
    }
    // ...
};

int main() {
    Point p2(10, 20); // Calls the parameterized constructor
    return 0;
}

2. Destructors: Cleanup

A destructor is a special member function that is automatically called when an object is destroyed (when it goes out of scope or is explicitly deleted).

Rules for Destructors

  1. The destructor must have the same name as the class, prefixed with a tilde (~).

  2. It has no return type and takes no arguments.

  3. Only one destructor can exist per class.

Destructors are essential for releasing resources that the object might have acquired, especially dynamic memory allocated with new (covered in Chapter 7).

class ResourceUser {
private:
    int* data; // A pointer to dynamic memory

public:
    ResourceUser() {
        data = new int[100]; // Allocation in constructor
    }

    // Destructor
    ~ResourceUser() {
        delete[] data; // Release the dynamic memory
        std::cout << "Resources released." << std::endl;
    }
};
// When a ResourceUser object goes out of scope, the destructor is automatically called.

3. The this Pointer

Inside any non-static member function, C++ provides an implicit, hidden argument called the this pointer.

  • What it is: this is a pointer that holds the memory address of the object the function was called on.

  • Purpose: It is primarily used to differentiate between a member variable and a local variable (often a constructor parameter) that has the same name.

class Example {
private:
    int value;

public:
    // Parameter 'value' shadows the member 'value'
    Example(int value) {
        // Use 'this->' to explicitly refer to the member variable
        this->value = value;
    }

    // Returning the object itself for method chaining
    Example& setValue(int v) {
        this->value = v;
        return *this; // Returns a reference to the current object
    }
};

The this pointer is a core concept for understanding object identity and method chaining.