C++ Programs Based on Operator Overloading with Output

Operator overloading is an important feature of C++ that allows programmers to define the behavior of existing operators for user-defined data types. In this article, we will learn eight practical C++ programs based on operator overloading, including unary operators, binary operators, relational operators, and stream insertion and extraction operators.

Operator Overloading C++ का एक महत्वपूर्ण feature है, जिसकी सहायता से programmers user-defined data types के लिए existing operators का behavior define कर सकते हैं। इस article में हम Operator Overloading पर आधारित आठ practical C++ programs सीखेंगे, जिनमें unary operators, binary operators, relational operators तथा stream insertion और extraction operators शामिल हैं।

Program 1: C++ Program to Overload Unary Minus (-) Operator

The unary minus (-) operator is used to change the sign of a value. In this program, we overload the unary minus operator to change the signs of the data members of an object.

Unary Minus (-) operator का उपयोग किसी value का sign बदलने के लिए किया जाता है। इस program में हम unary minus operator को overload करके object के data members के signs बदलेंगे।

Source Code

#include <iostream>
using namespace std;
class Number {
private:
int x, y;
public:
Number(int a, int b) {
x = a;
y = b;
}
void operator-() {
x = -x;
y = -y;
}
void display() const {
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
};
int main() {
Number n(10, 20);
cout << "Before Overloading Unary Minus:" << endl;
n.display();
-n;
cout << "\nAfter Overloading Unary Minus:" << endl;
n.display();
return 0;
}

Output

Before Overloading Unary Minus:
x = 10
y = 20
After Overloading Unary Minus:
x = -10
y = -20

Explanation

The operator-() function is defined inside the class to overload the unary minus operator. When -n is executed, the signs of both data members are changed.

Class के अंदर operator-() function को define करके unary minus operator overload किया गया है। जब -n execute होता है, तो दोनों data members के signs बदल जाते हैं।

Program 2: C++ Program to Overload Increment (++) Operator

The increment (++) operator increases the value of a variable by one. In C++, the increment operator has two forms: prefix increment and postfix increment.

Increment (++) operator किसी variable की value को एक से बढ़ाता है। C++ में increment operator के दो forms होते हैं: Prefix Increment और Postfix Increment।

Source Code

#include <iostream>
using namespace std;
class Number {
private:
int value;
public:
Number(int v) {
value = v;
}
// Prefix increment
Number& operator++() {
++value;
return *this;
}
// Postfix increment
Number operator++(int) {
Number temp = *this;
value++;
return temp;
}
void display() const {
cout << "Value = " << value << endl;
}
};
int main() {
Number n(10);
cout << "Original Value:" << endl;
n.display();
++n;
cout << "\nAfter Prefix Increment:" << endl;
n.display();
n++;
cout << "\nAfter Postfix Increment:" << endl;
n.display();
return 0;
}

Output

Original Value:
Value = 10
After Prefix Increment:
Value = 11
After Postfix Increment:
Value = 12

Explanation

The prefix increment operator is overloaded using operator++() without any parameter. The postfix increment operator is overloaded using operator++(int), where the dummy int parameter distinguishes it from the prefix version.

Prefix increment operator को बिना parameter वाले operator++() function से overload किया जाता है। Postfix increment operator को operator++(int) से overload किया जाता है, जिसमें dummy int parameter इसे prefix version से अलग करता है।

In prefix increment, the value is increased first and then the updated object is returned by reference. In postfix increment, the original value is saved, the object is increased, and the old value is returned.

Prefix Increment में पहले value बढ़ाई जाती है और फिर updated object reference के रूप में return होता है। Postfix Increment में पहले original value save की जाती है, फिर object की value बढ़ाई जाती है और पुरानी value return की जाती है।

Program 3: C++ Program to Overload Decrement (--) Operator

The decrement (--) operator decreases the value of a variable by one. Like increment, it also supports prefix and postfix forms.

Decrement (--) operator किसी variable की value को एक से कम करता है। Increment operator की तरह इसके भी Prefix और Postfix forms होते हैं।

Source Code

#include <iostream>
using namespace std;
class Number {
private:
int value;
public:
Number(int v) {
value = v;
}
// Prefix decrement
Number& operator--() {
--value;
return *this;
}
// Postfix decrement
Number operator--(int) {
Number temp = *this;
value--;
return temp;
}
void display() const {
cout << "Value = " << value << endl;
}
};
int main() {
Number n(10);
cout << "Original Value:" << endl;
n.display();
--n;
cout << "\nAfter Prefix Decrement:" << endl;
n.display();
n--;
cout << "\nAfter Postfix Decrement:" << endl;
n.display();
return 0;
}

Output

Original Value:
Value = 10
After Prefix Decrement:
Value = 9
After Postfix Decrement:
Value = 8

Explanation

The operator--() function overloads the prefix decrement operator, whereas operator--(int) overloads the postfix decrement operator. Both decrease the object's value by one.

operator--() function Prefix Decrement operator को overload करता है, जबकि operator--(int) Postfix Decrement operator को overload करता है। दोनों functions object की value को एक से कम करते हैं।

Program 4: C++ Program to Overload Binary Plus (+) Operator

The binary plus (+) operator is used to add two values. We can overload it to add the data members of two objects.

Binary Plus (+) operator का उपयोग दो values को जोड़ने के लिए किया जाता है। हम इसे overload करके दो objects के data members को जोड़ सकते हैं।

Source Code

#include <iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
Complex operator+(const Complex& c) const {
Complex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
void display() const {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(10, 20);
Complex c2(30, 40);
Complex c3 = c1 + c2;
cout << "First Complex Number: ";
c1.display();
cout << "Second Complex Number: ";
c2.display();
cout << "Sum of Complex Numbers: ";
c3.display();
return 0;
}

Output

First Complex Number: 10 + 20i
Second Complex Number: 30 + 40i
Sum of Complex Numbers: 40 + 60i

Explanation

The operator+() function is overloaded to add two complex number objects. It adds their real parts and imaginary parts separately and returns a new object containing the result.

दो complex number objects को जोड़ने के लिए operator+() function को overload किया गया है। यह दोनों के real parts और imaginary parts को अलग-अलग जोड़कर result वाला नया object return करता है।

Program 5: C++ Program to Overload Binary Minus (-) Operator

The binary minus (-) operator is used to subtract one value from another. In this program, we overload it to subtract two complex numbers.

Binary Minus (-) operator का उपयोग एक value में से दूसरी value घटाने के लिए किया जाता है। इस program में हम इसे overload करके दो complex numbers को subtract करेंगे।

Source Code

#include <iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
Complex operator-(const Complex& c) const {
Complex temp;
temp.real = real - c.real;
temp.imag = imag - c.imag;
return temp;
}
void display() const {
cout << real;
if (imag >= 0)
cout << " + " << imag << "i";
else
cout << " - " << -imag << "i";
cout << endl;
}
};
int main() {
Complex c1(50, 40);
Complex c2(20, 10);
Complex c3 = c1 - c2;
cout << "First Complex Number: ";
c1.display();
cout << "Second Complex Number: ";
c2.display();
cout << "Difference of Complex Numbers: ";
c3.display();
return 0;
}

Output

First Complex Number: 50 + 40i
Second Complex Number: 20 + 10i
Difference of Complex Numbers: 30 + 30i

Explanation

The operator-() function subtracts the real part of the second object from the real part of the first object. It performs the same operation on the imaginary parts and returns a new object.

operator-() function दूसरे object के real part को पहले object के real part में से subtract करता है। इसी प्रकार imaginary parts को भी subtract करके एक नया object return करता है।

Program 6: C++ Program to Overload Multiplication (*) Operator

The multiplication (*) operator is used to multiply two values. In this program, we overload it to multiply two complex numbers.

Multiplication (*) operator का उपयोग दो values को multiply करने के लिए किया जाता है। इस program में हम इसे overload करके दो complex numbers का multiplication करेंगे।

For two complex numbers:

दो complex numbers के लिए:

[
(a+bi)(c+di)=(ac-bd)+(ad+bc)i
]

Source Code

#include <iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) {
real = r;
imag = i;
}
Complex operator*(const Complex& c) const {
Complex temp;
temp.real = real * c.real - imag * c.imag;
temp.imag = real * c.imag + imag * c.real;
return temp;
}
void display() const {
cout << real;
if (imag >= 0)
cout << " + " << imag << "i";
else
cout << " - " << -imag << "i";
cout << endl;
}
};
int main() {
Complex c1(2, 3);
Complex c2(4, 5);
Complex c3 = c1 * c2;
cout << "First Complex Number: ";
c1.display();
cout << "Second Complex Number: ";
c2.display();
cout << "Product of Complex Numbers: ";
c3.display();
return 0;
}

