C++ program to overload '-=' operator

//C++ program to overload '-=' operator as A-=B
//where A and B are objects of class.

#include <iostream>
using namespace std;
class Example
{
    private:
        int m,cm;
    public:
        Example(int meter,int centimeter)
        {
            m=meter;
            cm=centimeter;
        }
        void display()
        {
            cout<<m<<"m "<<cm<<"cm";
        }
        void operator-=(Example e)
        {
            m=m-e.m;
            cm=cm-e.cm;
        }
};
int main()
{
    Example a(2,90),b(3,50);
    a-=b;
//same as a.operator-=(b);
    a.display();
    return 0;
}

No comments:

Post a Comment