Overload the unary operator - - on a Distance class object, which represents distance as feet and inch, so that we get the previous distance as output. Include a constructor and a destructor which always keeps track of the number of objects present at any time.
Answers
Answer:
I don't know my dear friend so please make me bralinist,votes
Answer:
Types of Operator Overloading in C++
Operator Overloading:
C++ provides a special function to change the current functionality of some operators within its class which is often called as operator overloading. Operator Overloading is the method by which we can change the function of some specific operators to do some different task.
This can be done by declaring the function, its syntax is,
Return_Type classname :: operator op(Argument list)
{
Function Body
}
In the above syntax Return_Type is value type to be returned to another object, operator op is the function where the operator is a keyword and op is the operator to be overloaded.
Operator function must be either non-static (member function) or friend function.
Operator Overloading can be done by using three approaches, they are
Overloading unary operator.
Overloading binary operator.
Overloading binary operator using a friend function.
Below are some criteria/rules to define the operator function:
In case of a non-static function, the binary operator should have only one argument and unary should not have an argument.
In the case of a friend function, the binary operator should have only two argument and unary should have only one argument.
All the class member object should be public if operator overloading is implemented.
Operators that cannot be overloaded are . .* :: ?:
Operator cannot be used to overload when declaring that function as friend function = () [] ->.
Refer this, for more rules of Operator Overloading
Overloading Unary Operator: Let us consider to overload (-) unary operator. In unary operator function, no arguments should be passed. It works only with one class objects. It is a overloading of an operator operating on a single operand.
Example:
Assume that class Distance takes two member object i.e. feet and inches, create a function by which Distance object should decrement the value of feet and inches by 1 (having single operand of Distance Type).
// C++ program to show unary operator overloading
#include <iostream>
using namespace std;
class Distance {
public:
// Member Object
int feet, inch;
// Constructor to initialize the object's value
Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}
// Overloading(-) operator to perform decrement
// operation of Distance object
void operator-()
{
feet--;
inch--;
cout << "\nFeet & Inches(Decrement): " << feet << "'" << inch;
}
};
// Driver Code
int main()
{
// Declare and Initialize the constructor
Distance d1(8, 9);
// Use (-) unary operator by single operand
-d1;
return 0;
}
Output:
Feet & Inches(Decrement): 7'8
In the above program, it shows that no argument is passed and no return_type value is returned, because unary operator works on a single operand. (-) operator change the functionality to its member function.
Note: d2 = -d1 will not work, because operator-() does not return any value.
Overloading Binary Operator: In binary operator overloading function, there should be one argument to be passed. It is overloading of an operator operating on two operands.
Let’s take the same example of class Distance, but this time, add two distance objects.