Part III. Foundations for Object-oriented Programming in C

Chapter 17 - Classes in C++

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Operator Overloading
You have learned that it is possible to overload member functions in a class. In this section, you will learn that it is also possible to overload C++ operators. In C++, new definitions can be applied to such familiar operators as +, -, *, and / in a given class.
The concept of operator overloading is common in numerous programming languages, even if it is not specifically implemented. For example, all compiled languages make it possible to add two integers, two floats, or two doubles (or their equivalent types) with the + operator. This is the essence of operator overloading— using the same operator on different data types. In C++, it is possible to extend this simple concept even further. In most compiled languages, it is not possible, for example, to take a complex number, matrix, or character string and add them together with the + operator.
These operations are valid in all programming languages:
3 + 8
3.3 + 7.2
These operations are typically not valid operations:
(4 - j4) + (5 + j10)
(15d 20m 45s) + (53d 57m 40s)
“combine” + “strings”
If the last three operations were possible with the + operator, the workload of the programmer would be greatly reduced when designing new applications. The good news is that in C++, the + operator can be overloaded and the previous three operations can be made valid. Many additional operators can also be overloaded. Operator overloading is used extensively in C++. You will find examples throughout the various Microsoft C++ libraries.
Overloading Operators and Function Calls
In C++, the operators shown in Table 17-1 can be overloaded.
Table 17-1: Operators that Can Be Overloaded in C++
+
?
*0
/
=
<
>
+=
?=
*0=
/=
<<
>>
>>=
<<=
= =
!=
<=
>=
++
%
&
^^
!
|
~
&=
^=
|=
&&
||
%=
[]
( )
new
Delete
The main restrictions are that the syntax and precedence of the operator must remain unchanged from its originally defined meaning. Another important point is that operator overloading is valid only within the scope of the class in which overloading occurs.
Overloading Syntax
In order to overload an operator, the operator keyword is followed by the operator itself:
type operator opr(param list)
For example:
angle_value operator +(angle_argument);
Here, angle_value is the name of the class type, followed by the operator keyword, then the operator itself (+) and a parameter to be passed to the overloaded operator.
Within the scope of a properly defined class, several angles specified in degrees/minutes/seconds could be directly added together:
char str1[] = “37d 15m 56s”;
 angle_value angle1(str1);   
 
 char str2[] = “10d 44m 44s”;
 angle_value angle2(str2);
 
 char str3[] = “75d 17m 59s”;
 angle_value angle3(str3);
 
 char str4[] = “130d 32m 54s”;
 angle_value angle4(str4);
 
 angle_value sum_of_angles;

 sum_of_angles=angle1+angle2+angle3+angle4;
In this example, the symbol for degrees is (d), for minutes (m), and for seconds (s).
The carry information from seconds-to-minutes and from minutes-to-hours must be handled properly. A carry occurs in both cases when the total number of seconds or minutes exceeds 59. This doesn’t have anything to do with operator overloading directly, but the program must take this fact into account if a correct total is to be produced, as shown here:
//
//  opover.cpp
//  C++ program illustrates operator overloading.
//  Program will overload the “+” operator so that
//  several angles, in the format degrees minutes seconds,
//  can be added directly.
//  Copyright (c) Chris H. Pappas and William H. Murray, 1998
//

#include <iostream.h>
#include <stdlib.h>
#include <string.h>
int totaldegrees, totalminutes, totalseconds;

class angle_value {
 int degrees, minutes, seconds;
 
 public:
 angle_value() {degrees=0,
                minutes=0,
                seconds=0;}  // constructor
 angle_value(char *);
 angle_value operator +(angle_value);
};

angle_value::angle_value(char *angle_sum)
{
 degrees=atoi(strtok(angle_sum,"d"));
 minutes=atoi(strtok(0,"m"));
 seconds=atoi(strtok(0,"s"));
}
angle_value angle_value::operator+(angle_value angle_sum)
{
 angle_value ang;
 ang.seconds=(seconds+angle_sum.seconds)%60;
 ang.minutes=((seconds+angle_sum.seconds)/60+
             minutes+angle_sum.minutes)%60;
 ang.degrees=((seconds+angle_sum.seconds)/60+
             minutes+angle_sum.minutes)/60;
 ang.degrees+=degrees+angle_sum.degrees;
 totaldegrees=ang.degrees;
 totalminutes=ang.minutes;
 totalseconds=ang.seconds;
 return ang;
}

main()
{
 char str1[] = “37d 15m 56s”;
 angle_value angle1(str1);   
 
 char str2[] = “10d 44m 44s”;
 angle_value angle2(str2);
 
 char str3[] = “75d 17m 59s”;
 angle_value angle3(str3);
 char str4[] = “130d 32m 54s”;
 angle_value angle4(str4);
 
 angle_value sum_of_angles;

 sum_of_angles=angle1+angle2+angle3+angle4;
 cout << “The sum of the angles is ” << totaldegrees << “d ”
      << totalminutes << “m ” << totalseconds << “s”  << endl;

 return (0);
}
The following portion of code shows how the mixed units are added together. Here, the + operator is to be overloaded:
  .
  .
  .
 ang.seconds=(seconds+angle_sum.seconds)%60;
 ang.minutes=((seconds+angle_sum.seconds)/60+
             minutes+angle_sum.minutes)%60;
 ang.degrees=((seconds+angle_sum.seconds)/60+
             minutes+angle_sum.minutes)/60;
 ang.degrees+=degrees+angle_sum.degrees;
  .
  .
  .
The divide and modulus operations are performed on the sums to ensure correct carry information.
Further details of the program’s operation are omitted since you have seen most of the functions and modules in earlier examples. However, it is important to remember that when you overload operators, proper operator syntax and precedence must be maintained.
The output from this program shows the sum of the four angles to be as follows:
The sum of the angles is 253d 51m 33s
Is this answer correct?

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net