Lab 6 Binaryoperator Overloading OOP
Lab 6 Binaryoperator Overloading OOP
Experiment No 6
TITLE:
Write a program in C++ to perform following operations on complex numbers Add, Subtract, Multiply, Divide. Use
operator overloading for these operations.
APPARATUS: Computer with Windows Operating System. CODE BLOCK or Eclipse IDE (Neon 3). Or Dev C++
THEORY:
C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is
called function overloading and operator overloading respectively. An overloaded declaration is a declaration that
is declared with the same name as a previously declared declaration in the same scope, except that both
declarations have different arguments and obviously different definition (implementation).
When you call an overloaded function or operator, the compiler determines the most appropriate definition to use,
by comparing the argument types you have used to call the function or operator with the parameter types specified
in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload
resolution.
Binary Operator Overloading in C++
In the binary operator overloading function, there should be one argument to be passed. It is the overloading of an
operator operating on two operands. Below is the C++ program to show the overloading of the binary operator (+)
using a class Distance with two distant objects.
class Distance {
public:
Distance()
{
this->feet = 0;
this->inch = 0;
}
Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}
// Driver Code
int main()
{
Distance d1(8, 9);
Distance d2(10, 2);
Distance d3;
Distance()
{
this->feet = 0;
this->inch = 0;
}
Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}
// Driver Code
int main()
{
Distance d1(8, 9);
Distance d2(10, 2);
Distance d3;
The operator function is implemented outside of the class scope by declaring that function as the
friend function.
In these ways, an operator can be overloaded to perform certain tasks by changing the functionality
of operators
COMMENTS AND CONCLUSION: