Search This Blog

Monday, 12 November 2012

Write a program for merging two arrays using operator overloading

#include<iostream.h>
#include<conio.h>
class Array{
int a[50];
int n;
public:
void getArray();
void putArray();
Array operator+(Array);
};
void Array::getArray()
{
cout<<"Get the size of array";
cin>>n;
cout<<"Get Elements one by one";
for(int i=0;i<n;i++)
cin>>a[i];
}
void Array::putArray()
{
cout<<"Display the Elements of array one by one\n";
for(int i=0;i<n;i++)
cout<<a[i]<<"\n";
}
Array Array::operator+(Array B)
{
int i,j;
Array T;
T.n=n+B.n;
for(i=n;i<n;i++)
T.a[i]=a[i];
for(j=0;j<B.n;j++,i++)
T.a[i]=B.a[j];
return T;
}
void main()
{
clrscr();
Array A,B,C;
A.getArray();
B.getArray();
C=A+B;
cout<<"Result Array";
C.putArray();
getch();
}

Output:
Get the Size of n
3
Get Elements one by one
10 20 30
Get the size of n
2
Get Elements one by one
60 70
Result Array
10 20 30 60 70


1 comment:

  1. i don't think this will work. check this line
    for(i=n;i<n;i++)

    ReplyDelete