Object oriented programming (oops)

class queue{

int n;

int a[n];

public:

queue(int d){

    n=d;
}

};

//above code always throws out an error but when ,I implement the same code dynamically it does not give me an error.

class cat{

int*a;

public:

cat(int n){

    a=new int[n]; // why do we dynamically allocate it 
    
}

}

you can try this

#include <iostream>
using namespace std;
class queue {

    int n;

    int a[1000];

public:

    queue(int d) {

        n = d;
    }
};

your code snippet gives error because n is not declare at compile time
so can’t create array of unknown size
n will come into existence only when object of class is created
and object always created after class declaration
so you can’t do this int arr[n]