Question
C++ templated function question Define a family of functions (a templated function) named insertAtEnd that inserts in a dynamically-allocated array of elements of any type
C++ templated function question
Define a family of functions (a templated function) named insertAtEnd that inserts in a dynamically-allocated array of elements of any type another element at the end (resize the array). The function should receive as parameters:
- the dynamically-allocated array
- the size of the array
- the element to insert
The function should return the resulted array.
Specialize the function for the type char. In this specialization the array must be null-terminated; the size parameter doesnt count the null-byte.
The client code listed below uses your templated function, and should not contain memory leaks. The comments next to each statement shows the content the array should have after the statement is executed.
A type that uses this template must include in its definition certain functions and/or operators. Identify each function and/or operator that your template assumes is defined. You may do so in the form of an exact prototype or an English descriptive phrase.
Write your solution in the textbox below.
// assume all necessary headers have been included int main() { { int* arrI = nullptr; arrI = insertAtEnd(arrI, 0, 1); // 1 arrI = insertAtEnd(arrI, 1, 5); // 1, 5 arrI = insertAtEnd(arrI, 2, -3);// 1, 5, -3 delete[] arrI; } { double* arrD = nullptr; arrD = insertAtEnd(arrD, 0, 1.2);// 1.2 arrD = insertAtEnd(arrD, 1, 2.3);// 1.2, 2.3 arrD = insertAtEnd(arrD, 2, 3.4);// 1.2, 2.3, 3.4 delete[] arrD; } { char* arrC = nullptr; arrC = insertAtEnd(arrC, 0, 'a');// a\0 arrC = insertAtEnd(arrC, 1, 'b');// ab\0 arrC = insertAtEnd(arrC, 2, 'c');// abc\0 cout << arrC; delete[] arrC; } }
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