WAP to create a class which stores a dynamic integer array and its size. Include all
the constructors and destructor in the class. Store two arrays of different size in
two objects. Join the two arrays and and store it in another object using a member
function.
#include<iostream>
using namespace std;
class dynamicArray{
private:
int arr[];
int limit;
public:
dynamicArray(){
}
dynamicArray(int max){
limit = max;
arr[limit];
}
int getLimit(){
return limit;
}
void join_2_arrays(){
cout<<"Enter the size of the first array:\n";
int n;
cin>>n;
cout<<"Enter the size of the second array:\n";
int m;
cin>>m;
int arr1[n];
cout<<"Enter elements of the first array: ";
for(int i=0; i<n; i++){
cout<<"Element: "<<(i+1)<<endl;
cin>>arr1[i];
}
int arr2[m];
cout<<"Enter elements of the second array: \n";
for(int i=0; i<m; i++){
cout<<"Element: "<<(i+1)<<endl;
cin>>arr2[i];
}
cout<<"Elements of the first array are: \n";
for(int i=0; i<n; i++){
cout<<"Element "<<(i+1)<<" :"<<arr1[i]<<endl;
}
cout<<"Elements of the second array are: \n";
for(int i=0; i<m; i++){
cout<<"Element "<<(i+1)<<" :"<<arr2[i]<<endl;
}
int a =m+ n;
int arr3[a];
for(int i=0; i<n; i++){
arr3[i] = arr1[i];
}
int x = a- 1;
for(int i=0, j=x; i<m; i++, j-- ){
arr3[j] = arr2[i];
}
cout<<"Elements of the third array are: \n";
for(int i=0; i<a; i++){
cout<<"Element "<<(i+1)<<" :"<<arr3[i]<<endl;
}
}
~dynamicArray(){
cout<<"Destructor called\n";
}
};
int main(){
dynamicArray d;
d.join_2_arrays();
}
Comments
Leave a comment