How to use a Class or Struct In C++

We can define a class or struct that contains an array as a member variable, and then define a member function that returns the size of the array. This method is useful if we need to pass the array and its size as a single parameter to a function.

C++ program to Find the Size of an Array using a Class and Member Function

C++




// C++ program to find size of
// an array usinga Class or Struct
#include <iostream>
using namespace std;
 
class MyArray {
private:
    // declare the array
    int arr[100];
    // size of the array
    int size;
 
public:
    // read values into the array
    void readArray();
    // get the size of the array
    int getSize();
};
 
void MyArray::readArray()
{
    // initialize size to 0
    size = 0;
    while (size < 100) {
        cout << "Enter a value for element " << size
             << " (or -1 to stop): ";
        cin >> arr[size];
        // exit the loop if -1 is entered
        if (arr[size] == -1) {
            break;
        }
        // increment size for each element entered
        size++;
    }
}
 
int MyArray::getSize()
{
    // return the size of the array
    return size;
}
 
int main()
{
    // declare an object of MyArray
    MyArray a;
    // read values into the array
    a.readArray();
    // get the size of the array
    int size = a.getSize();
    // print the size of the array
    cout << "Size of the array: " << size << endl;
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


Output

Enter a value for element 0 (or -1 to stop): Enter a value for element 1 (or -1 to stop): Enter a value for element 2 (or -1 to stop): Enter a value for element 3 (or -1 to stop): Enter a value for el...

Output

Enter a value for element 0 (or -1 to stop): 8
Enter a value for element 1 (or -1 to stop): 4
Enter a value for element 2 (or -1 to stop): 2
Enter a value for element 3 (or -1 to stop): 7
Enter a value for element 4 (or -1 to stop): 5
Enter a value for element 5 (or -1 to stop): 6
Enter a value for element 6 (or -1 to stop): -1
Size of the array: 6

Complexity Analysis

  • Time complexity: O(1), getSize() method returns size member variable that takes constant time.
  • Auxiliary space: O(1)


How to Find Size of an Array in C++ Without Using sizeof() Operator?

In C++, generally, we use the sizeof() operator to find the size of arrays. But there are also some other ways using which we can find the size of an array. In this article, we will discuss some methods to determine the array size in C++ without using sizeof() operator.

Similar Reads

Methods to Find the Size of an Array without Using sizeof() Operator

Given an array (you don’t know the type of elements in the array), find the total number of elements in the array without using the sizeof() operator. So, we can use the methods mentioned below:...

1. Using Pointer Hack

The following solution is concise when compared to the other solution. The number of elements in an array A can be found using the expression:...

2. Using Macro Function

...

3. Implement Our Own sizeof( )

We can define a macro that calculates the size of an array based on its type and the number of elements....

4. Using Template Function

...

5. Using a Sentinel Value

Using custom user-defined sizeof function which can provide the functionality same as sizeof( )....

6. Using a Class or Struct

...