You are on page 1of 5

11.

Demonstrate the overloading of unary operators and binary


operators of c++
11.1 Overloading of unary operator
#include <iostream>

using namespace std;

class Distance {

private:

int feet;

int inches;

public:

// 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(30, 12), D2(-5, 2000 );

-D1; // applying negation

D1.displayDistance();

-D2;

D2.displayDistance();

return 0;

11.2 Binary operator overloading


ANS:-
#include <iostream>

using namespace std;

class Distance {

public:

int feet, inch;

Distance()

this->feet = 0;

this->inch = 0;

Distance(int f, int i)

this->feet = f;

this->inch = i;

// Overloading (+) operator to perform addition of

// two distance object

Distance operator+(Distance& d2) // Call by reference

// Create an object to return

Distance d3;
// Perform addition of feet and inches

d3.feet = this->feet + d2.feet;

d3.inch = this->inch + d2.inch;

// Return the resulting object

return d3;

};

int main()

// Declaring and Initializing first object

Distance d1(6, 5);

// Declaring and Initializing second object

Distance d2(7, 2);

// Declaring third object

Distance d3;

// Use overloaded operator

d3 = d1 + d2;

// Display the result

cout << "\nTotal Feet & Inches: " << d3.feet << "'" << d3.inch;

return 0;

You might also like