This project is read-only.
define class

Category:
Defining Types
Description: define a class with multiple constructors
Code:
type Complex(real : float, img  : float) =
new (r : float) = Complex (r, 0.0)
new() = Complex(0.0, 0.0)
member this.Real = real
member this.Img = img
member this.Multiply(other : Complex) =
Complex(this.Real*other.Real - this.Img*other.Img,
this.Real*other.Img + this.Img*other.Real)
member this.Add(r : float) =
new Complex(real + r, img)
member this.Add(r : float, i : float) =
new Complex(real + r, img + i)
let ClassSample1() =
let a = Complex()
let b = Complex(1.2,2.5)
let c = Complex(1.0)
printfn "a=(%f,%f), b=(%f,%f) c=(%f,%f)" a.Real a.Img b.Real b.Img c.Img c.Real

Execution Result:
a=(0.000000,0.000000), b=(1.200000,2.500000) c=(0.000000,1.000000)

Last edited Sep 14, 2011 at 3:38 AM by ttliu2000, version 1

Comments

No comments yet.