In: Computer Science
This is c++. The goal is to test each method function by writing a simple program displaying how they work in int main .
#ifndef ARRAY_FUNCTIONS_H
#define ARRAY_FUNCTIONS_H
#include <iostream>
#include <iomanip>
using namespace std;
template <typename T>
class Array_functions
{
public:
int size;
int cap;
int* ptr;
//default ctor
Array_functions();
// ctor with one arg
Array_functions(int size);
//allactes a dynamic array
T* allocate(int n);
//resizes array onto another
T* resize_arr(T *dest,int old_cap, int new_cap);
//deletes array
void delete_arr(T& a);
//prints array
T* copy_arr(T*src, int size);
};
template <typename T>
Array_functions<T>::Array_functions(){
const bool debug = true;
size = 0;
cap = 0;
ptr = nullptr;
cout<< "debug Array_functions(): size : "<< size<<" cap : "<< cap;
}
template <typename T>
Array_functions<T>::Array_functions(int size){
size = 50;
ptr = allocate(cap + 1);
cout<< "debug Array_functions(): size : "<< size <<" ptr : ";
}
template <typename T>
T* Array_functions<T>::allocate(int capacity){
const bool debug = true;
if(debug)
cout<< " debug: allocate(int n) "<<endl;
return new T[capacity];
}
template <typename T>
T* Array_functions<T>::resize_arr(T *dest,int size, int new_cap){
dest = allocate(new_cap);
copy_arr(dest, size);
return *dest;
}
template <typename T>
void Array_functions<T>::delete_arr(T& arr){
delete []arr;
arr = nullptr;
}
template <typename T>
//Print array pointer
T* Array_functions<T>::copy_arr(T* src,
int size){
T* new_arr;
for(int i = 0; i < size; i++){
cout<< "Constents of list :"<<endl;
cout<< setw(4)<< *src <<endl;
new_arr = src;
new_arr++;
src++; //list array increments
}
delete_arr(src);
new_arr -=size;
cout<< endl;
return *new_arr;
}
#endif // ARRAY_FUNCTIONS_H
#include <iostream> #include "array_functions" using namespace std; int main(){ return 0; }
OUTPUT:
CODE:
#ifndef ARRAY_FUNCTIONS_H
#define ARRAY_FUNCTIONS_H
#include <iostream>
#include <iomanip>
using namespace std;
template <typename T>
class Array_functions
{
public:
int size;
int cap;
int* ptr;
//default ctor
Array_functions();
// ctor with one arg
Array_functions(int size);
//allactes a dynamic array
T* allocate(int n);
//resizes array onto another
T* resize_arr(T* dest, int old_cap, int new_cap);
//deletes array
void delete_arr(T& a);
//prints array
T* copy_arr(T* src, int size);
};
template <typename T>
Array_functions<T>::Array_functions() {
const bool debug = true;
size = 0;
cap = 0;
ptr = nullptr;
cout << "debug Array_functions(): size : " << size
<< " cap : " << cap;
}
template <typename T>
Array_functions<T>::Array_functions(int size) {
size = 50;
ptr = allocate(cap + 1);
cout << "debug Array_functions(): size : " << size
<< " ptr : ";
}
template <typename T>
T* Array_functions<T>::allocate(int capacity) {
const bool debug = true;
if (debug)
cout << " debug: allocate(int n) " << endl;
return new T[capacity];
}
template <typename T>
T* Array_functions<T>::resize_arr(T* dest, int size, int
new_cap) {
dest = allocate(new_cap);
copy_arr(dest, size);
return dest;
}
template <typename T>
void Array_functions<T>::delete_arr(T& arr) {
delete[]arr;
arr = nullptr;
}
template <typename T>
//Print array pointer
T* Array_functions<T>::copy_arr(T* src,
int size) {
T* new_arr = NULL;
for (int i = 0; i < size; i++) {
cout << "Constents of list :" << endl;
cout << setw(4) << *src << endl;
new_arr = src;
new_arr++;
src++; //list array increments
}
new_arr -= size;
cout << endl;
return new_arr;
}
#endif // ARRAY_FUNCTIONS_H
#include <iostream>
using namespace std;
int main() {
Array_functions<int> arr;
Array_functions<int> arr1(5);
int samplearr[] = { 2,3,4,1,5,6 };
arr.copy_arr(samplearr, 6);
arr1.allocate(10);
arr.resize_arr(samplearr, 6, 10);
return 0;
}