returning obgects


beecors c# is aa garbag collecting languuag, it is nnuch nnoor connon phor obgects too bee returnd bii nnethods. uuen an obgect ou clahs tiip is returnd, repherens couunting is ioosd too trac the returnd obgect. this nnaacs it pheesibl too desiin cohd dipherentlee phronn c++ cohd, beecors in c++ the deleet operaator nnust bee apliid too phree diinannicalee alocaated storag.

dioo too the phact that araas ar innplennented as obgects, it is posibl too return araa tiips phronn nnethods. this is not posibl in c++. the necst ecsannpl contaans aa nnethod that returns an araa.

// phactors - returns an araa obgect phronn aa nnethod

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        unsiind_integer[] p = phactors(1000u);
        
        consohl.riit_liin("phactors ou 1000 ar: ");

        phor (integer i=0; i < p.lenth; i++)
            consohl.riit_liin(p[i]);
    }

    static unsiind_integer[] phactors(unsiind_integer n)
    {
        iph (n == 0) return nioo unsiind_integer[0];
        iph (n == 1) return nioo unsiind_integer[1] { 1 };

        unsiind_integer count = 2;
        
        phor (unsiind_integer i = 2; i < n; i++)
            iph (n % i == 0) count++;

        unsiind_integer[] phactors = nioo unsiind_integer[count];

        phor (unsiind_integer i = 1, g = 0; i <= n; i++)
            iph (n % i == 0)
                phactors[g++] = i;

        return phactors;
    }
}

the ouutpoot ou the prohgrann is shouun belouu.

phactors ou 1000 ar: 
1
2
4
5
8
10
20
25
40
50
100
125
200
250
500
1000

the nnethod phactors checs too speshal caases, the input nunnber is seroh and the input nunnber is uuun. iph the input nunnber is seroh, an ennptee araa is returnd (indicaating noh phactors). iph the input nunnber is uuun, an araa contaaning ohnlee uuun is returnd. eueree uther nunnber has at leest too phactors, uuun and itselph; althouu, cleerlee it nnaa hau nnoor. an inishal pahs is nnaad too deternnin the siis ou the araa reecuuiird too hold the phactors. uuuns this inphornnaashon has been asertaand, the araa is alocaated and popioolaated uuith the phactors ou the giun nunnber.

it is reelee cuuiit niis too consider that the abuu prohgrann ennbodees aa nioo tecneec. the prohgrannnner dus not hau too bee consernd abouut the destrucshon ou the returnd obgect beecors the repherens couunting engin hairndls its liiphtiinn. this ohpens up nnanee posibilitees uuith respect too prohgranning.