Output

First Complex Number: 2 + 3i
Second Complex Number: 4 + 5i
Product of Complex Numbers: -7 + 22i

Explanation

The operator*() function overloads the multiplication operator. It applies the complex multiplication formula to calculate the real and imaginary parts of the result.

operator*() function multiplication operator को overload करता है। यह complex multiplication formula का उपयोग करके result के real और imaginary parts calculate करता है।

For example:

उदाहरण के लिए:

[
(2+3i)(4+5i)
]

[
=(8-15)+(10+12)i
]

[
=-7+22i
]

Program 7: C++ Program to Overload Relational Operators

Relational operators are used to compare two values. In C++, we can overload operators such as ==, !=, <, >, <=, and >= to compare objects.

Relational operators का उपयोग दो values की तुलना करने के लिए किया जाता है। C++ में हम ==, !=, <, >, <= और >= जैसे operators को overload करके objects की तुलना कर सकते हैं।

Source Code

#include <iostream>
using namespace std;
class Number {
private:
int value;
public:
Number(int v) {
value = v;
}
bool operator==(const Number& n) const {
return value == n.value;
}
bool operator!=(const Number& n) const {
return value != n.value;
}
bool operator<(const Number& n) const {
return value < n.value;
}
bool operator>(const Number& n) const {
return value > n.value;
}
bool operator<=(const Number& n) const {
return value <= n.value;
}
bool operator>=(const Number& n) const {
return value >= n.value;
}
};
int main() {
Number n1(10);
Number n2(20);
Number n3(10);
cout << boolalpha;
cout << "n1 == n3: " << (n1 == n3) << endl;
cout << "n1 != n2: " << (n1 != n2) << endl;
cout << "n1 < n2: " << (n1 < n2) << endl;
cout << "n2 > n1: " << (n2 > n1) << endl;
cout << "n1 <= n3: " << (n1 <= n3) << endl;
cout << "n2 >= n1: " << (n2 >= n1) << endl;
return 0;
}

