I have to make class Buffer with 3 private members: kapacitet - capcity of buffer, brojElemenata - number of elements in buffer, and bafer - buffer array in which elements are stored; public function push which stores element in bafer.
Here is my code:
#include <iostream>
using namespace std;
class Buffer
{
private:
int kapacitet;
int brojElemenata;
int *bafer = new int[kapacitet];
public:
Buffer()
{
kapacitet = 0;
brojElemenata = 0;
}
Buffer(int n)
{
kapacitet = n;
brojElemenata = 0;
}
~Buffer()
{
delete [] bafer;
}
void push(int el)
{
if(brojElemenata >= kapacitet)
cout << "Bafer is full." <<endl;
else{
bafer[brojElemenata] = el;
brojElemenata++;
}
}
};
int main()
{
cout << "** BUFFER **"<<endl;
Buffer b(2);
b.push(3);
}
Everything is compiled good, with no errors, but when I run code in some cases it doesnt work.
For example:
when I try to push value greater than 3 it outputs this:
** BUFFER **
terminate called after throwing an instance of 'std::bad_array_new_length'
what(): std::bad_array_new_length
Aborted (core dumped)
In cases I push values equally or lower than 3 it works.
Does someone has explanation for this?
Here is my code:
#include <iostream>
using namespace std;
class Buffer
{
private:
int kapacitet;
int brojElemenata;
int *bafer = new int[kapacitet];
public:
Buffer()
{
kapacitet = 0;
brojElemenata = 0;
}
Buffer(int n)
{
kapacitet = n;
brojElemenata = 0;
}
~Buffer()
{
delete [] bafer;
}
void push(int el)
{
if(brojElemenata >= kapacitet)
cout << "Bafer is full." <<endl;
else{
bafer[brojElemenata] = el;
brojElemenata++;
}
}
};
int main()
{
cout << "** BUFFER **"<<endl;
Buffer b(2);
b.push(3);
}
Everything is compiled good, with no errors, but when I run code in some cases it doesnt work.
For example:
when I try to push value greater than 3 it outputs this:
** BUFFER **
terminate called after throwing an instance of 'std::bad_array_new_length'
what(): std::bad_array_new_length
Aborted (core dumped)
In cases I push values equally or lower than 3 it works.
Does someone has explanation for this?