For this assignment I need to implement a c++ class that creates a ring buffer. A ring buffer still uses an array, but its has two indices, i..e., the head and the tail. Note: The head is the index of the first data point, and the tail by definition is the index of the last data point plus 1 (i.e., the tail indicates the first one that is not of interest). Therefore, if you use an array of N elements for this ring buffer, this ring buffer can actually hold N-1 data point. This is very important, because head==tail is the condition for an empty buffer.

This is the class that I need to implement, followed by the void main() function I need to test it with:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

#define SIZE 10 // don’t modify this number

class CRingBuffer
{
int m_buffer[SIZE];
int m_head; // the position of the first element
int m_tail; // the position after the last element

public:
CRingBuffer(); // constructor
~CRingBuffer() { }; // destructor
bool isfull(); // check if the buffer is full
bool isempty(); // check if the buffer is empty
void put(int data); // always put the data at end
bool get(int &data); // always get the data of the current head
int size(); // return the actual size of elements in the buffer
void dump(); // dump all the debug information
};

void main()
{
    CRingBuffer buff;
    buff.dump();

    for(int k=1;k<=20;k++)
    {
        buff.put(k);
        2
        buff.dump();
    }

    cout << "\n\n";
    int d;
    for(k=0; k<10; k++)
    {
        buff.get(d);
        buff.dump();
    }

    cout << “Press any key to exit.”;
    getchar();
}

What I need help with is definining each function underneith "public" in the class CRingBuffer definition. I'm fairly confused as to how to implement these, so any help that anyone could give me would be great. Thanks!!

By the way, the output should look like the following attached:

It looks an awful lot like you've just posted the assignment boilerplate code. How much work have you put into this yet?

Most of your class' functions are pretty basic. The constructor initializes things, the destructor probably doesn't need to do anything for this, isfull and isempty just compare the head and tail indices and respond accordingly, size is the difference in the indices, dump prints the contents of the array and the values of the indices, and put and get have to check if the state is valid then insert a value or remove one and adjust the indices accordingly.

It looks an awful lot like you've just posted the assignment boilerplate code. How much work have you put into this yet?

Most of your class' functions are pretty basic. The constructor initializes things, the destructor probably doesn't need to do anything for this, isfull and isempty just compare the head and tail indices and respond accordingly, size is the difference in the indices, dump prints the contents of the array and the values of the indices, and put and get have to check if the state is valid then insert a value or remove one and adjust the indices accordingly.

I haven't tried initializing the constructor or the void commands yet but I was wondering if someone could look at the booleans i worked on to see if I am correct thus far, thanks!:

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

#define SIZE 10 // don’t modify this number

