Function Overloading does not work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anil Gupte

    Function Overloading does not work?

    I get two errors
    void DisplayProjecte dValue(double amount, int years)
    {
    Random __gc * r = new Random();
    int randomRate = r->Next(0, 20);
    DisplayProjecte dValue(amount, years, randomRate);
    }

    void DisplayProjecte dValue(double amount, int years, double rate)
    {
    double rateFraction = 1 + (rate/100);
    double finalAmount = amount * Math::Pow(rateF raction, years);
    finalAmount = Math::Round(fin alAmount, 2);
    Console::WriteL ine(S"--------------------------------------------");
    Console::WriteL ine(S"Investmen t Amount: ");
    Console::Write( amount);
    Console::WriteL ine(S"Growth Rate [%]: ");
    Console::Write( rate);
    Console::WriteL ine(S"Period [years]: ");
    Console::Write( years);
    Console::WriteL ine (S"Projected Final value of Investment: ");
    Console::Write( finalAmount);
    Console::WriteL ine(S"--------------------------------------------");
    }

    I tried changing the last line of the first function to:
    DisplayProjecte dValue(amount, years, randomRate->ToDouble(0)) ;
    but that also gave me errors.

    This is from a book Visual C++ .Net by Templeman and Olsen (Microsoft
    Press).

    --
    Anil Gupte

    iCinema.com is a 7-character domain name that evokes a sense of entertainment, imagination, and fun. It combines the concepts of “i” (for information and interaction) with “cinema” to create an idea of an interactive cinema experien



  • Anil Gupte

    #2
    Re: Function Overloading does not work?

    I should add the error I am getting is: error C2660: 'DisplayProject edValue'
    : function does not take 3 arguments

    TIA,
    --
    Anil Gupte

    iCinema.com is a 7-character domain name that evokes a sense of entertainment, imagination, and fun. It combines the concepts of “i” (for information and interaction) with “cinema” to create an idea of an interactive cinema experien


    "Anil Gupte" <anil-list@icinema.co mwrote in message
    news:OdEFhB1LIH A.5244@TK2MSFTN GP03.phx.gbl...
    >I get two errors
    void DisplayProjecte dValue(double amount, int years)
    {
    Random __gc * r = new Random();
    int randomRate = r->Next(0, 20);
    DisplayProjecte dValue(amount, years, randomRate);
    }
    >
    void DisplayProjecte dValue(double amount, int years, double rate)
    {
    double rateFraction = 1 + (rate/100);
    double finalAmount = amount * Math::Pow(rateF raction, years);
    finalAmount = Math::Round(fin alAmount, 2);
    Console::WriteL ine(S"--------------------------------------------");
    Console::WriteL ine(S"Investmen t Amount: ");
    Console::Write( amount);
    Console::WriteL ine(S"Growth Rate [%]: ");
    Console::Write( rate);
    Console::WriteL ine(S"Period [years]: ");
    Console::Write( years);
    Console::WriteL ine (S"Projected Final value of Investment: ");
    Console::Write( finalAmount);
    Console::WriteL ine(S"--------------------------------------------");
    }
    >
    I tried changing the last line of the first function to:
    DisplayProjecte dValue(amount, years, randomRate->ToDouble(0)) ;
    but that also gave me errors.
    >
    This is from a book Visual C++ .Net by Templeman and Olsen (Microsoft
    Press).
    >
    --
    Anil Gupte

    iCinema.com is a 7-character domain name that evokes a sense of entertainment, imagination, and fun. It combines the concepts of “i” (for information and interaction) with “cinema” to create an idea of an interactive cinema experien

    >

    Comment

    • David Wilkinson

      #3
      Re: Function Overloading does not work?

      Anil Gupte wrote:
      I get two errors
      void DisplayProjecte dValue(double amount, int years)
      {
      Random __gc * r = new Random();
      int randomRate = r->Next(0, 20);
      DisplayProjecte dValue(amount, years, randomRate);
      }
      >
      void DisplayProjecte dValue(double amount, int years, double rate)
      {
      double rateFraction = 1 + (rate/100);
      double finalAmount = amount * Math::Pow(rateF raction, years);
      finalAmount = Math::Round(fin alAmount, 2);
      Console::WriteL ine(S"--------------------------------------------");
      Console::WriteL ine(S"Investmen t Amount: ");
      Console::Write( amount);
      Console::WriteL ine(S"Growth Rate [%]: ");
      Console::Write( rate);
      Console::WriteL ine(S"Period [years]: ");
      Console::Write( years);
      Console::WriteL ine (S"Projected Final value of Investment: ");
      Console::Write( finalAmount);
      Console::WriteL ine(S"--------------------------------------------");
      }
      Anil:

      In C++, a definition or declaraion of a function must appear before it
      can be used.

      --
      David Wilkinson
      Visual C++ MVP

      Comment

      • Anil Gupte

        #4
        Re: Function Overloading does not work?

        That worked, but that is vey weird - seems like the good old linear Fortran
        Programming days! Why should the position in a file have anything to do
        with a function being recognized?

        Also, why does this line not generate at least a warning if not an error?

        DisplayProjecte dValue(amount, years, randomRate);

        when randomRate is an int and the function is expecting a double?

        Thanx,

        --
        Anil Gupte

        iCinema.com is a 7-character domain name that evokes a sense of entertainment, imagination, and fun. It combines the concepts of “i” (for information and interaction) with “cinema” to create an idea of an interactive cinema experien


        "David Wilkinson" <no-reply@effisols. comwrote in message
        news:unmF832LIH A.5716@TK2MSFTN GP04.phx.gbl...
        Anil Gupte wrote:
        >I get two errors
        >void DisplayProjecte dValue(double amount, int years)
        >{
        >Random __gc * r = new Random();
        >int randomRate = r->Next(0, 20);
        >DisplayProject edValue(amount, years, randomRate);
        >}
        >>
        >void DisplayProjecte dValue(double amount, int years, double rate)
        >{
        >double rateFraction = 1 + (rate/100);
        >double finalAmount = amount * Math::Pow(rateF raction, years);
        >finalAmount = Math::Round(fin alAmount, 2);
        >Console::Write Line(S"--------------------------------------------");
        >Console::Write Line(S"Investme nt Amount: ");
        >Console::Write (amount);
        >Console::Write Line(S"Growth Rate [%]: ");
        >Console::Write (rate);
        >Console::Write Line(S"Period [years]: ");
        >Console::Write (years);
        >Console::Write Line (S"Projected Final value of Investment: ");
        >Console::Write (finalAmount);
        >Console::Write Line(S"--------------------------------------------");
        >}
        >
        Anil:
        >
        In C++, a definition or declaraion of a function must appear before it can
        be used.
        >
        --
        David Wilkinson
        Visual C++ MVP

        Comment

        • Carl Daniel [VC++ MVP]

          #5
          Re: Function Overloading does not work?

          Anil Gupte wrote:
          That worked, but that is vey weird - seems like the good old linear
          Fortran Programming days! Why should the position in a file have
          anything to do with a function being recognized?
          Because C, and subsequently C++ were defined to make it possible for the
          compiler to pass over the source code 1 time.
          >
          Also, why does this line not generate at least a warning if not an
          error?
          It does generate an error - you already posted it.
          >
          DisplayProjecte dValue(amount, years, randomRate);
          >
          when randomRate is an int and the function is expecting a double?
          You don't get an error for passing an int where a double is expected because
          the conversion from int to double is implicit and lossless.

          -cd


          Comment

          • Carl Daniel [VC++ MVP]

            #6
            Re: Function Overloading does not work?

            Anil Gupte wrote:
            I get two errors
            void DisplayProjecte dValue(double amount, int years)
            {
            Random __gc * r = new Random();
            Side note - this is based on the old "Managed Extensions for C++" introduced
            with VC7, which are now deprecated. Get a newer book that covers "C++/CLI"
            introduced with VC8, which is the going-forward solution for managed
            programming in C++.

            -cd



            Comment

            Working...