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> Code:
int Double (int original) 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....???? |
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).
|
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:
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. |
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).....
|
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. |
deleted
|
deleted
|
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:
|
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.
|
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...
|
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
|
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:
|
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:
Code:
class foo |
All times are GMT -4. The time now is 07:36 AM. |
Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.