English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm taking the final version of the C++ course offered by my jr college, and up to this point I have had almost no trouble understanding and implementing what I've learned in class and in my book, however I'm having a hard time understanding overloading, weither be it function, or operator (espically operator).

I am not looking for a textbook def, or a google define, or anything of the sort. My main questions are what I'm having a hard time understanding.

1)My text book says that operator overloading does not allow the programmer to create NEW operators, but extend the def of pre-exisitng ones. I dont see how this is very useful being as the examples in the book (where the overloading functions are defined {take + as an example} are comparing data members such as the example below})

int operator+(const class1& obj1, const class1& obj2)
{
class1 objtemp;
objtemp = obj1.length + obj2.length;
return value;
}

The rest will posted in an addiontal detail, I am out of space.

2006-10-25 11:42:40 · 3 answers · asked by D 4 in Computers & Internet Programming & Design

2)How exactly is this usefull in the bussiness world, I realize that perhaps it would save programming time to explictly overload operators, but isn't the idea in the bussiness world ot keep programs as simple as possible so that you can pass the work on to someone else and they can understand it?

3)Any thing else i should know???I'm sorry this chapter has me very confused!!

2006-10-25 11:44:49 · update #1

3 answers

As you have already figured out, operator overloading simply allows the developer to define custom functionality for a given operator (such as +, -, /, *, etc.). I have found this is mostly useful in performing math-like operations on objects. For example: you're working on a payroll application and you have two objects that represent an employee's last two paychecks (class paycheck). Since the objects are really pointers, simply adding the two objects together would likely add the two addresses they exist at together. However, by overloading +, you can have one of the two objects add the other objects values (like gross income, taxes, vacation hours, medical expenses, and net income) to itself.

2006-10-25 18:18:22 · answer #1 · answered by Anonymous · 1 0

Operators are ALREADY overloaded to begin with. The concept is simply extended so programmers can do more of it. For example, you can use the "+" operator on integers, floats, doubles, and strings! So it is already overloaded, in other words, it operates differently depending on the kind of objects it operates on.

This can be misused, but it is intended for situations where it makes sense to reuse the "+" operator. Of course, you can overload it to do any thing and if you make a poor choice then it will make the program more difficult to read. A good choice will make it easier to read.

This is associated with the concept of "polymorphism".

2006-10-25 11:56:01 · answer #2 · answered by DadOnline 6 · 0 0

Overloading is while u can use many knowledge varieties with the equal role. AnObject.Function(Parameter datatype) You can enforce the role a number of occasions and every time the information kind is one of a kind , so the equal process or role will also be known as with one of a kind parameter’s knowledge kind ! Overriding is while u inherit a category and u had to difference the conduct of a role within the inherited magnificence so u adjust or override the process via exchanging the behaviour of it while inherited. Do no longer understand approximately partial and hiding!

2016-09-01 02:39:25 · answer #3 · answered by ? 4 · 0 0

fedest.com, questions and answers