Monday 14 February 2022

operator overloading

Function To Add Complex number using operator overloading

Program:


// Online C++ compiler to run C++ program online

#include <bits/stdc++.h>

using namespace std;

class Complex {

  private:

    int a, b;

  public:

    void setcomplex(int x, int y)

  {

    a = x;

    b = y;

  }

  Complex operator + (Complex p1)

  {

    Complex tmp;

    tmp.a = p1.a + a;

    tmp.b = p1.b + b;

    return tmp;

  }

  void showValue() {

    cout << a << " " << b;

  }

};

int main() {

  Complex el;

  el.setcomplex(2, 4);

  Complex pq;

  pq.setcomplex(2, 5);

  Complex ans = el + pq;

  ans.showValue();

  return 0;

}

No comments:

Post a Comment

The Future of Web Development: Why Next.js is Going Viral

  Are you ready to level up your web development game? Look no further than Next.js, the latest sensation in the world of web development th...