This sample demonstrates various features of records.
You can find the ComplexNumbers sample project at:
- Start > Programs > Appmethod > Samples > Object Pascal > RTL > ComplexNumbers
- Subversion Repository for Object Pascal: http://sourceforge.net/p/radstudiodemos/code/HEAD/tree/branches/RadStudio_XE8//Object%20Pascal/RTL/ComplexNumbers/
This application provides an implementation for a record that handles complex numbers. It exemplifies operator overloading, visibility specifiers, instance methods, class methods, and class variables.
How to Use the Sample
- Navigate to Start | Programs | Appmethod 1.16 | Samples and open Win32OperatorOverload.dproj.
- Press F9 or choose Run > Run.
The project has two source files:
The implementation for TComplex, a record that handles complex numbers.
A program that uses TComplex instances, operators, and methods.
TComplex implements a record that manages complex numbers. It provides fields, operators, instance methods, class methods, and class variables for operations with complex numbers.
- A constructor is simulated by using a private class procedure and the initialization section of the unit.
- The TComplex class overloads some of the Object Pascal operators: Add, Substract, Multiply, Divide, Negative, Equal, NotEqual, and so on.
- TComplex contains class variables, like Symbol and SymbolBeforeImaginary, that belong to the entire class and not to individual instances.
- TComplex implements both class methods (for example
class function From(const AReal, AImaginary: Double): TComplex; static;) and instance methods, for example
function ToString: System.String;.
- Members of TComplex have different visibility specifiers: strict private, private, and public.