Tuesday, 29 May 2012

Hi friends,
Here i am explain you concept of operator overloading using both friend function and without friend function:

First we see i it is implemented without friend function and what it's anomaly  that we will going ti consider operator overloading using friend function.

Here first i have provided both link and code for O.overloading without friend function.
Download : operatorol.cpp


Here is code:


#include <iostream.h>
#include <conio.h>


class new_vector
{
int a[3];
public:
new_vector()
{}
void input()
{
for(int i=0;i<3;i++)
{
cout<<"Enter vector elements:";
cin>>a[i];
}
cout<<endl;
}


void display()
{
for(int i=0;i<3;i++)
{
cout<<"Enter vector element:"<<a[i]<<endl;
}
}


new_vector  operator+(new_vector v)
{
new_vector v1;
for(int i=0;i<3;i++)
{
v1.a[i]=a[i]+v.a[i];
}
return v1;
}


friend new_vector operator-(new_vector, new_vector);


};


new_vector operator-(new_vector v1,new_vector v2)
{
new_vector v3;
for(int i=0;i<3;i++)
{
v3.a[i]=v1.a[i]-v2.a[i];
}
return v3;
}


void main()
{
new_vector v,v1,v2;
clrscr();
v.input();
v2.input();
v1 = v + v2;
v1.display();
v1 = v2 - v;
v1.display();
getch();


}


Though we can achive both overloading concept in one program i have not do it b'cos by sepparating the program became more readble.

Now first have a look at output of above program  that  adds the two one dimensional matrix by using object that is user defined data type thats what  operator overloading is all about:

Now talk about it's restriction that we can not use built in data type on the left side of any overloaded operartor that is for exp. "ob1= ob2 + 3;" is legal but  "ob1= 3 + ob2;" is illegal. there is a reason behind it because when we overload opeartor without friend function only one argument is passed explicitly as you would have observe it in above program like we do for exp. "ob.add(ob1);" i which invoking object is passed implicitly. That's the reason that we can not use built in type on the left side of overloaded operator b'cos it's already say for reserved for invoking object.

So here friend function come in place for implementing things like "ob1 = 3 + ob2;"

However friend function is very useful in another way also  but here for the sake of our discussion we exclude it, thugh if you want to learn another uses of friend function visit my site here:


Now i am first going to provide .cpp files for download if you like to yourself run it then just download and run its all tested , here below is link:
operator overloading with friend function  

And here code have a look at it :


#include <iostream.h>
#include <conio.h>

class new_vector
{
int a[3];
public:
new_vector()
{}
void input()
{
for(int i=0;i<3;i++)
{
cout<<"Enter vector elements:";
cin>>a[i];
}
cout<<endl;
}

void display()
{
for(int i=0;i<3;i++)
{
cout<<"Enter vector element:"<<a[i]<<endl;
}
}


friend new_vector operator-(new_vector, new_vector);

};

new_vector operator-(new_vector v1,new_vector v2)
{
new_vector v3;
for(int i=0;i<3;i++)
{
v3.a[i]=v1.a[i]-v2.a[i];
}
return v3;
}

void main()
{
new_vector v,v1,v2;
clrscr();
v.input();
v2.input();

v1 = v2 - v;
v1.display();
getch();

}


You may be thought why i have not use built in data imy exp. that b'cos  it's easy to implement then matrix that you should try  to test how  much  you understand the concept!

try implementing like "ob1 = 23 + ob2;" in your program.

However here is output for above code:

For any other tutorial on c++,java,vb.net,visual basic, ms access, sql visit my site at:


No comments:

Post a Comment