Overloading Arithmetic Operator ‘ + ‘ Using A friend function
//header files
class Distance
{
private:
int feet;
float inches;
public:
Distance(int i,
float f) // two argument
constructor
{
feet=i;
inches=f;
}
friend operator +(Distance, Distance); //prototype of operator overloading
function for +
};
Distance
operator + (Distanced1, Distance d2)
{
return
Distance (d1.feet+d2.feet , d1.inches+d2.inches);
}
void
main()
{
Distance d1(4,5), d2(2,6), d3;
d3=d1+d2; //
+ operator overloaded.
cout<<d3.feet<<d3.inches;
}
No comments:
Post a Comment