Thread: Virtual function polymorphism dilemma.

  1. #1
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    139

    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
    28,412
    So, by your observation, virtual functions do not "implicitly call the base class's implementation of said function at all".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    139
    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
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User Chris87's Avatar
    Join Date
    Dec 2007
    Posts
    139
    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, 11: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, 04:51 PM
  5. Polymorphism/Inheritance/virtual function problem?
    By JasonLikesJava in forum C++ Programming
    Replies: 16
    Last Post: 10-22-2002, 11:53 AM