Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Function overloading
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Constructor overloading== [[Constructor (object-oriented programming)|Constructors]], used to create instances of an object, may also be overloaded in some [[object-oriented]] [[programming language]]s. Because in many languages the constructor's name is predetermined by the name of the class, it would seem that there can be only one constructor. Whenever multiple constructors are needed, they are to be implemented as overloaded functions. In [[C++]], [[default constructor]]s take no parameters, instantiating the object [[Instance variable|members]] with their appropriate default values, "which is normally zero for numeral fields and empty string for string fields".<ref>{{cite book |last1=Chan |first1=Jamie |title=Learn C# in One Day and Learn It Well |date=2017 |isbn=978-1518800276 |page=82 |edition=Revised}}</ref> For example, a default constructor for a restaurant bill object written in C++ might set the tip to 15%: <syntaxhighlight lang=Cpp> Bill() : tip(0.15), // percentage total(0.0) { } </syntaxhighlight> The drawback to this is that it takes two steps to change the value of the created Bill object. The following shows creation and changing the values within the main program: <syntaxhighlight lang=Cpp> Bill cafe; cafe.tip = 0.10; cafe.total = 4.00; </syntaxhighlight> By overloading the constructor, one could pass the tip and total as parameters at creation. This shows the overloaded constructor with two parameters. This overloaded constructor is placed in the class as well as the original constructor we used before. Which one gets used depends on the number of parameters provided when the new Bill object is created (none, or two): <syntaxhighlight lang=Cpp> Bill(double tip, double total) : tip(tip), total(total) { } </syntaxhighlight> Now a function that creates a new Bill object could pass two values into the constructor and set the data members in one step. The following shows creation and setting the values: <syntaxhighlight lang=Cpp> Bill cafe(0.10, 4.00); </syntaxhighlight> This can be useful in increasing program efficiency and reducing code length. Another reason for constructor overloading can be to enforce mandatory data members. In this case the default constructor is declared private or protected (or preferably deleted since [[C++11]]) to make it inaccessible from outside. For the Bill above total might be the only constructor parameter{{snd}} since a Bill has no sensible default for total{{snd}} whereas tip defaults to 0.15.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)