abstract class used as a parameter in a function.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DavidFarrow
    New Member
    • Sep 2010
    • 3

    abstract class used as a parameter in a function.

    The program calls for an abstract base class (used in two additional functions). No problems here.
    The final part is to create a function that accepts the base class as a parameter: static void getStats() that accepts a parameter of type Account (see below). It is to display the data from Inquiry.

    Code:
    abstract class Account
    {
     ...
     public virtual double Inquiry()
      {
       ...
      }
    }
    class checkingAccount : Account
    {
     ...
    }
    class savingsAccount : Account
    {
     ...
    }
    I can't seem to find any documentation on how to pass an abstract type as a parameter. And then I have to figure out which account I'm passing to get the right inquiry results.

    Point me in the right direction, either a quick piece of code or a good literature source.
    Thanks
    D
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    This is what you are looking for.

    static void getStats(Accoun t account)
    {
    }

    The good thing is that you can pass either a checking account or savings account into the getStats method.

    Comment

    • DavidFarrow
      New Member
      • Sep 2010
      • 3

      #3
      Thanks for the help.
      I was really overthinking the problem.

      Comment

      • hype261
        New Member
        • Apr 2010
        • 207

        #4
        David,

        I missed this statement in your original post and I felt it needed correcting.

        Quote...
        And then I have to figure out which account I'm passing to get the right inquiry results.

        If you have to figure out which type of account you are passing to a method it generally means that something is messed up in your design.

        The benefit of virtual methods is that you don't have to figure out which account type is being used. If both the savings and checking account override the Inquiry method then when you pass either to the getStatus method the appropriate Inquiry method will be called. The is one of the main advantages of an OO language.

        Comment

        • DavidFarrow
          New Member
          • Sep 2010
          • 3

          #5
          As I said before, I was really overthinking the problem. Once I saw your code snippet it was like having a bucket of cold water thrown on my face. Wake-up time.
          I have the entire program running and am now in the process of trying to find anything that will break it.

          Comment

          Working...