Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Inheritance and virtual functions

    Hi everyone,

    my question is in the use of virtual functions and they are actually two questions:

    1.- Do we have to re-declare the function in the subclass that is actually going to implement the function?
    2.- Whether we do or not, does the function in the subclass have a particular level of exposure: public or private?

    The way I understand virtual functions is that they are useful to declare a functionality that subclasses should have, and the type of object they should return. For example if you’re programming maximum likelihood estimators, you could have the super class MLEstimator where you define all the functions that a particular estimator must implement, like MLEval, for the evaluator, Estimate, Display, etc... However, if they force a particular exposure level to the function when implemented it can be troubling. For example, I may want Estimate to be public, and to call Display from inside Estimate and, thus, to be private.

    Thanks!!!
    Alfonso Sanchez-Penalver

  • #2
    Do the following help?.

    Originally posted by Alfonso Sánchez-Peñalver View Post
    1.- Do we have to re-declare the function in the subclass that is actually going to implement the function?
    .ÿ
    .ÿversionÿ16.1

    .ÿ
    .ÿclearÿ*

    .ÿ
    .ÿsetÿlinesizeÿ79

    .ÿ
    .ÿmata:
    -------------------------------------------------ÿmataÿ(typeÿendÿtoÿexit)ÿ-----
    :ÿmataÿsetÿmatastrictÿon

    :ÿ
    :ÿclassÿAÿ{
    >ÿÿÿÿÿpublic:
    >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿvirtualÿvoidÿprobe()
    >ÿ}

    :ÿ
    :ÿclassÿBÿextendsÿAÿ{
    >ÿÿÿÿÿpublic:
    >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿfinalÿvoidÿprobe()
    >ÿ}

    :ÿvoidÿfunctionÿB::probe()ÿprintf("BÿHere!\n")

    :ÿ
    :ÿaBÿ=ÿB()

    :ÿaB.probe()
    BÿHere!

    :ÿ
    :ÿclassÿDÿextendsÿAÿ{
    >ÿÿÿÿÿ/*ÿpublic:
    >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿfinalÿvoidÿprobe()ÿ*/
    >ÿ}

    :ÿvoidÿfunctionÿB::probe()ÿprintf("DÿHere!\n")

    :ÿ
    :ÿaDÿ=ÿD()

    :ÿaD.probe()
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ<istmt>:ÿÿ3499ÿÿvirtualÿD::probe()ÿnotÿfound
    (1ÿlineÿskipped)
    -------------------------------------------------------------------------------
    r(3499);

    endÿofÿdo-file

    r(3499);

    .


    2.- Whether we do or not, does the function in the subclass have a particular level of exposure: public or private? . . . For example, I may want Estimate to be public, and to call Display from inside Estimate and, thus, to be private.
    .ÿ
    .ÿversionÿ16.1

    .ÿ
    .ÿclearÿ*

    .ÿ
    .ÿlocalÿline_sizeÿ`c(linesize)'

    .ÿsetÿlinesizeÿ79

    .ÿ
    .ÿmata:
    -------------------------------------------------ÿmataÿ(typeÿendÿtoÿexit)ÿ-----
    :ÿmataÿsetÿmatastrictÿon

    :ÿ
    :ÿclassÿAÿ{
    >ÿÿÿÿÿpublic:
    >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿvirtualÿvoidÿestimate()
    >ÿÿÿÿÿÿÿÿÿprivate:
    >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿvirtualÿvoidÿdisplay()
    >ÿ}

    :ÿ
    :ÿclassÿBÿextendsÿAÿ{
    >ÿÿÿÿÿpublic:
    >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿfinalÿvoidÿestimate()
    >ÿÿÿÿÿÿÿÿÿprivate:
    >ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿfinalÿvoidÿdisplay()
    >ÿ}

    :ÿvoidÿfunctionÿB::estimate()ÿdisplay()

    :ÿvoidÿfunctionÿB::display()ÿprintf("BÿHere!\n")

    :ÿ
    :ÿaBÿ=ÿB()

    :ÿaB.estimate()
    BÿHere!

    :ÿ
    :ÿend
    -------------------------------------------------------------------------------

    .ÿ
    .ÿsetÿlinesizeÿ`line_size'

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .

    Comment


    • #3


      Thanks Joseph Coveney for your illustrative examples. This opens another question. Does declaring a function as virtual have any other functionality than simply declaring that it has to be implemented in a subclass? I know that you can implement it in the class that you declare it as virtual, but that if declared in a subclass the function in the subclass takes precedence. So is that the only other functionality?

      I ask because notice that in your example of classes A and B, nothing would change if you didn't declare the functions in class A.
      Alfonso Sanchez-Penalver

      Comment

      Working...
      X