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

C++ operator overloading

Operator overloading in C++ allows users to redefine the meaning of operators for user-defined data types, enabling operations like string concatenation with the '+' operator. The syntax for operator overloading involves defining a function with the operator keyword followed by the operator symbol. Examples are provided for overloading both unary and binary operators, demonstrating how to implement and use these functionalities in C++.

Uploaded by

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

C++ operator overloading

Operator overloading in C++ allows users to redefine the meaning of operators for user-defined data types, enabling operations like string concatenation with the '+' operator. The syntax for operator overloading involves defining a function with the operator keyword followed by the operator symbol. Examples are provided for overloading both unary and binary operators, demonstrating how to implement and use these functionalities in C++.

Uploaded by

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

UNIT - III

Operator Overloading:
• Operator overloading is a compile-time polymorphism in which the operator is
overloaded to provide the special meaning to the user-defined data type.
Operator overloading is used to overload or redefines most of the operators
available in C++.
• C++ has the ability to provide the operators with a special meaning for a data
type, this ability is known as operator overloading. Operator overloading is a
compile-time polymorphism. For example, we can overload an operator ‘+’ in a
class like String so that we can concatenate two strings by just using +.
Operator Overloading Syntax

Where the return type is the type of value returned by the function.
class_name is the name of the class.
OperatorSymbol is an operator function where Symbol is the operator being
overloaded, and the operator is the keyword.

Difference between Operator Functions and Normal Functions


• Operator functions are the same as normal functions. The only differences are,
that the name of an operator function is always the operator keyword followed
by the symbol of the operator, and operator functions are called when the
corresponding operator is used.
Example Program for – Overloading Unary Operators
#include <iostream> Output:
using namespace std; The Count is: 10
class Test
{
private:
int num;
public:
Test(): num(8){}
void operator ++() {
num = num+2;
}
void Print() {
cout<<"The Count is: "<<num;
}
};
int main()
{
Test tt;
++tt; // calling of a function "void operator ++()"
tt.Print();
return 0;
}
Example Program for Overloading Binary Operators:
#include<iostream.h> void complex :: Output:
#include<conio.h> show(complex c)
{
class complex cout<<c.x<<"+i"<<c.y<<"\n";
{ }
float x, y;
int main()
public: {
void input(float real, clrscr();
float img) complex A,B,C;
{ A.input(3.1, 5.65);
x=real; y=img; B.input(1.1, 9.12);
}
cout<<"A=";
friend complex A.show(A);
sum(complex, cout<<"B=";
complex); B.show(B);
void show(complex);
}; C = sum(A,B);

complex cout<<"Sum is:"; C.show(C);


sum(complex c1,
complex c2) getch();
{ return 0;
complex c3; }

c3.x = c1.x + c2.x;


c3.y = c1.y + c2.y;

return (c3);
}

You might also like