In: Computer Science
class A
{
public:
//constructors //
other members private:
int a;
int b;
};
Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
a) as friend function
class A
{
private:
int a;
int b;
public:
A(int a,int b)
{
this->a = a;
this->b = b;
}
friend A operator +(const A &obj1,const A
&obj2); // declared friend as public method
};
b) as member function
class A
{
private:
int a;
int b;
public:
A(int a,int b)
{
this->a = a;
this->b = b;
}
A operator +(const A &obj); // public
method inside the class
};
c) as non-friend, non-member function
class A
{
private:
int a;
int b;
public:
A(int a,int b)
{
this->a = a;
this->b = b;
}
// net public get methods to access and b outside class
int getA(){
return a;
}
int getB(){
return b;
}
};
A operator +( A &obj1, A &obj2); // outside the class
Do ask if any doubt.