Skip to main content

C++ Code for Operator Overloading

A C++ program that demonstrates Operator Overloading for the + operator on a Complex number class.
#include <iostream>
using namespace std;
class Complex {
int real, imag; // Real and imaginary parts
public:
// Constructor with default values
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
// Overloading + operator
Complex operator + (Complex const &obj) {
// Add real and imaginary parts separately
return Complex(real + obj.real, imag + obj.imag);
}
// Function to display complex number
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(2, 3), c2(4, 5);
// Calls overloaded operator+
Complex c3 = c1 + c2;
// Display result
c3.display(); // Output: 6 + 8i
return 0;
}
Output 
 6 + 8i

Explanation

Complex(int r = 0, int i = 0) Constructor initializes complex real and imaginary values.
Complex operator + (Complex const &obj) → Operator + Overloads operator + to add two complex numbers.

Adds real parts separately.
One adds imaginary parts one at a time.
c1 + c2 is converted automatically in main to c1. operator + c2.
Result in c3 is added to result in c2 = (2 + 4) + (3 + 5)i = 6 + 8i.

This is a classic Operator Overloading example.

Thanks for reading 💗!


If you found this post useful:

⮕  Share it with others who might benefit.
⮕  Leave a comment with your thoughts or questions—I’d love to hear from you.
⮕  Follow/Subscribe to the blog for more helpful guides, tips, and insights.


Comments

Popular posts from this blog

Why Python Is the Best Programming Language for Machine Learning, AI, and Deep Learning

  Artificial Intelligence (AI), Machine Learning (ML), and Deep Learning (DL) have become some of the most transformative technologies of our time. From self-driving cars and recommendation systems to chatbots and healthcare diagnostics, these technologies are reshaping industries at an incredible pace. But have you ever wondered: Why do most AI researchers, data scientists, and developers prefer Python over other programming languages? In this blog, we’ll explore in depth why Python has emerged as the most popular and powerful language for AI, ML, and DL development . 1. Simplicity and Readability – Focus on Problem Solving, Not Syntax Complex mathematics and algorithms are some of the greatest challenges that newcomers in the world of AI/ML face. Python eases this load by providing a clean syntax that is easy to read. Let’s look at three concrete examples where Python’s clean syntax helps newcomers in AI/ML handle complex mathematics and algorithms more easily compared to ot...

All About Inheritance in OOP: Explained with Real-Life Examples

  The first thing that comes to mind when  you hear the word  inheritance,  is passing something down from parents to children . In Object-Oriented Programming, it works in a very similar way—it allows one class (child class) to inherit properties and behaviors from another class (parent class). This concept makes code reusable, organized, and easier to maintain . Let’s explore inheritance step by step with real-time analogies to make it super simple. What is Inheritance in OOP? In OOP, inheritance is a concept in which a class inherits the properties (variables) and behaviors (methods) of other classes. Common features are defined in the parent class . These features are extended by the child class which is capable of adding its own features.. It is best to consider it as a family tree : a child takes after a parent in terms of eye color or height but can also possess an individual characteristic. Why Use Inheritance? Code Reusability – No need t...

C++ code for Single Inheritance

Here’s a C++ program that shows how a Single Inheritance actually works with the help of Vehicle and Car class # include < iostream > using namespace std ; // Base class (Parent) class Vehicle { public : void start ( ) { cout < < " Vehicle started " < < endl ; } } ; // Derived class (Child) class Car : public Vehicle { public : void playMusic ( ) { cout < < " Music is playing " < < endl ; } } ; // Main function int main ( ) { Car myCar ; // Accessing parent class method myCar . start ( ) ; // Accessing child class method myCar . playMusic ( ) ; return 0 ; } Output:           Vehicle started           Music is playing Short Explanation: Vehicle is the base (parent) class . It has a method start() that prints "Vehicle started" . Car is the derived (child) ...