...::: Recent Updates :::...

Sunday, March 20, 2011

GTU Question 7 (Sem 2 OOCP Practical)

//Coded By - Raj Sahin N.
//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"< a.read();
cout<<"Enter elements for Vector A"< b.read();
clrscr();
do
{
clrscr();
cout<<"1.Add + "< cout<<"2.Sub - "< cout<<"3.Mul * "< cout<<"4.Div / "< cout<<"5.Mul Value"< cout<<"6.Copy >> "< cout<<"7.Disp << "< cout<<"8.Disp Item v[i] "< cout< cin>>ch;
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< cin>>val;
c=a*val;
c.disp();
break;
case 6:
clrscr();
a>>b;
cout< b.disp();
break;
case 7:
clrscr();
cout< a< break;
case 8:
int val1;
clrscr();
cout< cin>>val1;
a[val1-1];
break;



};
fflush(0);
cout< cin>>conn;

}while(conn=='y' || conn=='Y');
getch();
}

----------------------------
RAJ SOLUTION'S
www.rajsolution.com
www.sahinraj.blogspot.com

No comments:

Post a Comment