Files
ooplab/lab3/8.cpp
Amneesh Singh ad185c994e labs 1-6: init
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
2022-12-24 14:03:08 +05:30

46 lines
673 B
C++

#include <iostream>
using namespace std;
class Complex {
private:
int r, i;
public:
Complex() = default;
Complex(int);
Complex(int, int);
Complex operator+(Complex);
void display();
};
Complex::Complex(int a) { this->r = this->i = a; }
Complex::Complex(int r, int i) {
this->r = r;
this->i = i;
}
Complex Complex::operator+(Complex op) {
return Complex(this->r + op.r, this->i + op.i);
}
void Complex::display() {
if (this->r)
cout << this->r;
if (this-> r && this->i > 0)
cout << '+';
if (this->i)
cout << this->i << 'i';
cout << endl;
}
int main() {
Complex c;
Complex a(4), b(34, -2124);
c = a + b;
c.display();
}