View Single Post
  #1  
Old 02-25-2004, 09:25 AM
samandhi's Avatar
samandhi
Demi-God
 
Join Date: Aug 2003
Posts: 1,056
Default A question about C++ programming.

I am trying to learn C++ (from a book, since I dont have the money to take any REAL classes hehe), and have a question that is NOT answered in the book. Probably not a big deal, but I want to know the "why" before Ill totally understand the "how"..
Say I have some code like this:
Code:
 #include <iostream.h>
int Double (int);
int Double (long);
int Double (float);
int Double (double);

int main();
{
  int       myInt = 6500;
  long     myLong = 65000;
  float     myFloat = 6.5F;
  double  myDouble = 6.5e20

  int      doubledInt;
  long    doubledLong;
  float    doubledFloat;
  double doubledDouble;

  cout << "myInt: " << myInt << "\n";
  blah
  blah
  blah

  doubledInt     = Double(myInt);
  doubledLong  = Double (myLong);
  doubledFloat  = Double (myFloat);
  doubleDouble = Double (myDouble);

  cout << "doubledInt: " << doubledInt << "\n";
  blah
  blah
  blah

    return 0;
}
You get THAT idea... Then you have the "Double" functions
Code:
 int Double (int original)
{
  cout << "In Double (int) \n";
  return 2 * original;
}
and the rest is the same exept creating a description/function for each call/prototype from the main function..

Now, while the book says that in this type of polymorphism the function making the call, or the program itself will automatically pick the correct function from the ones listed to do the mathmatical calculations, it doesnt say HOW it knows how to do this. Because I have to tell it how to do everything else (in code which is translated to machine code, or electrical currents if you are going THAT deep), why would I not have to do anything here. Is it something in the compiler itself that is told, that because these are funtions with the same names just different variables, perform a sort-of do while loop? If not, then how does it know....????
__________________

Quote:
Analysis paralysis will keep you from failing, but it will also keep you from succeeding.
  • L.L. CoolJ
Reply With Quote