0% found this document useful (0 votes)
66 views

Operator Overloading DK Mamonai

This document discusses operator overloading in C++. Operator overloading allows operators to be used with user-defined types by redefining their meaning. It allows extending the functionality of C++ by overloading operators for classes. Unary operators like increment (++) can be overloaded by defining a member function with the operator keyword followed by the operator symbol. Two examples show overloading unary ++ to increment object counters.

Uploaded by

Darya Memon
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF or read online on Scribd
0% found this document useful (0 votes)
66 views

Operator Overloading DK Mamonai

This document discusses operator overloading in C++. Operator overloading allows operators to be used with user-defined types by redefining their meaning. It allows extending the functionality of C++ by overloading operators for classes. Unary operators like increment (++) can be overloaded by defining a member function with the operator keyword followed by the operator symbol. Two examples show overloading unary ++ to increment object counters.

Uploaded by

Darya Memon
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF or read online on Scribd
You are on page 1/ 3

OBJECT: TO

BECOME FAMILIAR WITH

OPERATOR OVERLOADING.

Operator Overloading Operator overloading is one of the main feature of object oriented programming. The operator overloading means adding the functionality of the operators to the user defined data types. As suppose there are three variables a, b, & c all of type int then,

int a; int b; int c; a+b+c;


The above statement (a+b+c) is legal as you can use the addition operator with the integer type data but when you want to use the same addition operator with your own defined data types then what use will do, here the concept of operator overloading help to solve this hurdle. The operator overloading gives you the opportunity to redefine the C++ programming language. As you can also overload or change the functionality of the operators with the predefined data types. By using the classes and the operator overloading you can extend the functionality of C++ language in many ways.

The Keyword Operator To teach the compiler that we are overloading we use the keyword operator in the decelerator of the member function in the class with the return type and the operator follows the keyword operator and at last the parenthesis come enclosing the arguments in to it like in below decelerator:

void operator ++()


Here the operator ++ is to be overloaded and in the decelerator the return type is void and there are no arguments in the decelerator.

Overloading Unary Operators The unary operators are those which require one operand to operate. Like the increment and decrement operators.

Program (OptOver.cpp)
#include <constream.h> class Counter { private: int count; public: Counter(){count=0;} int get_count(){return count;} void operator ++(){++count;} }; void main() { Counter C1, C2; cout<<C1 = <<C1.get_count()<<endl; cout<<C2 = <<C2.get_count()<<endl; ++C1; ++C2; ++C2; cout<<C1 = <<C1.get_count()<<endl; cout<<C2 = <<C2.get_count()<<endl; getch(); }
In the above program the unary operator ++ is overloaded with the objects C1 and C2 in which it increments both the objects by the value of one as it does with other predefined data type.

====================================================================

Program (OptOver2.cpp)
#include <constream.h> class Counter { private: int count; public: Counter(){count=0;} int getcount(){return count;} void operator ++(){count--;} }; void main() { Counter C1, C2; cout<<C1.getcount()<<endl; cout<<C2.getcount()<<endl; int g=++C1; cout<<endl<<g; ++C2; ++C2; int h=C2; cout<<endl<<h; getch(); } ====================================================================

You might also like