#17-Relational Operator Overloading

Write a program to compare the magnitude of a complex number by overloading the <,>,==,!= operator.

Its output will come as follows.

Explanation now comes as follows.

Our target of this program is to overload relational operators for comparing objects of money class.

So, we will divide money into paisa and rupees.

And, then we will take 2 inputs and compare those values(obviously its sum).

So if I input 100 rupees 20 paisa and again 100 rupees 20 paisa, then it will show “equal”.

Basically while overloading the relational operator, we will follow the syntax as shown in figure below.

C:\Users\Bijan\Desktop\main is main.png

Create a class named city, that will have 2 members variables, cityname(char[20]) and distfromA(float). Add member functions to set and retrieve the cityname and distfromA separately. Add operator overloading to find the distance between the cities(just find the difference of distfromA) and sum of idstance of those cites from A. In the main fxn, initialise three city objects. Set the first and second city to be B and C. Display the sum of distfromA of B and C and the distance between B and C.

note-: u can assume the necessary data 🙂

 

Its output comes as follows.

The working of program above is as follows. Firstly, A,B and C are displayed with the distances. Then, again it will do c1-c2/c1+c2 and finally displayed the subtraction and addition.

Q) Write a program that will add objects of a date class with members day,month, and year using operator overloading by overloading – operator.

The working of above program can be known by deeply looking inside the main function.

d1,d2,d3,d4 are the objects of the class. and we did d3=d1+d2 to add whereas, d4=d1-d2. These are the basic syntax in programming operator overloading that; if you remember; it will be better.

So, we have finished studying the relational operator overloading.

Next topic will be about the conversion of data types from one type to another.

Before you go-:

Subscribe to this blog(by bookmarking) we don’t send notifications to you.

Likewise you can join our email list.

Motivational-:

From sadhguru-:

You keep on working hard, there is no motivation.

Set a goal that you can’t reach, yes the one you can’t reach. Set big goals. And work hard, that you don’t waste a second of your day.

Allocate time for everything, if you are addicted to even studying, then after some time, you are going to lose your motivation to work hard.

So, have fun on one side, and in the other side, do hard work. For every 3 hours of hard work you take at least 1/2 hour break. That will help you keep stress free.

Be the first to comment

Leave a Reply

Your email address will not be published.


*