Overload (programming)

Overload (programming)

To "overload" a method in programming is to have two or more methods with the same name, but are distinguished by the number and/or type of variables they require. Generally methods, operators, and constructors are overloaded in programming. Overloading is a feature available only to object oriented programming languages like C++,Java. However, in case Java, operators can not be overloaded.

Overloading Methods

For example, "doTask()" and "doTask(object O)" are overloaded methods. To call the latter, an object must be passed as a parameter, whereas the former does not require a parameter, and is called with an empty parameter field.A common error would be to assign a default value to the object in the second method, this would result in an "ambiguous call" error, as the compiler wouldn't know which of the two methods to use.

Another example would be a "Print(object O)" method. In this case we would like the method to be different when printing, for example, text and different for pictures. We write the two different methods as overloaded: "Print(text_object T); Print(image_object P)". If we write the overloaded print methods for all objects our program will "print", we never have to worry about the type of the object, and the correct function call again, the call is always: "Print(something)".

Overloading Operators

It is also possible to overload operators (such as plus, minus, multiply, divide), comparisons ( such as >, <, =), input and output, et al. Overloading operators serves a purpose when performing math and other operations on uncommon types, especially user defined data types. One could overload the addition operator to add two dates together (in the context of the following one would be adding two time spans together, not necessarily actual dates, and the code doesn't allow for months with less than 31 days). This would be accomplished by something similar to the following syntax (in C++):

Date Date::operator+(int Month, int Day, int Year) { Date temp; temp.Year += Year; temp.Day += Day; if temp.Day > 31 { temp.Day - 31; temp.Month + 1; } temp.Month += Month; if temp.Month > 12 { temp.Month - 12; temp.Year + 1; } }

Addition is a binary operation, which means it has a left and right operand. In C++, the "temp" object in this case is the left operand and the arguments being passed are the right operands. Note that a unary operator would receive no arguments since it doesn't have any operands.

The compiler would be able to differentiate the two addition operators because one is passed three parameters instead of the normal addition which is passed just one. As mentioned above however, methods are overloaded based on their number AND type of parameters.Another overloaded addition operator could be overloaded with three parameters IF the data types passed are different too. The compiler would differentiate those two by their data types. The following shows how it would be setup using an angle as a data type with Degree-Minute-Second notation "within the same program".

Angle Angle::Operator+(double Degree, double Minutes, double Seconds) { "code" }

The different data types, one being integers, the other being doubles, allows this to be valid in the compiler. Any number of combinations could be created, as long as they don't have the same number of parameters of the same data types.

Overloading Constructors

The third type of overloading can be done with constructors, which are used to create an instance of an object. The same principle applies that it's the number and/or types of parameters passed into the constructor that decides the overloading.

A default constructor usually doesn't have any parameters passed, normally just setting the objects members to certain values. For example, a default constructor for a restaurant bill object might set the Tip to 15%:

Bill() { tip = 15.0, total = 0.0 }

The drawback to this is that is takes two steps to change the value of the created Bill object. The following shows creation and changing the values within the main program:

Bill cafe; cafe.tip = 10.00; cafe.total = 4.00;

By overloading the constructor, you could pass parameters into it to set the tip or total to a given value at creation. This shows the overloaded constructor with two parameters:

Bill(double setTip, double setTotal) { tip = setTip, total = setTotal }

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:

Bill cafe(10.00, 4.00);

This way of programming can be very useful in increasing program efficiency and reducing code in the long run. For every Bill object the user creates, it can be done in one step using the same piece of code over.


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Comparison of programming languages (object-oriented programming) — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • Extensible programming — is a term used in computer science to describe a style of computer programming that focuses on mechanisms to extend the programming language, compiler and runtime environment. Extensible programming languages, supporting this style of programming …   Wikipedia

  • Oxygene (programming language) — Oxygene Developer RemObjects Software Stable release 3.0.21 (August 29, 2009; 2 years ago (2009 08 29)) Influenced by Object Pas …   Wikipedia

  • Methods of neuro-linguistic programming — NLP TOPICS   …   Wikipedia

  • Lua (programming language) — Infobox programming language name = Lua paradigm = Multi paradigm: scripting, imperative, functional year = 1993 designer = Roberto Ierusalimschy Waldemar Celes Luiz Henrique de Figueiredo developer = latest release version = 5.1.4 latest release …   Wikipedia

  • Template (programming) — Templates are a feature of the C++ programming language that allow functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one. Templates are of… …   Wikipedia

  • Polymorphism in object-oriented programming — In simple terms, polymorphism is the ability of one type, A, to appear as and be used like another type, B. In strongly typed languages, this usually means that type A somehow derives from type B, or type A implements an interface that represents …   Wikipedia

  • List of object-oriented programming terms — Those words found in object oriented programming. Some are related to OOP and some not. A * Abstract class (also called deferred class) * Abstract method * Abstraction (computer science) * Access control * Accessor method * Allocated class *… …   Wikipedia

  • techno-creep — n. The gradual encroachment of technology into every aspect of society. Also: technocreep, techno creep. Example Citations: Technology is also infringing on our home lives, with Moggridge citing a frightening trend in South Korea to have… …   New words

  • ACCU (organisation) — ACCU (previously known as Association of C and C++ Users ) is a non profit, worldwide user group of people interested in software development, dedicated to raising the standard of programming. Originally, the association was primarily for C… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”