代写C C++ Java Python 视频讲解客服在线客服在线

代做国外C C++ Java程序 Python QQ 2365427650 Email 2365427650@qq.com 免费咨询编程问题

« An infectious diseaseGraph algorithms in C »

overloading Buffer

Implement a Buffer template class. The buffer stores elements
of some type. In case the buffer is full and the user attempts
to add more elements to it, memory is allocated with some
margin. E. g., consider a buffer of capacity 10 which already
contains 10 elements. In case of an attempt to add new element
memory should be allocated, and not for 11 elements but, for
example, for 15 elements. Upon addition of two buffers of
sizes 10 and 20, memory is allocated for, say, 40 elements
instead of 30 and so on.
size - number of elements in the buffer;
capacity - maximum number of elements that can be stored in
the buffer. If size == capacity and a new element is added,
allocation of memory for a greater number of elements takes
place;
buffer - pointer to a memory region where the elements of the
buffer are stored;
Resize(int AddCapacity) - extends capacity of the current buffer by
AddCapacity. The data stored in the buffer and its size are
left unchanged.
template <typename T>
class Buffer
{
private:
unsigned int size;
unsigned int capacity;
T* buffer;
void Resize(unsigned int AddCapacity);public:
Buffer();
Buffer(unsigned int Capacity);
Buffer(const Buffer& Other);
Buffer(Buffer&& Other);
~Buffer();
Buffer& operator=(const Buffer& Other);
Buffer& operator=(Buffer&& Other);
//...
};
I
Implement the given constructors, destructor, assignment
operators and Resize member function.
The Buffer(int Capacity) constructor creates a buffer of
capacity Capacity, the default constructor creates a buffer
of some given capacity (defined by you).
The constructors, destructor and assignment operators must
contain debug prints with their names.
II
Implement constant member functions for retrieving the current
number of elements in the buffer (Size()), its capacity
(Capacity()) and the values of the first (First()) and last
(Last()) elements.III
Implement the Delete(unsigned int Idx) member function, which
removes the element at the given index Idx. The buffer elements
are numbered starting at zero.
For example, if your buffer contained the following data
{1, 2, 3, 4, 5, 6, 7, 8, 9}
Then, after the element at index 3 is removed, its contents
would be:
{1, 2, 3, 5, 6, 7, 8, 9}
IV
Overload the operators +, +=, <<, [] and the writing to an
output stream operator (<<).
Operator +
The return type: Buffer<T>
1) Addition of two buffers. The result is a new buffer, the
operands themselves are left unchanged.
Buffer<int> buff1 = ...; // {1, 2, 3, 4, 5}
Buffer<int> buff2 = ...; // {6, 7, 8, 9, 10}
...
Buffer<int> buff3 = buff1 + buff2; // {1,2,3,4,5,6,7,8,9,10}
2) Addition of a buffer and an element of type T. The result
is a new buffer with the element added to its end. The operands
themselves are left unchanged.
Buffer<int> buff1 = ...; // {1, 2, 3, 4, 5}
Buffer<int> buff2 = buff1 + 10; // {1,2,3,4,5,10}
3) Addition of an element of type Т и буфера. and a buffer.
The result is a new buffer with the element added to its beginning. Positions of the other elements are shifted by one.
The operands themselves are left unchanged.
Buffer<int> buff1 = ...; // {1, 2, 3, 4, 5}
Buffer<int> buff2 = 10 + buff1; // {10,1,2,3,4,5}
Operator +=
The return type: Buffer<T>&
1) Addition of an element of type Т to the end of the current
buffer.
Buffer<int> buff1 = ...; // {1, 2, 3, 4, 5}
buff1 += 10; // {1, 2, 3, 4, 5, 10}
2) Addition of a buffer to the end of the current one.
Buffer<int> buff1 = ...; // {1, 2, 3, 4, 5}
Buffer<int> buff2 = ...; // {6, 7, 8, 9, 10}
buff1 += buff2; //buff1 = {1,2,3,4,5,6,7,8,9,10}
Hint: the private member function Resize implemented earlier
could be used to change the size of a buffer.
Operator <<
The return type: void
1) Addition of an element of type Т to the end of the current
buffer.
Buffer<int> buff1 = ...; // {1, 2, 3, 4, 5}
buff1 << 10; // {1, 2, 3, 4, 5, 10}
2) Addition of a buffer to the end of the current one.
Buffer<int> buff1 = ...; // {1, 2, 3, 4, 5}
Buffer<int> buff2 = ...; // {6, 7, 8, 9, 10}
buff1 << buff2; //buff1 = {1,2,3,4,5,6,7,8,9,10}Hint: the private member function Resize implemented earlier
could be used to change the size of a buffer.
Operator []
Retrieving and setting the value of the element at the given
index.
The following code must work:
Buffer<Type> buff;
...
Type val = buff[3];
buff[5] = val2;
Operator << (writing to an output stream)
Outputs the contents of a buffer to the screen. This operator
must be implemented as a global function. It is forbidden to
use friend functions.
The following code must work:
Buffer<int> buff; //buff = {1, 2, 3, 4, 5}
std::cout << buff << std::endl;
The result:
{ 1, 2, 3, 4, 5 }
V
Your application must call all the implemented public member
and global functions.
Class and function declarations must be put into a Buffer.h
header file, their implementations – into a Buffer.hpp file.
It is necessary to include Buffer.hpp into the source file
containing the main() function of your applicatiion.
classes,
constructors,
destructor,
operator
overloading, friend functions
  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Walle Build 100427

Copyright 代写C.