New in C++:Abstract Data Types(ADT) - Base Class

Discussion in 'C++' started by SunScreen, Nov 18, 2004.

  1. SunScreen

    SunScreen Guest

    Hi all,

    I would like to help with the following:

    // my conventions:
    // BC = Base Class
    // DC = Derived Class

    class BC // this ADT base class or interface
    {
    BC(){}
    ~BC(){}
    bcFunc1() = 0;
    bcFunc2() = 0;
    }

    class DC : public BC
    {
    DC(){}
    ~DC(){}
    bcFunc1() { // Do something }
    bcFunc2() { // Do something }

    dcFunc() { // Do something }
    }

    int main()
    {
    // I declare a pointer to BC assigned to DC object
    BC* myBC = new DC;

    myBC->bcFunc1();
    myBC->bcFunc2();

    // now the tricky part - the following line will not compile
    // myBC->dcFunc();

    // one way to handle this is using the dynamic_cast operator
    DC* myDC = dynamic_cast<DC*> (myBC);

    // now I can call the DC function
    myDC->dcFunc();

    delete myBC;
    delete myDC;
    }

    I know that using the dynamic_cast operator is bad programming code.
    Is there any other way call the DC function?


    Thanx in Advance,
    Sun
     
    SunScreen, Nov 18, 2004
    #1
    1. Advertising

  2. "SunScreen" <> wrote in message
    news:...
    > Hi all,
    >
    > I would like to help with the following:
    >
    > // my conventions:
    > // BC = Base Class
    > // DC = Derived Class
    >
    > class BC // this ADT base class or interface
    > {
    > BC(){}
    > ~BC(){}
    > bcFunc1() = 0;
    > bcFunc2() = 0;
    > }
    >
    > class DC : public BC
    > {
    > DC(){}
    > ~DC(){}
    > bcFunc1() { // Do something }
    > bcFunc2() { // Do something }
    >
    > dcFunc() { // Do something }
    > }
    >
    > int main()
    > {
    > // I declare a pointer to BC assigned to DC object
    > BC* myBC = new DC;
    >
    > myBC->bcFunc1();
    > myBC->bcFunc2();
    >
    > // now the tricky part - the following line will not compile
    > // myBC->dcFunc();
    >
    > // one way to handle this is using the dynamic_cast operator
    > DC* myDC = dynamic_cast<DC*> (myBC);
    >
    > // now I can call the DC function
    > myDC->dcFunc();
    >
    > delete myBC;
    > delete myDC;
    > }
    >
    > I know that using the dynamic_cast operator is bad programming code.
    > Is there any other way call the DC function?


    Well you could use static_cast, but that is equally 'bad programming code'.
    These things are necessary sometimes but the reason they are considered bad
    practice is because the need to call a dervied class member function though
    a base class pointer when the member function is not present and virtual in
    the base class, is normally considered bad design.

    What need do you have to do this? I'm not saying that you shouldn't, I'm
    just saying that you should think carefully about it. It may be that you can
    avoid this by redesigning your code.

    john
     
    John Harrison, Nov 18, 2004
    #2
    1. Advertising

  3. SunScreen

    Howard Guest

    "SunScreen" <> wrote in message
    news:...
    > Hi all,
    >
    > I would like to help with the following:
    >
    > // my conventions:
    > // BC = Base Class
    > // DC = Derived Class
    >
    > class BC // this ADT base class or interface
    > {
    > BC(){}
    > ~BC(){}
    > bcFunc1() = 0;
    > bcFunc2() = 0;
    > }
    >
    > class DC : public BC
    > {
    > DC(){}
    > ~DC(){}
    > bcFunc1() { // Do something }
    > bcFunc2() { // Do something }
    >
    > dcFunc() { // Do something }
    > }
    >
    > int main()
    > {
    > // I declare a pointer to BC assigned to DC object
    > BC* myBC = new DC;
    >
    > myBC->bcFunc1();
    > myBC->bcFunc2();
    >
    > // now the tricky part - the following line will not compile
    > // myBC->dcFunc();
    >
    > // one way to handle this is using the dynamic_cast operator
    > DC* myDC = dynamic_cast<DC*> (myBC);
    >
    > // now I can call the DC function
    > myDC->dcFunc();
    >
    > delete myBC;
    > delete myDC;
    > }
    >
    > I know that using the dynamic_cast operator is bad programming code.
    > Is there any other way call the DC function?
    >
    >
    > Thanx in Advance,
    > Sun


    Yes, a couple of ways. For one, you could make a virtual function dcFunc()
    in the base class that does nothing (or is abstract virtual). Or, you could
    make a new virtual function in both classes, then have it do nothing (or be
    abstract virtual) in the base class but have it call dcFunc() in the DC
    class. Either way, you use a virtual function in the base class in order to
    call its overridden function in the derived class.

    -Howard
     
    Howard, Nov 18, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Kaeppler
    Replies:
    1
    Views:
    443
    R.F. Pels
    May 22, 2005
  2. Sameer
    Replies:
    4
    Views:
    605
    Roedy Green
    Aug 31, 2005
  3. Uzytkownik
    Replies:
    3
    Views:
    596
    Uzytkownik
    Apr 3, 2005
  4. deanfamily11
    Replies:
    9
    Views:
    392
    Alf P. Steinbach
    Oct 11, 2005
  5. Replies:
    4
    Views:
    824
    Rolf Magnus
    May 17, 2006
Loading...

Share This Page