0% found this document useful (0 votes)
0 views3 pages

C++ Operator Overloadinf U B

The document contains C++ code demonstrating operator overloading for both binary and unary operators. The first part defines a class 'Arith_num' that overloads the '+' operator to add two numbers, while the second part defines a class 'Distance' that overloads the '-' operator to negate the distance values. The provided code includes examples of input and output for both functionalities.

Uploaded by

Vinod Pande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

C++ Operator Overloadinf U B

The document contains C++ code demonstrating operator overloading for both binary and unary operators. The first part defines a class 'Arith_num' that overloads the '+' operator to add two numbers, while the second part defines a class 'Distance' that overloads the '-' operator to negate the distance values. The provided code includes examples of input and output for both functionalities.

Uploaded by

Vinod Pande
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

/* use binary (+) operator to perform the addition of two numbers.

*/
#include <iostream>
using namespace std;
class Arith_num
{
// declare data member or variable
int x, y;
public:
// create a member function to take input
void input()
{
cout << " Enter the first number: ";
cin >> x;
}
void input2()
{
cout << " Enter the second number: ";
cin >> y;
}
// overloading the binary '+' operator to add number
Arith_num operator + (Arith_num &ob)
{
// create an object
Arith_num A;
// assign values to object
A.x = x + ob.x;
return (A);
}
// display the result of binary + operator
void print()
{
cout << "The sum of two numbers is: " <<x;
}
};
int main ()
{
Arith_num x1, y1, res; // here we create object of the class Arith_num i.e x1 and y1
// accepting the values
x1.input();
y1.input();
// assign result of x1 and x2 to res
res = x1 + y1;
// call the print() function to display the results
res.print();
return 0;
}

Output

Enter the first number: 5


Enter the second number: 6
The sum of two numbers is: 11

Unary Operator Overloading

#include <iostream>
using namespace std;

class Distance {
private:
int feet; // 0 to infinite
int inches; // 0 to 12

public:
// required constructors
Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}

// method to display distance


void displayDistance() {
cout << "F: " << feet << " I:" << inches <<endl;
}

// overloaded minus (-) operator


Distance operator- () {
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
};

int main() {
Distance D1(11, 10), D2(-5, 11);

-D1; // apply negation


D1.displayDistance(); // display D1

-D2; // apply negation


D2.displayDistance(); // display D2

return 0;
}

When the above code is compiled and executed, it produces the following result −
F: -11 I:-10
F: 5 I:-11

You might also like