0% found this document useful (0 votes)
2 views2 pages

Add Two Distance

The document contains a C++ program that demonstrates binary operator overloading to add two Distance objects. It defines a Distance class with feet and inch attributes, and an overloaded '+' operator to sum the distances. The main function creates two Distance instances, adds them, and outputs the total feet and inches.

Uploaded by

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

Add Two Distance

The document contains a C++ program that demonstrates binary operator overloading to add two Distance objects. It defines a Distance class with feet and inch attributes, and an overloaded '+' operator to sum the distances. The main function creates two Distance instances, adds them, and outputs the total feet and inches.

Uploaded by

Suman Chatterjee
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

C++ program to show binary operator overloading to add two distances:

#include <iostream>

using namespace std;

class Distance {
public:

int feet, inch;

Distance()
{
feet = 0;
inch = 0;
}

Distance(int f, int i)
{
feet = f;
inch = i;
}

Distance operator+(Distance& d2)


{

Distance d3;

[Link] = feet + [Link];


[Link] = >inch + [Link];

return d3;
}
};

int main()
{

Distance d1(8, 9);

Distance d2(10, 2);

Distance d3;

d3 = d1 + d2;
cout << "\nTotal Feet & Inches: " << [Link] << "'" << [Link];
return 0;
}

You might also like