Virtual function polymorphism dilemma.

This is a discussion on Virtual function polymorphism dilemma. within the C++ Programming forums, part of the General Programming Boards category; Does anyone know if virtual functions implicitly call the base class's implementation of said function at all? For my homework, ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    67

    Virtual function polymorphism dilemma.

    Does anyone know if virtual functions implicitly call the base class's implementation of said function at all?

    For my homework, I have a base class called Package and a child called TwoDayPackage. Package defines a virtual function called calculateCost() which returns a double value in the expression of weight (in ounces) times the cost per ounce. The child class implementation is supposed to add a flat fee on to that. However, as I'm dealing with virtual polymorphism, I'm wondering if there's an alternative to this:
    Code:
    virtual double calculateCost()
    {
    return (Package::calculateCost()+flatFee);
    }
    The above is what TwoDayPackage defines. I don't know if this seems the right way to do this though. My assignment is to modify my previous homework assignment by virtual-izing the functions, but despite being virtual, TwoDayPackage seems to disregard Package's implementation if I take out the call to the base function call and just give me the flat fee only, as I've seen in the test program I made to debug it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    So, by your observation, virtual functions do not "implicitly call the base class's implementation of said function at all".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    Well, I want it to add the derived class variable "flatFee" to the return value of the base class's implementation, but somehow I feel explicitly calling Package::calculateCost() in the child class calculateCost() seems redundant with virtual functions. The thing is, I don't know any other way to modify it to add on to the base function return value. It just seems an awful lot like redefining the function as opposed to overriding it.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by Chris87
    Well, I want it to add the derived class variable "flatFee" to the return value of the base class's implementation, but somehow I feel explicitly calling Package::calculateCost() in the child class calculateCost() seems redundant with virtual functions. The thing is, I don't know any other way to modify it to add on to the base function return value.
    In this case calling the base class function is the correct thing to do. If the base class had been designed a little differently by providing a virtual function that did nothing but was called from Package::calculateCost, then you could have written an override for that function instead of return the flat fee and do nothing else.

    Quote Originally Posted by Chris87
    It just seems an awful lot like redefining the function as opposed to overriding it.
    It is an override since it will be called when calculateCost is called on an object of a derived class, through a Package pointer or reference.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    Awesome, thanks! I just didn't want the code to look rather... stupid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pure Virtual Polymorphism HELP
    By Syndacate in forum C++ Programming
    Replies: 17
    Last Post: 01-14-2011, 10:21 AM
  2. mixed template and virtual polymorphism
    By m37h0d in forum C++ Programming
    Replies: 0
    Last Post: 06-09-2009, 07:15 AM
  3. inheritance, polymorphism, and virtual functions
    By cs_student in forum C++ Programming
    Replies: 8
    Last Post: 08-04-2008, 08:47 AM
  4. virtual functions in polymorphism
    By omeydhomey in forum C++ Programming
    Replies: 3
    Last Post: 03-15-2003, 03:51 PM
  5. Polymorphism/Inheritance/virtual function problem?
    By JasonLikesJava in forum C++ Programming
    Replies: 16
    Last Post: 10-22-2002, 11:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21