Output

n1 == n3: true
n1 != n2: true
n1 < n2: true
n2 > n1: true
n1 <= n3: true
n2 >= n1: true

Explanation

Six relational operator functions are defined inside the Number class. Each function compares the values of two objects and returns a Boolean result.

Number class के अंदर छह relational operator functions define किए गए हैं। प्रत्येक function दो objects की values compare करके Boolean result return करता है।

The boolalpha manipulator displays the Boolean results as true or false instead of 1 or 0.

boolalpha manipulator Boolean results को 1 या 0 के स्थान पर true या false के रूप में display करता है।

Program 8: C++ Program to Overload Insertion (<<) and Extraction (>>) Operators

The stream insertion (<<) operator is used to display object data using cout, while the stream extraction (>>) operator is used to accept object data using cin.

Stream Insertion (<<) operator का उपयोग cout की सहायता से object का data display करने के लिए किया जाता है, जबकि Stream Extraction (>>) operator का उपयोग cin की सहायता से object का data input करने के लिए किया जाता है।

Source Code

#include <iostream>
using namespace std;
class Student {
private:
int rollNo;
float marks;
public:
friend istream& operator>>(istream& in, Student& s);
friend ostream& operator<<(ostream& out,
const Student& s);
};
istream& operator>>(istream& in, Student& s) {
cout << "Enter Roll Number: ";
in >> s.rollNo;
cout << "Enter Marks: ";
in >> s.marks;
return in;
}
ostream& operator<<(ostream& out, const Student& s) {
out << "Roll Number: " << s.rollNo << endl;
out << "Marks: " << s.marks << endl;
return out;
}
int main() {
Student s1;
cin >> s1;
cout << "\nStudent Details:" << endl;
cout << s1;
return 0;
}

