0% found this document useful (0 votes)
26 views5 pages

UNIT III Operator Overloading and Type Conversion

Uploaded by

Hello Hello
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

UNIT III Operator Overloading and Type Conversion

Uploaded by

Hello Hello
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

UNIT III Operator Overloading and Type Conversion & Inheritance:

The Keyword Operator, Overloading Unary Operator, Operator Return Type, Overloading
Assignment Operator (=), Rules for Overloading Operators, Inheritance, Reusability, Types of
Inheritance, Virtual Base Classes- Object as a Class Member, Abstract Classes, Advantages of
Inheritance, Disadvantages of Inheritance.

The Keyword Operator:


 An operator is a symbol or sign used to specify an operation.
 An operator is an object capable of manipulating a value or operator. For example, in
"1 + 2", the "1" and "2" are the operands and the plus symbol is the operator.

Operator Overloading in C++:

 Operator overloading in C++ refers to the ability to redefine or customize the behavior
of certain operators when they are used with user-defined data types (classes and
structs).
 This allows you to provide a specific meaning or action for operators beyond their built-
in functionality.
 Basically, Overloading in C++ is the process of creating two or more members with the
same name but differing numbers or types of parameters.

List of Operators that can be Overloaded in C++:

When it comes to operator overloading in C++ program, we can overload the following
operators:
 Binary operators

 Unary operators

 Special operators

Here is a table representing all the operators with examples that you can overload in C++:

Unary arithmetic operators –, ++, -, +

Binary arithmetic operators %, /, *, -, +

De-referencing operator (->)

Bitwise operator ^, ~, >>, <<, |, &

Assignment operator %=, -=, /=, *=, +=, =

Subscript operator []

De-allocation New, delete

Relational operator >=. <=, ==, <, >

Logical operator !, | |, &


1
Function Call ()

Rules for overloading operators in C++:

 The only existing operator can be overloaded. New operators cannot be overloaded.
 Every overloaded operator must contain at least one operand of user-defined datatype.
 During operator overloading, we cannot change the basic meaning of operators.
 If unary operators are overloaded by a member function, then they take no explicit
arguments. But if they are overloaded through a friend function, then take one
argument.
 If binary operators are overloaded by a member function, then they take one explicit
argument. But, if they are overloaded through a friend function, then take two
arguments.

We cannot overload the following operators in c++:


 Scope resolution operator(::)
 Ternary operator(?:)
 Member selector(.)
 Sizeof operator
 Member pointer selector(*)

Syntax:

class ClassName
{
...  The keyword is the operator.
public:
...  The return type of the function is
return-type operator op(params-list) ‘returnType.’
{
 The arguments passed to the
//body of the function
...
function are arguments.
}  The operator is nothing but the
... symbol that you need to overload.
};

2
Overloading Unary Operator:
#include <iostream> #include <iostream>
using namespace std; using namespace std;

class Count class Hello


{ {
private: int x = 1, y = 2;
int value; public:
public: void print()
// Constructor to initialize count to 5 {
Count(int x) cout << x << " " << y << " " ;
{ }
value=x; // ++ operator overloading defined here
} void operator ++()
// Overload ++ when used as prefix {
void operator ++ () x = x + 100;
{ y = y + 100;
++value;
cout << "Count: " << value << endl; }
} };
};
int main() { int main()
Count count1(5); {
// Call the "void operator ++ ()" function Hello obj;
//count1.operator ++(); cout << "Values initially were -" << endl;
count1.operator ++(); obj.print();
// calling overloaded ++ operator on obj
return 0; object
} ++obj;
cout << "\nNew Values are -" << endl;
Output: obj.print();
return 0;
Count 6 }

Output:

Values initially were -


12
New Values are -
101 102

3
Overloading Binary Operator:

#include <iostream> #include <iostream>


using namespace std; using namespace std;
class A
{ class binaryOperator
{
int x; public:
public: int a, b;
A(int i) public:
{ binaryOperator()
x=i; {}
} binaryOperator(int c, int d)
void operator+(A a) {
{ a = c;
int m = x+a.x; b = d;
cout<<" addition of two objects is : "<<m; }
void operator + (binaryOperator obj)
} {
cout << "obj1.a + obj2.a: " << a +
}; obj.a << endl << endl;
int main() cout << "obj1.b + obj2.b: " << b +
{ obj.b;
A a1(5); }
A a2(4); };
a1+a2; int main()
{
return 0; binaryOperator obj1(10, 20);
} binaryOperator obj2(30, 40);
obj2 + obj1;
Output: return 0;
}
addition of two objects is : 9 Output:

obj1.a + obj2.a: 40

obj1.b + obj2.b: 60

4
Overloading ! Operator

#include <iostream>
using namespace std;

class NotOp {
private:
int a;
bool b;
public:
NotOp() : a(false), b(true) {}
// Overloading the NOT (!) operator
void operator ! () {
a= !a;
b= !b;
}
void out() {
cout<<"Output: "<<endl<< a<<endl<<b;
}
};
int main() {
NotOp obj;
!obj;
obj.out();
return 0;
}
Output:
Output:
1
0

You might also like