//LICA MCA II
//GTU Question 7 (Sem 2 OOCP Practical)
/*Overload all the four arithmetic
operators to operate on a vector class
and also the overload the * operator to multiply scalar values to the vector class.
Overload the >> operator to input a vector and the << operator to display the vector in the form (10,20,.....).
Also overload the [] operator to access the indivisual member of the
vector.
Use Dynamic memory allocation to achieve the solution.
Write appropriate constructor and destructure for the class */
#include
#include
#include
class vector
{
int *v;
int size;
public:
vector(){};
vector(int a)
{
size=a;
v=new int[size];
for(int i=0;i
v[i]=0;
}
}
vector operator + (vector b)
{
vector temp(size);
for(int i=0;i
temp.v[i]=v[i]+b.v[i];
}
return temp;
}
vector operator -(vector b)
{
vector temp(size);
for(int i=0;i
temp.v[i]=v[i]-b.v[i];
}
return temp;
}
vector operator /(vector b)
{
vector temp(size);
for(int i=0;i
temp.v[i]=v[i]/b.v[i];
}
return temp;
}
vector operator *(vector b)
{
vector temp(size);
for(int i=0;i
temp.v[i]=v[i]*b.v[i];
}
return temp;
}
vector operator *(int b)
{ vector temp(size);
for(int i=0;i
temp.v[i]=v[i]*b;
}
return temp;
}
void operator << (vector b)
{
for(int i=0;i
cout<
}
vector operator >> (vector b)
{
for(int i=0;i
b.v[i]=v[i];
}
}
vector operator [](int index)
{
cout<
void disp()
{
for(int i=0;i
cout<
}
void read()
{
for(int i=0;i
cin>>v[i];
}
}
};
void main()
{
clrscr();
int ch;
char conn;
vector a(5),b(5),c(c);
cout<<"Enter elements for Vector A"<
cout<<"Enter elements for Vector A"<
clrscr();
do
{
clrscr();
cout<<"1.Add + "<
switch(ch)
{
case 1:
c=a+b;
c.disp();
break;
case 2:
c=a-b;
c.disp();
break;
case 3:
c=a*b;
c.disp();
break;
case 4:
c=a/b;
c.disp();
break;
case 5:
clrscr();
int val;
cout<
c=a*val;
c.disp();
break;
case 6:
clrscr();
a>>b;
cout<
break;
case 7:
clrscr();
cout<
case 8:
int val1;
clrscr();
cout<
a[val1-1];
break;
};
fflush(0);
cout<
}while(conn=='y' || conn=='Y');
getch();
}
----------------------------
RAJ SOLUTION'S
www.rajsolution.com
www.sahinraj.blogspot.com
No comments:
Post a Comment