Operator overloading
Operator overloading:-
Operator overloading is a type of a compile time polymorphism in C++. In C++ an operator is a symbol that tells the compiler to perform specific task. Every operator have their own functionality to work with data types. Class is user-defined data type so, compiler doesn't understand, how to use operators with user-defined data types. To use operators with user-defined data types, they need to be overload according to a programmer's requirement.
{tocify} $title={Table of Contents}
Operator overloading is a way of providing new implementation of existing operators to work with user-defined data types.
C++ provides a wide variety of operators to perform operations on various operands.
C++ allows almost all operators to be overloaded in which case at least one operand must be an instance of a class.
C++ allows most operators to be overloaded for user-defined types(classes).
The following operators can be overloaded:
The following can not be overloaded:
Defining operator overloading:-
To define an additional task to an operator, we must specify what it means in relation to the class to which the operator is applied. This is done with the help of a special function, Called operator function, which describe the task.
The general form of operator function is:-
Return type classname
:: operator op(arglist)
{
Function body // task defined
}
|
Where return type is the type of value returned by the specified
operation and op is the operator being overloaded is applied.
The op is preceded by the keyword operator. Operator op is the function name.
complex complex ::
operator+(complex c)
|
Where first complex
is return type so, it
return a complex value and second complex is classname.
In this example we overload a + operator
Operator overloading example:-
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class complex
{
float r; // real part
float i; // imaginary part
public:
complex() //constructor 1
{}
complex(float real,float imag) // constructor 2
{
r=real;
i=imag;
}
complex operator+(complex);
void display(void);
};
complex complex ::
operator+(complex c)
{
complex temp;
temp.r = r+c.r;
temp.i = i+c.i;
return(temp);
}
void complex :: display(void)
{
cout<<r<<"+j"<<i<<endl;
}
void main()
{
complex c1,c2,c3;
c1 = complex(1.5 , 3.5);
c2 = complex(2.5 , 4.5);
c3 = c1+c2;
cout<<"c1 = ";
c1.display();
cout<<endl<<"c2
= ";
c2.display();
cout<<endl<<"c3
= ";
c3.display();
getch();
}
|
Where, In this program
we using constructor and, there are four member function two function are
constructor function, one operator function and one display function. The class
of the program is complex, and
two constructor function are:-
First constructor function is blank and second is use to call the data member of the class.
The operator function is:-
complex complex ::
operator+(complex c)
{
complex temp;
temp.r = r+c.r;
temp.i = i+c.i;
return(temp);
}
|
There are two types of
operator overloaing in C++
·
Binary Operator
Overloading
·
Unary Operator
Overloading
📘 IT Tech Language
☁️ Cloud Computing - What is Cloud Computing – Simple Guide
- History and Evolution of Cloud Computing
- Cloud Computing Service Models (IaaS)
- What is IaaS and Why It’s Important
- Platform as a Service (PaaS) – Cloud Magic
- Software as a Service (SaaS) – Enjoy Software Effortlessly
- Function as a Service (FaaS) – Serverless Explained
- Cloud Deployment Models Explained
🧩 Algorithm - Why We Learn Algorithm – Importance
- The Importance of Algorithms
- Characteristics of a Good Algorithm
- Algorithm Design Techniques – Brute Force
- Dynamic Programming – History & Key Ideas
- Understanding Dynamic Programming
- Optimal Substructure Explained
- Overlapping Subproblems in DP
- Dynamic Programming Tools
🤖 Artificial Intelligence (AI) - Artificial intelligence and its type
- Policy, Ethics and AI Governance
- How ChatGPT Actually Works
- Introduction to NLP and Its Importance
- Text Cleaning and Preprocessing
- Tokenization, Stemming & Lemmatization
- Understanding TF-IDF and Word2Vec
- Sentiment Analysis with NLTK
📊 Data Analyst - Why is Data Analysis Important?
- 7 Steps in Data Analysis
- Why Is Data Analysis Important?
- How Companies Can Use Customer Data and Analytics to Improve Market Segmentation
- Does Data Analytics Require Programming?
- Tools and Software for Data Analysis
- What Is the Process of Collecting Import Data?
- Data Exploration
- Drawing Insights from Data Analysis
- Applications of Data Analysis
- Types of Data Analysis
- Data Collection Methods
- Data Cleaning & Preprocessing
- Data Visualization Techniques
- Overview of Data Science Tools
- Regression Analysis Explained
- The Role of a Data Analyst
- Time Series Analysis
- Descriptive Analysis
- Diagnostic Analysis
- Predictive Analysis
- Pescriptive Analysis
- Structured Data in Data Analysis
- Semi-Structured Data & Data Types
- Can Nextool Assist with Data Analysis and Reporting?
- What Kind of Questions Are Asked in a Data Analyst Interview?
- Why Do We Use Tools Like Power BI and Tableau for Data Analysis?
- The Power of Data Analysis in Decision Making: Real-World Insights and Strategic Impact for Businesses
📊 Data Science - The History and Evolution of Data Science
- The Importance of Data in Science
- Why Need Data Science?
- Scope of Data Science
- How to Present Yourself as a Data Scientist?
- Why Do We Use Tools Like Power BI and Tableau
- Data Exploration: A Simple Guide to Understanding Your Data
- What Is the Process of Collecting Import Data?
- Understanding Data Types
- Overview of Data Science Tools and Techniques
- Statistical Concepts in Data Science
- Descriptive Statistics in Data Science
- Data Visualization Techniques in Data Science
- Data Cleaning and Preprocessing in Data Science
🧠 Machine Learning (ML) - How Machine Learning Powers Everyday Life
- Introduction to TensorFlow
- Introduction to NLP
- Text Cleaning and Preprocessing
- Sentiment Analysis with NLTK
- Understanding TF-IDF and Word2Vec
- Tokenization and Lemmatization
🗄️ SQL
💠 C++ Programming - Introduction of C++
- Brief History of C++ || History of C++
- Characteristics of C++
- Features of C++ || Why we use C++ || Concept of C++
- Interesting Facts About C++ || Top 10 Interesting Facts About C++
- Difference Between OOP and POP || Difference Between C and C++
- C++ Program Structure
- Tokens in C++
- Keywords in C++
- Constants in C++
- Basic Data Types and Variables in C++
- Modifiers in C++
- Comments in C++
- Input Output Operator in C++ || How to take user input in C++
- Taking User Input in C++ || User input in C++
- First Program in C++ || How to write Hello World in C++ || Writing First Program in C++
- How to Add Two Numbers in C++
- What are Control Structures in C++ || Understanding Control Structures in C++
- What are Functions and Recursion in C++ || How to Define and Call Functions
- Function Parameters and Return Types in C++ || Function Parameters || Function Return Types
- Function Overloading in C++ || What is Function Overloading
- Concept of OOP || What is OOP || Object-Oriented Programming Language
- Class in C++ || What is Class || What is Object || How to use Class and Object
- Object in C++ || How to Define Object in C++
- Polymorphism in C++ || What is Polymorphism || Types of Polymorphism
- Compile Time Polymorphism in C++
- Operator Overloading in C++ || What is Operator Overloading
- Python vs C++ || Difference Between Python and C++ || C++ vs Python
🐍 Python - Why Python is Best for Data
- Dynamic Programming in Python
- Difference Between Python and C
- Mojo vs Python – Key Differences
- Sentiment Analysis in Python
🌐 Web Development
🚀 Tech to Know & Technology
- The History and Evolution of Data Science
- The Importance of Data in Science
- Why Need Data Science?
- Scope of Data Science
- How to Present Yourself as a Data Scientist?
- Why Do We Use Tools Like Power BI and Tableau
- Data Exploration: A Simple Guide to Understanding Your Data
- What Is the Process of Collecting Import Data?
- Understanding Data Types
- Overview of Data Science Tools and Techniques
- Statistical Concepts in Data Science
- Descriptive Statistics in Data Science
- Data Visualization Techniques in Data Science
- Data Cleaning and Preprocessing in Data Science

