0% found this document useful (0 votes)
15 views5 pages

Demonstrate The Overloading of Unary Operators and Binary Operators of C++ 11.1 Overloading of Unary Operator

The document demonstrates overloading unary and binary operators in C++. It overloads the unary minus operator (-) to change the sign of distance objects represented by feet and inches. It also overloads the binary plus operator (+) to add two distance objects together by summing their feet and inches and returning a new distance object. The main function tests these operator overloads by applying negation to distance objects and adding two distances together.

Uploaded by

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

Demonstrate The Overloading of Unary Operators and Binary Operators of C++ 11.1 Overloading of Unary Operator

The document demonstrates overloading unary and binary operators in C++. It overloads the unary minus operator (-) to change the sign of distance objects represented by feet and inches. It also overloads the binary plus operator (+) to add two distance objects together by summing their feet and inches and returning a new distance object. The main function tests these operator overloads by applying negation to distance objects and adding two distances together.

Uploaded by

Rishav Jindal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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