#include <iostream>
class MyClass {
private:
int myArray; // Pointer to a pointer to int for 2D array
int rows;
int cols;
public:
MyClass(int r, int c) : rows(r), cols(c) {
// Dynamically allocate memory for the 2D array
myArray = new int*[rows]; // Create an array of int pointers (rows)
for (int i = 0; i < rows; ++i) {
myArray[i] = new int[cols]; // For each row, create an array of ints (cols)
}
}
~MyClass() {
// Deallocate memory when object is destroyed
for (int i = 0; i < rows; ++i) {
delete[] myArray[i]; // Delete individual rows
}
delete[] myArray; // Delete the array of int pointers
}
// Function to display the 2D array
void displayArray() {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
std::cout << myArray[i][j] << " ";
}
std::cout << std::endl;
}
}
};
int main() {
// Create an object of MyClass with a 2x3 array
MyClass obj(2, 3);
// Display the initialized array
obj.displayArray();
return 0;
}