Question
The problem is in C++ and I am very lost on what to do, any comments in the code would be greatly appreciated. Thanks! circbuf.h
The problem is in C++ and I am very lost on what to do, any comments in the code would be greatly appreciated. Thanks!
circbuf.h
#ifndef CIRCBUF_H
#define CIRCBUF_H
#include
using std::ostream;
#include
using std::vector;
#include
using std::initializer_list;
class CircBuf{
private:
int sz_;
int cnt_;
vector
size_t head_;
size_t tail_;
public:
CircBuf(size_t s=10);
CircBuf(initializer_list
long front() const;
long back() const;
bool full() const;
bool empty() const;
void add(long);
void remove();;
friend ostream& operator
/*
friend CircBuf operator+(CircBuf &buf, long val);
friend CircBuf operator+(long val, CircBuf &buf);
friend CircBuf operator+(CircBuf &buf2, CircBuf &buf1);
*/
};
ostream& operator
/*
CircBuf operator+(CircBuf &buf, long val);
CircBuf operator+(long val, CircBuf &buf);
CircBuf operator+(CircBuf &buf2, CircBuf &buf1);
*/
#endif
main.cpp
#include
#include "lab10_circbuf.h"
int main(){ cout
CircBuf cb(4); cout
CircBuf cb_il({2,4,6,8}, 5); cout
try{ CircBuf bad_il({2,4,6,8}, 4); } catch (runtime_error &e){ cout
cout
cb.remove(); cout
cout
cout
cout
// should throw, it's full try{ cb.add(1111); } catch (runtime_error err){ cout
// should thow, it's empty try{ cout
CircBuf cb2({1,2,3,4}, 5); cb2.add(20); cb2.remove(); cb2.add(30); cb2.remove(); cout
/* // Extra Credit cb = cb + 25; cb = 30 + cb; cout
The Problem We are going to work on making our own classes with private data members and special accessors We are going to build a CircularBuffer class, a common data structure with well known accessors Some Background Our CircularBuffer will be a data structure that stores long. A CircularBuffer is a fixed size FIFO (First In, First Out) data structure. It is essentially a line (a queue). First thing added (the Head position in the diagram) is the first thing read. The next thing added is at the Tail position. It is the last thing added, the last thing that will be readread. The underlying data structure for this approach has a fixed size data structure. It can become empty, it can become full. It does not grow or shrink in size over the course of the run of the program. Head (extract) IFO asa Circular Buffer Tail (insert) Things you can do with your CircularBuffer you can add to the CircularBuffer. An element is added at the Tail position. The write position is then advanced (clockwise in the diagram you can remove an element. The element at the Head position is removed. The Head position is then advanced (clockwise in the diagram) you can test if it is full e you can test if it is empty you can report the front element . you can report the back element Your Tasks We are going to make a CircBuf class with these characteristics and test it. The Class The CircBuff class will have an underlying data member buf of type vector
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started