Go Back   EQEmulator Home > EQEmulator Forums > Archives > Archive::Misc > Archive::Off Topic

Archive::Off Topic Archive area for Off Topic's posts that were moved here after an inactivity period of 90 days.

Reply
 
Thread Tools Display Modes
  #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
  #2  
Old 02-25-2004, 09:29 AM
Badil
Fire Beetle
 
Join Date: Dec 2003
Posts: 16
Default

The compiler looks at the name of the function that you are calling and at the 'types' of the parameters, then looks for a function with that name that takes that 'type' of parameter, and calls that function (or more precisely, the compiler inserts code into the executable to call the that function).
Reply With Quote
  #3  
Old 02-25-2004, 11:40 AM
samandhi's Avatar
samandhi
Demi-God
 
Join Date: Aug 2003
Posts: 1,056
Default

So, then in essence the compiler DOES do a comparative loop of sorts? And by saying that it inserts code into the executable, you mean the the compiler puts a sort-of id tag on those calls to show it which is the correct function for it to make the call to?
__________________

Quote:
Analysis paralysis will keep you from failing, but it will also keep you from succeeding.
  • L.L. CoolJ
Reply With Quote
  #4  
Old 02-25-2004, 11:59 AM
smogo
Discordant
 
Join Date: Jan 2004
Location: 47
Posts: 339
Default

Quote:
the compiler puts a sort-of id tag on those calls
this is basiccally it. The compiler stores any function it compiles with its signature, that is made of the types of its arguments. For example, the sig of a Double(int i) is "I", the sig for Double(float f, float f, void * data) is "FFP"

When the compiler links the code, it searches for the function with the same name, but also the same sig. This is done at link time, not runtime. In the executable, the correct function reference is already done, so no overhead.
Reply With Quote
  #5  
Old 02-25-2004, 12:05 PM
samandhi's Avatar
samandhi
Demi-God
 
Join Date: Aug 2003
Posts: 1,056
Default

Sweet, thank you. That is what I was wondering about (the overhead of storing the info on what function is called by each different call. Or tagging each call at runtime if you will).....
__________________

Quote:
Analysis paralysis will keep you from failing, but it will also keep you from succeeding.
  • L.L. CoolJ
Reply With Quote
  #6  
Old 02-25-2004, 12:14 PM
Merth
Dragon
 
Join Date: May 2003
Location: Seattle, WA
Posts: 609
Default

What you are doing is called overloading, where the compiler determines the correct function while it is compiling.

I'd be careful about using the term "Polymorphism" here because there is something in C++ that is traditionally given that term, but behaves very differently from overloading. In that, the actual function that is executed is not determined until runtime.
Reply With Quote
  #7  
Old 02-25-2004, 12:24 PM
samandhi's Avatar
samandhi
Demi-God
 
Join Date: Aug 2003
Posts: 1,056
Default

deleted
__________________

Quote:
Analysis paralysis will keep you from failing, but it will also keep you from succeeding.
  • L.L. CoolJ
Reply With Quote
  #8  
Old 02-25-2004, 12:26 PM
samandhi's Avatar
samandhi
Demi-God
 
Join Date: Aug 2003
Posts: 1,056
Default

deleted
__________________

Quote:
Analysis paralysis will keep you from failing, but it will also keep you from succeeding.
  • L.L. CoolJ
Reply With Quote
  #9  
Old 02-25-2004, 12:27 PM
samandhi's Avatar
samandhi
Demi-God
 
Join Date: Aug 2003
Posts: 1,056
Default

Oh I see... Well, the book is calling it Polymorphism (which, Im NOT saying your wrong at all), IN the overload section... If the code above is NOT polymorphism, but is overloading, could you maybe give me a simple example of poly?

Matter of fact the source code example is called :
Quote:
A Demonstration of Function Polymorphism
Sorry bout the triple posting. Like a dumbarse instead of editing my post I hit back in browser, then hit submit after adding what I wanted to add... DOH>>>>
__________________

Quote:
Analysis paralysis will keep you from failing, but it will also keep you from succeeding.
  • L.L. CoolJ
Reply With Quote
  #10  
Old 02-25-2004, 01:40 PM
Shadow-Wolf
Dragon
 
Join Date: Oct 2003
Posts: 511
Default

