Simple Addition in C++ Binary Operator Overloading
On this page (6sections)
About this program
This is an example program in c operator overloading example programs. Read the concept first: Operators in C++, then study the code and output below.
Simple Addition in C++ Binary Operator Overloading Program
// Header Files
#include<iostream>
#include<conio.h>
//Standard namespace declaration
using namespace std;
class overloading {
int value;
public:
void setValue(int temp) {
value = temp;
}
overloading operator+(overloading ob) {
overloading t;
t.value = value + ob.value;
return (t);
}
void display() {
cout << value << endl;
}
};
//Main Functions
int main() {
overloading obj1, obj2, result;
int a, b;
cout << "Enter the value of Complex Numbers a,b:";
cin >> a>>b;
obj1.setValue(a);
obj2.setValue(b);
result = obj1 + obj2;
cout << "Input Values:\n";
obj1.display();
obj2.display();
cout << "Result:";
result.display();
getch();
return 0;
}
Sample Output
Enter the value of Complex Numbers a,b:10
5
Input Values:
10
5
Result:15
How It Works
This C++ program demonstrates Simple Addition in Binary Operator Overloading. It first reads the required values as input, then performs the core operation step by step, and finally prints the output shown in the Sample Output above.
- Declare the variables that hold the program’s data.
- Read the input values that the program will work with.
- Print the final result to the console so you can compare it with the sample output.
Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.
Related Pages
Learn the concept first, then study the code:
- C++ Programs — Browse all C++ Programs.
- Operators in C++ — Concept — operator overloading fundamentals.
Frequently Asked Questions
What does this C++ program do?
It is a C++ example program that demonstrates Simple Addition in Binary Operator Overloading, including the complete source code and the expected sample output.
How do I compile and run this C++ program?
Save the code in a `.cpp` file, compile it with `g++ filename.cpp -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses reading user input and user-defined functions, illustrating a common pattern in C++ programming.