|
 |
 |
[an error occurred while processing this directive]
- 3. Testing the overloaded operators
Create the following example to test the operator:
#include <iostream>
using namespace std;
#include ddate.h
int main()
{
DDate Date1, Date2, Date3;
Date1.SetDate (19, 05, 1998);
Date2.SetDate (19, 05, 1998);
Date3.SetDate (19, 01, 1998);
cout << "The first date ";
if (Date1 > Date2)
cout << "is ";
else
cout << "is not ";
cout << "greater than the second date" << endl;
cout << "The first date ";
if (Date1 == Date2)
cout << "is ";
else
cout << "is not ";
cout << "equal to the second date" << endl;
cout << "The first date ";
if (Date1 < Date3)
cout << "is ";
else
cout << "is not ";
cout << "less than the third date" << endl;
return 0;
}
First of all, the code defines the Date1, Date2, and Date3 objects and initializes them with the dates May 19, 1998, May 19, 1998, and January 19, 1998. Then the dates are compared using the overloaded operators. The result should be
The first date in not greater than the second date
The first date is equal to the second date
The first date is not less than the third date
Comments
Operator overloading is a very powerful feature of C++. The idea behind it is to make code more readable and intuitive. If overloaded operators dont serve this idea, the results can be terrible. Lets consider the following code:
int operator== (int InValue)
{
if (InValue > 5 ) then return 5;
else return InValue;
}
In this example, the equality operator (==) was overloaded with a function that returns the lesser of 5 and the function argument. If another programmer wants to use this function to compare two variables, just imagine what the result could be.
4.6 Provide access to encapsulated data to certain classes?
Problem
In How-To 4.4, we created an overloaded operator for adding two complex numbers. I understand that if I want to use the + operator in the expressions like
Complex1 + 2.3;
or
Complex1 + FloatNumber;
where FloatNumber is a variable of a type float, I could define the overloaded operator. I simply have to change the declaration to
CComplex operator+(float);
and the definition to
CComplex CComplex::operator+(float float1)
{
CComplex NewComplex;
NewComplex.rreal = rreal + float1;
NewComplex.iimage = iimage;
return NewComplex;
}
What should I do if I want to use expressions like
2.3 + Complex1;?
I cant overload this operator because I would have to simulate the function
CComplex CComplex::Add(float, CComplex)
and I have no idea how to do it. I would use
CComplex operator+(float, CComplex)
as a normal function but I need to have direct access to the private data of CComplex class. How do I use the private data without breaching the encapsulation?
Technique
C++ introduced the notion of friend functions and friend classes. A friend function can access private data of a class without being a member function of the class. In the following example, the FFriend function can access the private data of the TestClass class.
Class TestClass
{
private:
public:
friend int FFriend(int);
};
int FFriend(int data)
{
}
We are going to create friend functions for our CComplex class that will support + and - operators in expressions like
2.3 + Complex1
4.76 - Complex1;
Steps
- 1. Analyze the class and specify operations that cannot be represented by member functions
This step is very important. The idea of friend functions has been discussed a lot, and it has both good points and bad points. The biggest problem pointed out by many writers is that the friend functions break the wall that encapsulation creates. However, we can follow this rule: Dont use friend functions if you dont have to. A limited usage of friend functions increases the power of C++ and can significantly improve the readability of the code. In our example that created the CComplex class, we figured out a way of supporting + and - operators for a few types of expressions. The only type of arithmetic expression in which + and - cant be represented by member functions is with a float variable as the first argument.
Therefore, we start creating functions to support expressions such as
2.3 + Complex1;
- 2. Declaring a friend function
We already know that the function declarations should be
friend CComplex operator+(float, CComplex);
and
friend CComplex operator-(float, CComplex);
A friend function declaration can be placed in either the private or public part of a class declaration. We prefer to put it in the public section to show the accessibility of the function.
Even when declared within the class declaration, a friend function is not a member function of this class.
- 3. Writing the function definition
The function definition is no different from the other functions. For our + and - operations, the functions could be
// + operator support for the cases
// 2.3 + Complex1
//
CComplex operator+(float float1, CComplex Complex1)
{
CComplex NewComplex;
NewComplex.rreal = float1 + Complex1.rreal;
NewComplex.iimage = Complex1.iimage;
return NewComplex;
}
// - operator support for the cases
// 2.3 - Complex1
//
CComplex operator+(float float1, CComplex Complex1)
{
CComplex NewComplex;
NewComplex.rreal = float1 - Complex1.rreal;
NewComplex.iimage = Complex1.iimage;
return NewComplex;
}
A similar approach could be used if we wanted to add support for other data types.
|