lol im trying to learn how to code in C++ but i never seem to get anything right, might be that i dont have a book to learn by lol.
Reply With Quote
  #11  
Old 02-25-2004, 02:08 PM
samandhi's Avatar
samandhi
Demi-God
 
Join Date: Aug 2003
Posts: 1,056
Default

Well, I just started about 2 months ago. There are some really good books out there but I cant seem to find any that are up to date.... The one I am using is from 1997. It is still up to date in most areas, but some things are a BIT dated, such as #include statements. They have changed somewhat in most compilers and you will even get warnings saying that you are using an antequated header...
__________________

Quote:
Analysis paralysis will keep you from failing, but it will also keep you from succeeding.
  • L.L. CoolJ
Reply With Quote
  #12  
Old 02-25-2004, 02:09 PM
aendaar
Sarnak
 
Join Date: Dec 2003
Location: Hellhole, Texas
Posts: 77
Default

heh..im trying to learn C..i got a damn $30 book....#C for dummies...just a newbie question, whats the diff between the two? #C requires a whole lot more crap...*grabs book* ahh yes, visual studio .NET but thats besides the point whats the difference? because i wasted $30 on a book i cant even use
__________________
If we all took an eye for an eye and a tooth for a tooth, we would have no eyes or teeth.
---confucius...not really...i think it was ghandi
Reply With Quote
  #13  
Old 02-25-2004, 06:03 PM
samandhi's Avatar
samandhi
Demi-God
 
Join Date: Aug 2003
Posts: 1,056
Default

Well Im not totally sure of all the differences, but C# is pretty new, and a ton of things changed just between C and C++. This is the next generation of the C programming language. I have found this paper that might give SOME light to you, only thing is it is called Differences between C++ and C#... And yes, if you were wanting to learn C, then you DID waste $30. Look on the bright side, C# is totally different, but you may want to learn it also <smile>
The paper is here: http://www.libertyassociates.com/pages/C2Cdiff.htm

Also, this might help C and C++ are more alike than C# and any other C programming language, it is more Microsoft oriented. Well here check out this quote:
Quote:
C# runs under the .NET Framework.
:
:
:

So does visual C++.NET if you use managed code.

Good Points of C#
C# is a lot like Java, but creating the GUI is much easier.
All memory management is done for you with a garbage collector.
You can use it to code ASP.NET web pages.
It's easy to learn and use.

Bad Points of C#
It's much slower than unmanaged C++.
It requires the .NET framework to run on a machine.
Only supported on Microsoft based OS's.

That being said, I think that C# has it's place just like any of the languages. It great for some things but not so great for others. I use C# for things like databases and web pages.
__________________

Quote:
Analysis paralysis will keep you from failing, but it will also keep you from succeeding.
  • L.L. CoolJ
Reply With Quote
  #14  
Old 02-26-2004, 02:00 AM
ltlruss's Avatar
ltlruss
Discordant
 
Join Date: Oct 2003
Location: Boise, ID
Posts: 288
Default

Incase anyone wants to check it out, I found a web site that goes through the basics of C++. I just started looking at it so I don't know how much it goes into. Just thought someone might be interested in checking it out. It's at Click here
__________________
Quote:
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.
- Albert Einstein

SEARCH It's your friend
Reply With Quote
  #15  
Old 02-26-2004, 04:07 AM
Merth
Dragon
 
Join Date: May 2003
Location: Seattle, WA
Posts: 609
Default

Quote:
If the code above is NOT polymorphism, but is overloading, could you maybe give me a simple example of poly?
Simple OO polymorphism:
Code:
class foo
{
public:
   virtual void blah() { cout << "in foo::blah"; }
};

class bar : public foo
{
public:
   virtual void blah() { cout << "in bar::blah"; }
};

void main()
{
   bar* mybar = new bar();
   foo* myfoo = (foo*)mybar;
   myfoo->blah();
}
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

   

All times are GMT -4. The time now is 11:29 AM.


 

Everquest is a registered trademark of Daybreak Game Company LLC.
EQEmulator is not associated or affiliated in any way with Daybreak Game Company LLC.
Except where otherwise noted, this site is licensed under a Creative Commons License.
       
Powered by vBulletin®, Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Template by Bluepearl Design and vBulletin Templates - Ver3.3