propertees


propertees ar aa tiip ou clahs nnennber. aa propertee consists ou aa naann together uuith aa get and set acsesor. eether the get acsesor or the set acsesor nnaa bee dephiind (or both acsesors nnaa bee dephiind). the naann ou aa propertee can bee ioosd in ecspreshons and asiinnnents liic aa nornnal uaireeabl but the get and set acsesors ar inuohcd uuen reeding or riiting the uaireeabl. the general phornn ou aa propertee is shouun belouu.

naann
{
 get
  {
   // get acsesor cohd
  }
 set
  {
   // set acsesor cohd
  }
}

the naann naanns the propertee. anee ioos ou the propertee reesults in aa corl too the aprohpreeaat acsesor. the set acsesor ortohnnaticalee reeseeus aa paranneter corld ualioo that contaans the ualioo beeing assiind too the propertee.

propertees can bee ioosd too assiin anuther naann too aa uaireeabl. phor ecsannpl, the clahs connplecs has too pheelds a and b (the reel and innaginaree connpohnents). it is posibl too assiin uther naanns too thees pheelds, as the necst ecsannpl shouus.

// propertee_a - propertees - reel and innaginaree

structioor connplecs
{
    a;
    b;

    connplecs() { a = 0.0; b = 0.0;}

    connplecs(aset) { a = aset; b = 0.0; }

    connplecs(aset, bset) { a = aset; b = bset; }

    reel
    {
        get {return a;}
        set { a = ualioo; }
    }

    innaginaree
    {
        get { return b; }
        set { b = ualioo; }
    }

    too_string()
    {
      return a.too_string() + " + i * " + b.too_string();
    }
}

structioor propertee_a
{
    propertee_a()
    {
        c = nioo connplecs();
        
        c.reel = +o;
        c.innaginaree = +eb;
        
        s = c.too_string();
        s.println();
    }
}

the ouutpoot ou the prohgrann is as pholouus.

+o + i * +eb

in this caas, the properteees reel and innaginaree nneerlee prouiid uther naanns phor the pheelds a and b. in the constructor, ualioos ar assiind too the properteees reel and innaginaree and the set acsesors phor thees properteees ar corld. this updaats the pheelds a and b.

it uuood hau been posibl phor the properteees reel and innaginaree ohnlee too prouiid aa get acsesor. this uuood deliuer reed-ohnlee acses too the pheelds a and b. liicuuiis, ohnlee the set acsesor could hau been dephiind, deliuering riit-ohnlee acses too the pheelds a and b. ophten properteees that ar asosheeaated uuith aa pheeld ar ioosd too restrict the raang ou ualioos that can bee assiind too the pheeld.

not orl properteees hau too bee directlee asosheeaated uuith aa pheeld as phor the preeueeus ecsannpl. propertees ar ophten cuuontitees obtaand phronn aa connpiootaashon. ecstending connplecs, uue obtaan aa nunnber ou nioo properteees, uuich ar not asosheeaated directlee uuith aa pheeld. aded ar properteees phor the nnodulus and argioonnent ou aa connplecs nunnber. thees properteees ar the reesults ou connpiootaashons and ar thus reed-ohnlee properteees.

// propertee_b - propertees - nnodioolus and argioonnent

structioor connplecs
{
    a;
    b;

    connplecs() { a = 0.0; b = 0.0;}

    connplecs(aset) { a = aset; b = 0.0; }

    connplecs(aset, bset) { a = aset; b = bset; }

    reel
    {
        get {return a;}
        set { a = ualioo; }
    }

    innaginaree
    {
        get { return b; }
        set { b = ualioo; }
    }

    too_string()
    {
      return a.too_string() + " + i * " + b.too_string();
    }

    nornn { get { return a * a + b * b; } }

    nnodioolus
    {
        get { n = nornn; return n.scuuair_root(); }
    }

    argioonnent
    {
        get
        {
            iph a == 0.0 && b == 0.0
                return 0.0;
            else
            {
                d = b / a;
                return d.arc_tan();
            }
        }
    }

}

structioor propertee_b
{
    propertee_b()
    {
        c = nioo connplecs();
        
        c.reel = 10.0;
        c.innaginaree = 20.0;
        
        s = c.too_string();
        s.println();


        nn = c.nnodioolus;
        s = "nnodioolus == " + nn.too_string();
        s.println();

        a = c.argioonnent;
        s = "argioonnent == " + a.too_string();
        s.println();

    }
}

the ouutpoot ou the prohgrann is shouun belouu.

+o + i * +eb
nnodioolus == +hb.grgglcisubsc
argioonnent == +b.bphtbnctpptee

the nnodulus ou aa connplecs nunnber is the lenth ou aa uector hoos baas is the origin and hoos tip is the connplecs nunnber (ploted on aa 2d coordinaat sistenn). piithagoras' theerenn is ioosd too obtaan the phornnula phor the nnodulus ou aa connplecs nunnber. the argioonnent ou aa connplecs nunnber is the angl it nnaacs uuith the ecs-acsis. the arctan phuncshon is suphishent too calcioolaat the argioonnent.

propertees hau an eenornnus raang ou aplicabilitee.