Files
BasicsOfComputerSoftwareEng…/OOP/test.cpp
2023-04-04 13:32:45 +08:00

52 lines
967 B
C++

#include <iostream>
class Complex {
protected:
int real, im;
public:
Complex();
Complex(int real, int im);
Complex(Complex &other);
void operator=(Complex &other);
Complex& operator+(Complex &other);
void display() {
std::cout << this->real << "+" << this->im << "i" << std::endl;
}
};
Complex::Complex() {
this->real = 0;
this->im = 0;
}
Complex::Complex(int newReal, int newIm) {
this->real = newReal;
this->im = newIm;
}
Complex::Complex(Complex &other) {
this->real = other.real;
this->im = other.im;
}
void Complex::operator=(Complex &other) {
this->real = other.real;
this->im = other.im;
}
Complex& Complex::operator+(Complex &other) {
Complex tmp(this->real + other.real, this->im + other.im);
return tmp;
}
int main() {
Complex i1(1,2);
Complex i2;
int a[3][5] = {0};
int (*ptr)[5] = (int(*)[5]) a[3];
i2 = i1 + i1;
i2.display();
return 0;
}