Main Menu


Sponsored Links

  


  
  
Web Cartoon Maker: a Fun Way to Learn C++ Contents Previous Next

Overloading

In the previous section you might have noticed that Fred and GetArea perform similar functions – finding the area of a circle – but take different parameters. For GetArea , we have to provide the radius; for Fred we provide two points.

If two functions do the same thing, it is natural to give them the same name. In other words, it would make more sense if Fred were called GetArea .

Having more than one function with the same name, which is called overloading , is legal in C++ as long as each version takes different parameters. So we can go ahead and rename Fred :

double GetArea ( double dXC, double dYC, double dXP, double dYP )

{

return = GetArea ( GetDistance ( dXC, dYC, dXP, dYP ) );

}

This looks like a recursive function, but it is not. Actually, this version of area is calling the other version. When you call an overloaded function, C++ knows which version you want by looking at the arguments that you provide. If you write:

double dX = GetArea ( 3.0 );

C++ goes looking for a function named GetArea that takes a double as an argument, and so it uses the first version. If you write:

double dX = GetArea ( 1.0, 2.0, 4.0, 6.0 );

C++ uses the second version of GetArea.

Although overloading is a useful feature, it should be used with caution. You might get yourself nicely confused if you are trying to debug one version of a function while calling a different one.

Note: The above reminds me of one of the cardinal rules of debugging: make sure that the version of the program you are looking at is the version of the program that is running! Some time you may find yourself making one change after another in your program, and seeing the same thing every time you run it. This is a warning sign that for one reason or another you are not running the version of the program you think you are. To check, stick in an output statement (it doesn’t matter what it says) and make sure the behavior of the program changes accordingly.

Note: Additionally, your program code should always contain a comment indicating the date, and possibly a number, defining the current revision. If several programmers are working on a program, a formal revision control system should be implemented – it is far more efficient than scrapping a lot of code because it was based on out of date versions.


Contents Previous Next
  
News

New Tales Animator Video by Alan Sturgess

Alan Sturgess shared an excellent video he made using Tales Animator! You can still download Tales Animator here. Unfortunately it is only available for Wi

...

Simple Online Character Designer

There is a prototype of simple online character designer available HERE. It is only a prototype, it does not contain many pieces yet but it can already generat

...

Book is updated

Now our book "Web Cartoon Maker: A Fun Way to Learn C++" is fully in synch with WCM 1.5! It is available for download and online reading HERE.

...

Web Cartoon Maker 1.5 is here!

Web Cartoon Maker 1.5 is finally here! You can download it HERE! Here is what was updated in version 1.5: Web Cartoon Maker Desktop Edition is now fully standal

...

read more news...


Poll