Write a program in C++, which perform two operator overloading function for A=2*B,where A and B are the objects of the same class.
#include <iostream>
using namespace std;
class Number
{
private:
double number;
public:
Number(){}
Number(double number)
{
this->number = number;
}
double getNumber()
{
return number;
}
friend Number operator *(double number, Number otherNumber)
{
Number result(otherNumber.getNumber()*number);
return result;
}
friend Number operator *(Number other, double value)
{
return value*other;
}
};
int main()
{
Number A(5);
Number B(6);
A = 2*B;
cout << "A = " << A.getNumber() << endl;
cout << "B = " << B.getNumber();
int k;
cin>>k;
return 0;
}
Comments
Leave a comment