In C++, please explain with comments and show your outputs. I need to create a template class called TBuffer in a TBuffer.hpp from editing the IntgerBuffer Code, and please explain the changes as well.
IntegerBuffer
#include #include class IntegerBuffer { protected: int data[32]; int capacity; int data_length; int zero_value; public: IntegerBuffer() : capacity(32), data_length(0), zero_value(0) {}; int add(int value); int add(const int array[], int length); void print(int col_width, int row_width) const; }; int IntegerBuffer::add(int value) { if (dataLength < capacity) { data[dataLength] = value; ++dataLength; return 1; } else return 0; } int IntegerBuffer::add(const int array[], int length) { int count = 0; for (int i = 0; i < length; ++i) count += add(array[i]); return count; } void IntegerBuffer::print(int col_width, int row_width) const { for (int i = 0; i < dataLength; ++i) { // check if value should start a new row if (i % row_width == 0 && i > 0) std::cout << ' '; std::cout << std::setw(col_width) << data[i]; } std::cout << std::endl; }
Code of hw4.cpp
#include #include #include "TBuffer.hpp" using namespace std; int main() { TBuffer ib(0); TBuffer db(0.0); TBuffer sb(""); int input_data[10] = { 12, -4, 6, 8, 6, 5, -4, -6, 11, 9 }; ib.add(7); ib.add(input_data, 9); ib.add(12); ib.add(17); ib.print(5, 10); cout << " Sum: " << ib.sum() << endl << endl; db.add(8.463); db.add(3.74); db.add(5.231); db.add(3.74); db.print(7, 10); cout << " Sum: " << db.sum() << endl << endl; sb.add("Turing"); sb.add("Kleene"); sb.add("Church"); sb.add("Russell"); sb.add("Hoare"); sb.add("McCarthy"); sb.add("Chomsky"); sb.add("Frege"); sb.add("Jaffe"); sb.print(10, 7); cout << " Sum: " << sb.sum() << endl; }
Output of the code:
7 12 -4 6 8 6 5 -4 -6 Sum: 30 8.463 3.74 5.231 3.74 Sum: 21.174 Turing Kleene Church Russell Hoare McCarthy Chomsky Frege Jaffe Sum: TuringKleeneChurchRussellHoareMcCarthyChomskyFregeJaffe