can we use only one argument in operation overloading function as a parameter!
here is error of this code
1.‘com com::operator+(com, com)’ must take either zero or one argument|
2.extra qualification com::’ on member ‘operator+’ [-fpermissive]|
Extra qualification: com:: is not needed to be written when you are inside the com class.
1 2 3 4 5 6 7
class com{
double x,y;
public:
void getdata(double,double);
void display(com);
com operator +(com c,com d);
};
You can overload operator+ in two ways: internally to the com class (member function), or externally (global function).
If you overload it internally, it will look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class com{
double x,y;
public:
void getdata(double,double);
void display(com);
com operator +(com b);
};
com com::operator + (com b)
{
com fin;
// notice that we use our current x and y
fin.x = x + b.x;
fin.y = y + b.y;
return fin;
}
If you overload it externally, you do it exactly as you did already, but do not declare it in your com class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class com{
double x,y;
public:
void getdata(double,double);
void display(com);
friend com operator +(com c,com d);
// a friend function can access private members, in our case x and y
};
// no com:: this time because it's not part of the com class!
com operator +(com c,com d){
com finl;
finl.x=c.x+d.x;
finl.y=c.y+d.y;
return(finl);
}
what you are saying i got that but my main doubt is that can we use more than one argument in my code in 9th line like this
com operator +(com c,com d);
it is again giving error like ----.‘com com::operator+(com, com)’ must take either zero or one argument|
# include <iostream>
usingnamespace std;
class com
{
double x,y;
public:
void getdata(double,double);
void display(void);// This function does not necessirly need that argument
friend com operator +(com c,com d);
com operator =(com sum);
};
void com::getdata (double a,double b)
{
x=a;
y=b;
}
void com::display(void)
{
cout << x << "+i" << y << endl;
}
com operator +(com c,com d)
{
com finl;
finl.x = c.x + d.x;
finl.y = c.y + d.y;
return(finl);
}
com com::operator =(com sum)
{
x = sum.x;
y = sum.y;
return(*this);
}
int main()
{
com c1,c2,c3;
c1.getdata(2.3,3.4);
c2.getdata(5.2,4.4);
c3=c1+c2;
c3.display();
return 0;
}
At this case ,It's not the + operator overloading you're looking for,I think you need to overload the () ,revise @ne555 for the zero,one parameter problem
# include <iostream>
usingnamespace std;
class com
{
double x,y;
public:
void getdata(double,double);
void display(void);// This function does not necessirly need that argument
com operator ()(com c,com d);
com operator =(com sum);
};
void com::getdata (double a,double b)
{
x=a;
y=b;
}
void com::display(void)
{
cout << x << "+i" << y << endl;
}
com com::operator ()(com c,com d)
{
com finl;
finl.x= x + c.x+d.x;
finl.y= y + c.y+d.y;
return(finl);
}
com com::operator =(com sum)
{
x = sum.x;
y = sum.y;
return(*this);
}
int main()
{
com c1,c2,c3;
c1.getdata(2.3,3.4);
c2.getdata(5.2,4.4);
c3.getdata(0.0,0.0);
c3 = c3(c1,c2);
c3.display();
return 0;
}