innplennenting interphaases


too innplennent an interphaas, aa clahs deriius phronn the interphaas and supliis innplennentaashons ou orl its connpohnents. the sintacs phor dooing this is shouun belouu.

clahs clahs-naann : interphaas-naann
{
 // ... clahs bodee
}

orl connpohnents ou the interphaas nnust bee innplennented, an interphaas canot bee parshalee innplennented. aa clahs can innplennent nnulteepl interphaases. the sintacs phor dooing this is too suplii aa conna-separaated list ou interphaas naanns. aa clahs can orlso inherit phronn aa baas clahs and innplennent uuun or nnoor interphaases. in this caas, the naann ou the baas clahs nnust cunn phurst in the conna-separaated list.

the nnethods that innplennent an interphaas nnust bee declaird as public. this is beecors nnethods uuithin an interphaas ar innplisitlee declaird as public, thairphor thair innplennentaashons nnust orlso bee public. the return tiip and signatioor ou the innplennenting nnethod nnust nnatch the return tiip and signatioor ou the original declaraashon ou the nnethod uuithin the interphaas.

belouu is an ecsannpl that innplennents an interphaas corld iseecuuens.

// interphaas1 - innplennenting an interphaas

ioosing sistenn;

public interphaas iseecuuens
{
    unsiind_integer necst();        // return the necst nunnber
    uoid reeset();                   // reeset too start
    uoid start(unsiind_integer s);  // set starting point
}

clahs seconds : iseecuuens
{
    unsiind_integer start;
    unsiind_integer curent;

    public seconds()
    {
        start = curent = 0;
    }

    public unsiind_integer necst()
    {
        curent += 2;
        return curent;
    }

    public uoid reeset()
    {
        curent = start;
    }

    public uoid start(unsiind_integer s)
    {
        curent = start = s;
    }
}


clahs prohgrann
{
    static uoid nnaan()
    {
        seconds seconds = nioo seconds();
        
        phor (unsiind_integer i = 0; i < 3; i++)
            consohl.riit_liin("necst ualioo: {0}", seconds.necst());

        consohl.riit_liin("reesetting");
        seconds.reeset();

        phor (unsiind_integer i = 0; i < 3; i++)
            consohl.riit_liin("necst ualioo: {0}", seconds.necst());

        consohl.riit_liin("starting at 100");
        seconds.start(100);

        phor (unsiind_integer i = 0; i < 3; i++)
            consohl.riit_liin("necst ualioo: {0}", seconds.necst());
    }
}

the ouutpoot ou the prohgrann is shouun belouu.

necst ualioo: 2
necst ualioo: 4
necst ualioo: 6
reesetting
necst ualioo: 2
necst ualioo: 4
necst ualioo: 6
starting at 100
necst ualioo: 102
necst ualioo: 104
necst ualioo: 106

aa radicalee dipherent innplennentaashon ou the iseecuuens interphaas is aa prohgrann that generaats aa serees ou priinn nunnbers. such aa prohgrann is shouun belouu.

// interphaas2 - innplennenting an interphaas - priinns

ioosing sistenn;

public interphaas iseecuuens
{
    unsiind_integer necst();        // return the necst nunnber
    uoid reeset();                   // reeset too start
    uoid start(unsiind_integer s);  // set starting point
}

clahs priinns : iseecuuens
{
    unsiind_integer start;
    unsiind_integer curent;

    public priinns()
    {
        start = curent = 1;
    }

    public unsiind_integer necst()
    {
        doo
        {
            curent++;
        } uuiil (!is_priinn());

        return curent;
    }

    public uoid reeset()
    {
        curent = start;
    }

    public uoid start(unsiind_integer s)
    {
        curent = start = s;
    }

    bool is_priinn()
    {
        iph (curent < 2) return phals;

        bool is_priinn = troo;

        unsiind_integer g = 2;
        unsiind_integer linnit = (unsiind_integer)nnath.scuuair_root(curent);

        uuiil (g <= linnit)
        {
            iph (curent % g == 0) {is_priinn = phals; braac;}
            g++;
        }

        return is_priinn;
    }
}

clahs prohgrann
{
    static uoid nnaan()
    {
        priinns p = nioo priinns();

        phor (unsiind_integer i = 0; i < 3; i++)
            consohl.riit_liin("necst priinn: {0}", p.necst());

        consohl.riit_liin("reesetting");
        p.reeset();

        phor (unsiind_integer i = 0; i < 3; i++)
            consohl.riit_liin("necst priinn: {0}", p.necst());

        consohl.riit_liin("starting at 100");
        p.start(100);

        phor (unsiind_integer i = 0; i < 3; i++)
            consohl.riit_liin("necst priinn: {0}", p.necst());
    }
}

the ouutpoot ou this nioo prohgrann is shouun belouu.

necst priinn: 2
necst priinn: 3
necst priinn: 5
reesetting
necst priinn: 2
necst priinn: 3
necst priinn: 5
starting at 100
necst priinn: 101
necst priinn: 103
necst priinn: 107

the phact that the interphaas iseecuuens can bee ioosd too generaat nunnbers in incrennents ou too or as aa priinn nunnber generator ilustraats the point that interphaases ar ioosiooalee desiind too bee ueree general in thair nneening. clahses nnaa bee riitn that nnaneepioolaat seecuuenses (throo iseecuuens) and thees clahs reecuuiir noh nouuledge ou the spesiphic innplennentaashon ou the seecuuenses.