Untitled

 avatar
unknown
plain_text
4 months ago
1.1 kB
65
Indexable
#include<iostream>
using namespace std;

class arithmetic {
    int a, b;
public:
    void getdata(int p, int q) {
        a = p;
        b = q;
    }
    void display() {
        cout << "\na=" << a << "\nb=" << b;
    }
    arithmetic operator+(arithmetic a1) {
        a1.a = a1.a + a;
        a1.b = a1.b + b; 
        return a1;
    }
    
    friend arithmetic operator-(arithmetic a1, arithmetic a2); 
};

arithmetic operator-(arithmetic a1, arithmetic a2) {
    a1.a = a1.a - a2.a; 
    a1.b = a1.b - a2.b;
    return a1;
}

int main() {
    arithmetic a1, a2, a3, a4;
    int f, g, h, i;
    cout << "\n Enter 2 numbers\n";
    cin >> f >> g;
    a1.getdata(f, g);
    a1.display();
    cout << "\n Enter 2 numbers\n";
    cin >> h >> i;
    a2.getdata(h, i);
    a2.display();
    a3 = a1 + a2; // Using overloaded + operator
    cout << "\nResult of addition:";
    a3.display();
    a4 = a2 - a1; // Using overloaded - operator
    cout << "\nResult of subtraction:";
    a4.display();
    return 0;
}
Editor is loading...
Leave a Comment