15. Create a class Array which contains – int *ptr – int n Write a menu driven program : – to Accept an array from user – to display it. – to add two arrays using operator overloading. – to subtract two arrays using operator overloading.


#include<iostream.h>
#include<conio.h>
class array
{
int *ptr,n;
public:
void get();
void disp();
void operator+(array&);
void operator-(array&);
};
void array ::get()
{
cout<<“\nEnter array size: \n”;
cin>>n;
ptr=new int[n];
cout<<“\nEnter array element: \n”;
for(int i=0;i<n;i++)
{
cin>>ptr[i];
}
}
void array::disp()
{
cout<<“\nArray element: “;
for(int i=0;i<n;i++)
{
cout<<ptr[i]<<” “;
}
}
void array:: operator+(array &t)
{
array temp;
for(int i=0;i<n;i++)
{
temp.ptr[i]=ptr[i]+t.ptr[i];
}
cout<<“\nAddition of array: “;
for(i=0;i<n;i++)
{
cout<<temp.ptr[i]<<” “;
}
}
void array:: operator-(array &t)
{
array temp;
for(int i=0;i<n;i++)
{
temp.ptr[i]=ptr[i]-t.ptr[i];
}
cout<<“\nSutraction of array: “;
for(i=0;i<n;i++)
{
cout<<temp.ptr[i]<<” “;
}
}
void main()
{
int ch;
array a1,a2;
clrscr();
do
{
cout<<“\n\tMENU:\n”;
cout<<“\n1.To Accept an array from user”;
cout<<“\n2.To display it.”;
cout<<“\n3.To add two array\n4.subtract two arrays\n5.Exit\n “;
cout<<“\n\tEnter youer choice: \n”;
cin>>ch;
switch(ch)
{
case 1:
a1.get();
a2.get();
break;
case 2:
a1.disp();
a2.disp();
break;
case 3:
a1+a2;
break;
case 4:
a1-a2;
break;
case 5:
break;
}
}while(ch!=5);
getch();
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s