|
 |
 |
[an error occurred while processing this directive]
Steps
- 1. Create a complex number class
The class we define in this example will have only add and subtract operations. Creating multiplication and division operations is a good exercise that will be left to the reader.
// CComplex class declaration
// file ccomplex.h
class CComplex
{
private:
float rreal; // real part
float iimage; // imaginary part
public:
CComplex(void); // constructor
~CComplex(void); // destructor
// access functions
void GetComplex(void);
void DisplayComplex(void);
// arithmetic operations
CComplex Add(CComplex);
CComplex Subtract(CComplex);
};
// CComplex class definition
// file complex.cpp
#include <iostream.h>
#include ccomplex.h
CComplex::CComplex()
{
rreal = 0.0;
iimage = 0.0;
}
CComplex::~CComplex()
{
}
// gets real and imaginary parts of the complex number
void CComplex::GetComplex()
{
cout << "Enter Real Part: ";
cin >> rreal;
cout << "Enter Imaginary Part: ";
cin >> iimage;
}
// displays the object of class CComplex
void CComplex::DisplayComplex()
{
cout << rreal;
if (iimage >= 0)
cout << " + " << iimage;
else
cout << " - " << -iimage;
cout << "i" << endl;
}
// adds two objects of class CComplex
CComplex CComplex::Add(CComplex Complex1)
{
CComplex NewComplex; NewComplex.rreal = rreal + Complex1.rreal;
NewComplex.iimage = iimage + Complex1.iimage;
return NewComplex;
}
// subtracts CComplex object form another CComplex object
CComplex CComplex::Subtract(CComplex Complex1)
{
CComplex NewComplex;
NewComplex.rreal = rreal - Complex1.rreal;
NewComplex.iimage = iimage - Complex1.iimage;
return NewComplex;
}
The class we created consists of rreal and iimage data members that are used for the real and imaginary parts of a complex number. We specified float data types for both parts.
The class supports access functions such as GetComplex and DisplayComplex for inputting the real and imaginary parts and displaying the complex number on the users monitor screen.
- 2. Testing the class
To test the class we created a small program that accepts two complex numbers (Complex1 and Complex2), adds them together, and stores the result in the Complex3 object of type CComplex. The program then subtracts Complex2 from Complex1 and stores the result in the Complex4 object. Finally, the program shows Complex3 and Complex4 on the user screen.
// file comptest.cpp
#include ccomplex.h
int main()
{
// define complex numbers
CComplex TestComplex1, TestComplex2;
CComplex TestComplex3, TestComplex4;
// perform keyboard input of Complex1 and Complex2
TestComplex1.GetComplex();
TestComplex2.GetComplex();
// add and subtract Complex1 and Complex2
TestComplex3 = TestComplex1.Add(TestComplex2);
TestComplex4 = TestComplex1.Subtract(TestComplex2);
// display the result
TestComplex3.DisplayComplex();
TestComplex4.DisplayComplex();
return 0;
}
- 3. Replacing Add and Subtract member functions with overloaded operators
To use convenient notations for add and subtract operations, we have to replace the Add and Subtract member functions with overloaded operators.
In the class declaration section (file ccomplex.h), we replace the lines
CComplex Add(CComplex);
CComplex Subtract(CComplex);
with the lines
CComplex operator+(CComplex);
CComplex operator-(CComplex);
In the class definition section (file ccomplex.cpp), we replace the Add and Subtract functions with the following code:
CComplex CComplex::operator+(CComplex Complex1)
{
CComplex NewComplex;
NewComplex.rreal = rreal + Complex1.rreal;
NewComplex.iimage = iimage + Complex1.iimage;
return NewComplex;
}
CComplex CComplex::operator-(CComplex Complex1)
{
CComplex NewComplex;
NewComplex.rreal = rreal - Complex1.rreal;
NewComplex.iimage = iimage - Complex1.iimage;
return NewComplex;
}
Interestingly, the function bodies remain the same. We didnt change the actual code. We just changed the function names from Add to operator+ and from Subtract to operator-.
- 4. Testing the new operators
To test the + and - operators, we just replace the lines
TestComplex3 = TestComplex1.Add(TestComplex2);
TestComplex4 = TestComplex1.Subtract(TestComplex2);
with the following lines:
TestComplex3 = TestComplex1 + TestComplex2;
TestComplex4 = TestComplex1 - TestComplex2;
Would you believe it? The program displays the same results.
Comments
Operator overloading is a very powerful feature of C++. Good programming style assures code readability is dramatically increased with operator overloading. Creating new data types and implementing standard operations would be incomplete without this feature.
To understand and remember the syntax of the overloading operator definition, you can start creating a member function to support the operation. In our example we use the following syntax for the Add function declaration, definition, and call:
CComplex Add(CComplex);
CComplex CComplex::Add(CComplex Complex1)
TestComplex3 = TestComplex1.Add(TestComplex2);
To change the Add function to the + operator, we have to just replace Add with operator+ in the function declaration and definition:
CComplex operator+(CComplex);
CComplex CComplex::operator+(CComplex Complex1)
Function calls will be naturally replaced with
TestComplex3 = TestComplex1 + TestComplex2;
Similarly you can overload unary operators such as ++ or --. The only problem is in distinguishing between prefix and postfix notation. C++ convention defines the difference in operator declarations. For prefix notation, you can use an approach similar to binary operator overloading. For example, the prefix ++ operator can be declared as
CComplex operator++();
For postfix notation you should use the int keyword (surprise!) in the operator declaration. For example, postfix -- operator can be declared as
CComplex operator--(int);
|