Output

Enter Roll Number: 101
Enter Marks: 85.5
Student Details:
Roll Number: 101
Marks: 85.5

Explanation

The operator>> function is overloaded to accept student details through cin. The operator<< function is overloaded to display student details through cout.

operator>> function को cin के माध्यम से student details input करने के लिए overload किया गया है। operator<< function को cout के माध्यम से student details display करने के लिए overload किया गया है।

Both functions are declared as friend functions because they need access to the private data members of the Student class. They return stream references to support chaining.

दोनों functions को friend functions के रूप में declare किया गया है क्योंकि इन्हें Student class के private data members को access करना होता है। ये chaining को support करने के लिए stream references return करते हैं।

Frequently Asked Questions (FAQs)

1. What is operator overloading in C++?

Operator overloading allows existing operators to work with user-defined data types by defining their behavior inside or outside a class.

Operator Overloading की सहायता से existing operators को user-defined data types के साथ कार्य करने योग्य बनाया जाता है।

2. Which unary operators are commonly overloaded in C++?

Unary minus (-), increment (++), and decrement (--) are common examples of unary operators that can be overloaded.

Unary Minus (-), Increment (++) और Decrement (--) C++ में overload किए जाने वाले सामान्य unary operators हैं।

3. What is the difference between prefix and postfix increment?

Prefix increment increases the value before the expression is evaluated, whereas postfix increment uses the original value before increasing it.

Prefix Increment में expression evaluate होने से पहले value बढ़ती है, जबकि Postfix Increment में पहले original value उपयोग होती है और उसके बाद value बढ़ती है।

4. Why is a dummy int parameter used in postfix operator overloading?

The dummy int parameter distinguishes the postfix operator function from the prefix operator function. It is not used to pass the increment value.

Dummy int parameter का उपयोग Postfix operator function को Prefix operator function से अलग पहचानने के लिए किया जाता है। इसका उपयोग increment value pass करने के लिए नहीं होता है।

5. Which functions are generally used to overload stream insertion and extraction operators?

They are generally overloaded using non-member functions, often declared as friend functions inside the class.

इन्हें सामान्यतः non-member functions के रूप में overload किया जाता है, जिन्हें अक्सर class के अंदर friend functions के रूप में declare किया जाता है।

6. What is the return type of the overloaded relational operators?

Relational operator functions generally return bool because their result is either true or false.

Relational operator functions सामान्यतः bool return करते हैं क्योंकि इनका result या तो true होता है या false।

These eight practical programs demonstrate the use of operator overloading in C++. They cover unary minus, increment, decrement, binary addition, subtraction, multiplication, relational operators, and stream insertion and extraction operators. Practising these programs will help students understand how operators can be customized for user-defined classes.

ये आठ practical programs C++ में Operator Overloading के उपयोग को demonstrate करते हैं। इनमें Unary Minus, Increment, Decrement, Binary Addition, Subtraction, Multiplication, Relational Operators तथा Stream Insertion और Extraction Operators शामिल हैं। इन programs का अभ्यास करने से students समझ सकेंगे कि user-defined classes के लिए operators को किस प्रकार customize किया जा सकता है।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

Complete C++ Programming Course - OOP and Modern C++

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)