C++ Program for this Pointer

03/06/2024 0 By indiafreenotes

In C++, the ‘this’ pointer is an implicit parameter to all non-static member functions. It is a pointer to the object for which the member function is called. This pointer helps in accessing the member variables and other member functions of the class within its member functions.

Here’s a complete program that demonstrates the use of the this pointer in C++:

#include <iostream>

using namespace std;

class Rectangle {

private:

    int length;

    int width;

public:

    // Default constructor

    Rectangle() : length(0), width(0) {}

    // Parameterized constructor

    Rectangle(int length, int width) {

        // Use ‘this’ pointer to resolve name conflict

        this->length = length;

        this->width = width;

    }

    // Member function to set dimensions

    void setDimensions(int length, int width) {

        // Use ‘this’ pointer to resolve name conflict

        this->length = length;

        this->width = width;

    }

    // Member function to calculate area

    int area() const {

        return length * width;

    }

    // Member function to print dimensions

    void printDimensions() const {

        cout << “Length: ” << this->length << “, Width: ” << this->width << endl;

    }

    // Member function to return the current object

    Rectangle& increaseSize(int increment) {

        this->length += increment;

        this->width += increment;

        return *this;

    }

};

int main() {

    // Create an object of Rectangle using the default constructor

    Rectangle rect1;

    rect1.printDimensions();

    // Set dimensions using the member function

    rect1.setDimensions(10, 5);

    rect1.printDimensions();

    // Create another object of Rectangle using the parameterized constructor

    Rectangle rect2(3, 7);

    rect2.printDimensions();

    // Calculate and print the area of rect2

    cout << “Area of rect2: ” << rect2.area() << endl;

    // Increase the size of rect2 and print the new dimensions

    rect2.increaseSize(2).printDimensions();

    return 0;

}

Explanation of the Program

  1. Class Definition

‘Rectangle’ class has private member variables ‘length’ and ‘width’. It includes constructors, member functions to set dimensions, calculate area, print dimensions, and increase the size of the rectangle.

  1. Constructors
  • Default Constructor: Initializes length and width to 0.
  • Parameterized Constructor: Uses the ‘this’ pointer to distinguish between the member variables and parameters with the same name.
  1. Member Functions
  • Set Dimensions:

Uses the ‘this’ pointer to resolve the naming conflict between the member variables and parameters.

  • Area:

Returns the area of the rectangle.

  • Print Dimensions:

Prints the dimensions of the rectangle. Although this-> is not necessary here, it is used for clarity.

  • Increase Size:

Increases the size of the rectangle by a given increment and returns a reference to the current object.

  • Main Function:

Creates and manipulates Rectangle objects using the class member functions. Demonstrates the use of the ‘this’ pointer to set dimensions, calculate the area, and increase the size of the rectangle.