class CRingBuffer
{
int m_buffer[SIZE];
int m_head; // the position of the first element
int m_tail; // the position after the last element

public:
CRingBuffer(); // constructor
~CRingBuffer() { }; // destructor

bool isfull(); // check if the buffer is full
{
    if (m_buffer == 9)
        return true;
    else
    {
        return false;
    }
}

bool isempty(); // check if the buffer is empty
{
    if ( m_head == m_tail)
    {
        return true;
    }
    else
    {
        return false;
    }
}
void put(int data); // always put the data at end

bool get(int &data); // always get the data of the current head
{
    if (m_buffer == 0)
        return false;
    else
    {
        m_head* = data;
        return data;
    }

int size(); // return the actual size of elements in the buffer
void dump(); // dump all the debug information
};

void main()
{
    CRingBuffer buff;
    buff.dump();

    for(int k=1;k<=20;k++)
    {
        buff.put(k);
        2
        buff.dump();
    }

    cout << "\n\n";
    int d;
    for(k=0; k<10; k++)
    {
        buff.get(d);
        buff.dump();
    }

    cout << “Press any key to exit.”;
    getchar();
}
bool get(int &data); // always get the data of the current head
{
    if (m_buffer == 0)
        return false;
    else
    {
         m_head *= data;

Is this really what you want to do? ;)

bool get(int &data); // always get the data of the current head
{
    if (m_buffer == 0)
        return false;
    else
    {
         m_head *= data;

Is this really what you want to do? ;)

I want this boolean to return false when the buffer is empty, so I just realized it should be:

if(m_head == m_tail){
                 return false;
                }

But what I want the boolean to do is get the data of the current head....so actually should i thought I could use a pointer so that the m_head* = the int data that is referenced in the boolean function. Can I not do it this way and it should be done in another function? should it just be : else return true;? also, do my other boolean functions I defined look correct? thanks!

But what I want the boolean to do is get the data of the current head....so actually should i thought I could use a pointer so that the m_head* = the int data that is referenced in the boolean function.

You can't do it that way. Do you not see what's really going to happen? Since m_head is declared as an integer, what really happens is this:

m_head = m_head * data;

You're just multiplying the value in m_head against the value in data and storing this in m_head.

So I'm just guessing here, but when you call get() with an integer variable as a parameter, get() is supposed to set that to the value contained in m_head? In that case, a simple assignment would be all that's necessary:

data = m_head;

Since data is a reference, it the memory will remain in scope even after it returns.

You can't do it that way. Do you not see what's really going to happen? Since m_head is declared as an integer, what really happens is this:

m_head = m_head * data;

You're just multiplying the value in m_head against the value in data and storing this in m_head.

So I'm just guessing here, but when you call get() with an integer variable as a parameter, get() is supposed to set that to the value contained in m_head? In that case, a simple assignment would be all that's necessary:

data = m_head;

Since data is a reference, it the memory will remain in scope even after it returns.

I completely overlooked the fact that a*= b is the same thing as a = a*b, thanks for pointing that out to me! Now I'll try and complete the constructor, the void put() function and void debug dump function.

For the constructor I was thinking of just leaving it as a default constructor, without definition, do you believe this would work? thanks!

For the constructor I was thinking of just leaving it as a default constructor, without definition, do you believe this would work? thanks!

Either you define the constructor or you don't declare it. Declaring something without defining it is going to cause a linker error.

What I'd probably do is something like have 1 constructor that has a default parameter, which if used by the programmer can add a node immediately after the object is initalized.

Your choice, but just decide whether or not to define and declare a constructor.

I think everything else is all set for my code, but I was wondering if someone could take a look at the last function I have to define: void dump(). I was thinking of simply using delete[], would something like that work? thanks!

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

#define SIZE 10 // don’t modify this number

class CRingBuffer
{
int m_buffer[SIZE];
int m_head; // the position of the first element
int m_tail; // the position after the last element
int sizer = 0; //

public:

CRingBuffer(); // constructor

~CRingBuffer() { }; // destructor

bool isfull(); // check if the buffer is full
{
    if (sizer == 9)
        return true;
    else
    {
        return false;
    }
}

bool isempty(); // check if the buffer is empty
{
    if ( m_head == m_tail)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void put(int data); // always put the data at end
{
    m_buffer[m_tail] = data;
    sizer++;
    m_tail++;
}

bool get(int &data); // always get the data of the current head
{
    if (m_head == m_tail)
    {
        return false;
    }
    else
    {
        data = m_head;
        return data;
    }
}

int size(); // return the actual size of elements in the buffer
{
    return sizer;
}

void dump(); // dump all the debug information
};

void main()
{
    CRingBuffer buff;
    buff.dump();

    for(int k=1;k<=20;k++)
    {
        buff.put(k);
        2
        buff.dump();
    }

    cout << "\n\n";
    int d;
    for(k=0; k<10; k++)
    {
        buff.get(d);
        buff.dump();
    }

    cout << “Press any key to exit.”;
    getchar();
}

>I was thinking of simply using delete[]
Look closely at your code. Is there any place where you dynamically allocate memory? If so, then use it. If there is no dynamic memory allocation in your program (i.e. no new ), then you sure don't need (or rather shouldn't) deallocate memory.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.