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

Oop 3

Operator overloading allows user-defined types to use operators in C++. Only built-in operators can be overloaded and operator functions are called when the corresponding operator is used. Operator overloading functions can be defined as class members or friend functions.

Uploaded by

Pratik Hinge
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Oop 3

Operator overloading allows user-defined types to use operators in C++. Only built-in operators can be overloaded and operator functions are called when the corresponding operator is used. Operator overloading functions can be defined as class members or friend functions.

Uploaded by

Pratik Hinge
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Operator overloading is a powerful feature in C++ that allows programmers to define #include <iostream>

user-defined types and use operators with them. It is a form of compile-time polymorphism using namespace std; int main () {
that gives special meaning to an existing operator in C++ without changing its original class Complex { Complex c1 (10, 5), c2 (2, 4);
meaning . private: Complex c3 = c1 + c2;
In C++, the name of an operator function is always the operator keyword followed by the int real, imag; c3.print ();
symbol of the operator, and operator functions are called when the corresponding operator public: }
is used. However, there are some rules for operator overloading in C++ that must be Complex (int r = 0, int i = 0) {
followed. For example, only built-in operators can be overloaded, and the arity of the real = r;
operators cannot be changed. imag = i; }
// C++ Program to Demonstrate the working/Logic behind OperatorOverloading Complex operator+ (Complex const& obj) {
class A { Complex res;
statements; res.real = real + obj.real;
}; res.imag = imag + obj.imag;
int main() return res; }
{ A a1, a2, a3; void print () {
a3 = a1 + a2; cout << real << " + i" << imag << '\n';
return 0; }
} }:
Restrictions on Operators Overloading: In C++, there are some rules that must be In C++, operator functions can be defined as either class members or
followed when overloading operators : friend functions
Class member functions are functions that are declared inside the class
1. Only built-in operators can be overloaded. New operators cannot be created. definition and have access to the private and protected members of the
2. The arity of the operators cannot be changed. class. They are called using the object of the class and the dot operator.
3. Precedence and associativity of the operators cannot be changed. Class member functions are used when the left operand of the operator
4. Overloaded operators cannot have default arguments except the function call is an object of the class
operator () which can have default arguments.
5. Operators cannot be overloaded for built-in types only. At least one operand must Friend functions, on the other hand, are not members of the class but
be a user-defined type. are declared inside the class definition using the friend keyword. They
6. Assignment (=), subscript ([]), function call (()), and member selection (->) have access to the private and protected members of the class and are
operators must be defined as member functions. called using the object of the class and the scope resolution operator.
7. Except for the operators specified in point 6, all other operators can be either Friend functions are used when the left operand of the operator is not an
member functions or non-member functions. object of the class.
8. Some operators like (assignment)=, (address)& and comma (,) are by default Merits:
overloaded. • Using friend functions is generally syntactic, i.e., both member
function and friend function are equally privileged.
In C++, unary operators are operators that operate on a single operand. Unary operators • A friend function can be called like f(obj), where a member is
can be overloaded in C++ by defining a member function with the return type of your called like obj.f().
requirement. Then comes the “operator” keyword, followed by the sign of the • Friend functions are used when two or more classes are
operator. The function takes no arguments and returns a value . designed to be more tightly coupled than you want for.
Here are some examples of unary operators that can be overloaded in C++ : Demerits:
• + (unary plus) - (unary minus) * (pointer dereference) • They don’t bind dynamically, i.e., they don’t respond to
• & (address-of) ! (logical NOT) ~ (bitwise complement) polymorphism. A derived class does not inherit a friend
• ++ (prefix increment) -- (prefix decrement) function.
• Friendship is not inherited in C++. A friend function of a class is
not a friend of its derived classes.
• Using friend functions can make the code less readable and
harder to maintain.

//UNARY OPERATOR // FRIEND AND CLASS MEMBER FUNCTION PROGRAM.


#include <iostream> #include <iostream>
using namespace std; using namespace std;

class Number { class Complex {


private: private:
int num; int real, imag;
public: public:
Number (int n = 0) { Complex (int r = 0, int i = 0) {
num = n; real = r;
} imag = i;
void display () { }
cout << "Number = " << num << '\n'; Complex operator+ (Complex const& obj) {
} Complex res;
Number operator- () { res.real = real + obj.real;
Number temp; res.imag = imag + obj.imag;
temp.num = -num; return res;
return temp; }
} void print () {
}; cout << real << " + i" << imag << '\n';
int main () { }
Number n1 (10), n2 (-5); friend Complex operator- (Complex const& obj1, Complex const&
n1.display (); obj2);
n2.display (); };
-n1;
-n2; Complex operator- (Complex const& obj1, Complex const& obj2) {
n1.display (); Complex res;
n2.display (); res.real = obj1.real - obj2.real;
return 0; res.imag = obj1.imag - obj2.imag;
} return res;

int main () {
Complex c1 (10, 5), c2 (2, 4);
Complex c3 = c1 + c2;
Complex c4 = c1 - c2;
c3.print ();
c4.print ();
}
In C++, a member function is a function that is declared inside a class definition and has In C++, the scope resolution operator is denoted by :: and is used to
access to the private and protected members of the class . Member functions are called access the global variable or function when there is a local variable or
using the object of the class and the dot operator. They are used to perform operations on function with the same name . It is also used to define a function
the data members of the class. #include <iostream> outside the class definition .
using namespace std; Here are some examples of using the scope resolution operator in C++ :
• To access a global variable: ::x
class Rectangle { • To access a global function: ::function ()
private: • To define a function outside the class definition: return_type
int length, width; class_name::function_name () { ... }
public: #include <iostream>
Rectangle (int l = 0, int w = 0) { using namespace std;
length = l;
width = w; int x = 10;
}
int area () { int main () {
return length * width; int x = 5;
} cout << "Local variable x = " << x << '\n';
}; cout << "Global variable x = " << ::x << '\n';
int main () { return 0;
Rectangle r (10, 5); }
cout << "Area of rectangle = " << r.area () << '\n';
return 0;
}
Aspect Member Function Friend Function Normal Function In C++, binary operators are operators that operate on two operands.
Location Defined within the class Declaration inside Defined outside the Binary operators can be overloaded in C++ by defining a member
the class, defined class function or a non-member function with the return type of your
outside requirement. Then comes the “operator” keyword, followed by the sign
Access to Direct access to all Access to private No direct access to of the operator. The function takes one or two arguments and returns a
members class members members with private members value .
'friend' keyword Here are some examples of binary operators that can be overloaded in
Access Has access to public, Access to private Access to public C++ .
specifiers protected, and private and protected members only • + (addition)
members members • - (subtraction)
'this' pointer Implicit 'this' pointer Does not have 'this' No 'this' pointer • * (multiplication)
available pointer available • / (division)
Object Called using object Not associated Not associated with • % (modulus)
association instances with any object any object • == (equality)
Syntax Defined with class Declared with Defined globally or • != (inequality)
scope (::) 'friend' keyword within a namespace • < (less than)
Access Inherited and accessible Not inherited by Not inherited by • > (greater than)
permissions to derived classes derived classes derived classes • <= (less than or equal to)
Usage Often used for Used for granting Used for standalone • >= (greater than or equal to)
manipulating object external functions functionality • << (left shift)
attributes access • >> (right shift)
• & (bitwise AND)
• | (bitwise OR)
• ^ (bitwise XOR)
Here is an example of overloading the binary operator + in C++:

You might also like