Blg252e - Mod05 - 6spp Operator Overloading
Blg252e - Mod05 - 6spp Operator Overloading
Operator Overloading
Overloading
►It is possible to overload the built-in C++ operators such
as +, >=, and ++ so that they invoke different functions,
depending on their operands.
Operator Overloading 5
►a+b will call one function if a and b are integers, but will
Operator Overloading call a different function if a and b are objects of a class.
►Operator overloading makes your program easier to write
and to understand.
►Overloading does not actually add any capabilities to C++.
Everything you can do with an overloaded operator you
can also do with a function.
►However, overloaded operators make your programs
easier to write, read, and maintain.
Operator
Operator Overloading
Overloading Limitations
Limitations
►Operator overloading is only another way of calling a ►You can’t overload operators that don’t already exist in
function. C++. You can overload only the built-in operators.
►You have no reason to overload an operator except if it ►You can not overload the following operators
Operator Overloading 5
Operator Overloading 5
will make the code involving your class easier to write and .
especially easier to read. *
►Remember that code is read much more than it is written ->
,
::
?:
sizeof
Limitations
Limitations Overloading
Overloading the
the ++ operator
operator for
for ComplexT
ComplexT
/* A class to define complex numbers */
►The C++ operators can be divided roughly into binary and class TComplex {
unary. Binary operators take two arguments. Examples are float real,img;
public:
a+b, a-b, a/b, and so on. Unary operators take only one
Operator Overloading 5
Operator Overloading 5
: // Member functions
argument: -a, ++a, a--. TComplex operator+(TComplex&); // header of operator+
function
►If a built-in operator is binary, then all overloads of it };
remain binary. It is also true for unary operators. /* The Body of the function for operator + */
TComplex TComplex::operator+(TComplex& z) {
►Operator precedence and syntax (number of arguments) TComplex result;
cannot be changed through overloading. result.real = real + z.real; int main() {
result.img = img + z.img;
►All the operators used in expressions that contain only return result; TComplex z1,z2,z3;
built-in data types cannot be changed. At least one } : // Other operations
z3=z1+z2; like z3 = z1.operator+(z2);
operand must be of a user defined type (class). }
Object Oriented Programming 201 Object Oriented Programming 202
Overloading
Overloading the
the Assignment
Assignment Operator
Operator (=)
(=) Overloading
Overloading the
the Assignment
Assignment Operator
Operator (=)
(=)
►Because assigning an object to another object of the same void ComplexT::operator=(const ComplexT& z)
type is an activity most people expect to be possible, the {
compiler will automatically create a type::operator=(const
Operator Overloading 5
Operator Overloading 5
re = z.re;
type &) if you don’t make one.
im = z.im;
►The behavior of this operator is member wise assignment.
It assigns (copies) each member of an object to members }
of another object. (Shallow Copy)
►You don't need to write such an assignment operator
►If this operation is sufficient you don't need to overload
function, because the operator provided by the compiler
the assignment operator. For example, overloading of
does the same thing.
assignment operator for complex numbers is not
necessary.
Overloading
Overloading the
the Assignment
Assignment Operator
Operator (=)
(=) Example
Example
class string {
►In general, you don’t want to let the compiler do this for int size;
you. char *contents;
Operator Overloading 5
Operator Overloading 5
Operator
Operator Provided
Provided by
by the
the Compiler
Compiler Operator
Operator of
of the
the Programmer
Programmer
Operator Overloading 5
0x008d0080 0x008d0080
0x00185d12 0x008d0080 0x00185d12
0x00ef0080
contents contents
contents: contents X
X
s a s s a
t b t t b
r c r r c
i \0 i i \0
n n n
g Data is still wasting g g
memory space.
1 1 1
\0 \0 \0
Operator Overloading 5
to the object that called the operator function (its address). object into it, whereas the assignment operator copies data
// Assignment operator , can be chained as in a = b = c into an already existing object.
const String& String::operator=(const String &in_object) {
if (size != in_object.size){ // if the sizes of the source and destination
size = in_object.size; // objects are different
delete [] contents; // The old contents is deleted
contents = new char[size+1]; // Memory allocation for the new contents
}
strcpy(contents, in_object.contents);
return *this; // returns a reference to the object
}
Object Oriented Programming 209 Object Oriented Programming 210
Copy
Copy Constructor vs. Assignment
Constructor vs. Assignment Operator
Operator Overloading
Overloading Unary
Unary Operators
Operators
► Unary operators operate on a single operand. Examples are the
►A a; increment (++) and decrement (--) operators; the unary minus, as in -5;
and the logical not (!) operator.
►A b(a);
Operator Overloading 5
Operator Overloading 5
Operator Overloading 5
Operator Overloading 5
overloaded subscript operator can access but not modify the if(i < 0)
return contents[0]; // return first character
object. if(i >= size)
return contents[size-1]; // return last character
If c is an object of class C, the expression return contents[i]; // return i th character
}
c[i] int main() {
is interpreted as String s1("String 1");
s1[1] = 'p'; // modifies an element of the contents
c.operator[ ](i) s1.print();
cout << " 5 th character of the string s1 is: " << s1[5] << endl;
return 0;
}
Object Oriented Programming 215 Object Oriented Programming 216
Operator Overloading 5
"Pre"
"Pre" and
and "post"
"post" form
form of
of operators
operators ++
++ and
and --
-- Post-Increment
Post-Increment Operator
Operator
►Recall that ++ and -- operators come in “pre” and “post”
// postincrement operator
form.
►If these operators are used with an assignment statement ComplexT ComplexT::operator++(int) {
ComplexT temp;
Operator Overloading 5
Operator Overloading 5