. Write a program to overload binary – (minus) operator for Number class using friend function. Number class have two data members as num1 and num2.
#include <iostream>
class Number
{
public:
void setNumber(float n, float m)
{
num_1 = n;
num_2 = m;
}
void viewNumber()
{
std::cout << "num_1=" << num_1 << std::endl;
std::cout << "num_2=" << num_2 << std::endl;
}
friend Number operator - (const Number &n1, const Number &n2);
Number(){};
Number(float n, float m)
{
num_1 = n;
num_2 = m;
}
private:
double num_1{ 0 };
double num_2{ 0 };
};
Number operator - (const Number& n1, const Number& n2)
{
return Number(n1.num_1 - n2.num_1, n1.num_2 - n2.num_2);
}
int main()
{
//test work
Number t1(25, 64);
Number t2(47, 21);
Number t3;
//before
t3.viewNumber();
t3 = t1 - t2;
//after
t3.viewNumber();
return 0;
}
Comments
Leave a comment