Creating Dynamic Array using C++
#include <iostream>
using namespace std;
template <typename T>
class Array
{
private:
T* base;
int size;
public :
Array(T arr[], int length)
{
base = new T[sizeof(arr)];
size = length;
for (int i = 0; i < length; i++)
base[i] = arr[i];
}
void print();
};
template <typename T>
void Array<T>::print()
{
for (int i = 0; i < size; i++)
cout << " " << *(base + i) << endl;
}
int main() {
int arr1[] = {10, 20, 30};
Array<int> obj(arr1, 3);
obj.print();
char arr2[] = { 'v', 'a', 'r','u','n' };
Array<char> obj1(arr2, (sizeof(arr2) / sizeof(arr2[0])));
obj1.print();
return 0;
}
Comments
Post a Comment