复数test

This commit is contained in:
unlockable
2023-04-04 13:32:45 +08:00
parent 4581b32521
commit b82efce6f4

View File

@@ -1,40 +1,52 @@
#include <iostream> #include <iostream>
class Date { class Complex {
private: protected:
int d, m, y; int real, im;
public: public:
Date() throw(); Complex();
Complex(int real, int im);
Complex(Complex &other);
Date(int, int, int); void operator=(Complex &other);
Complex& operator+(Complex &other);
~Date(); void display() {
std::cout << this->real << "+" << this->im << "i" << std::endl;
void out(); }
}; };
Date::Date() throw() { Complex::Complex() {
this->d = 0; this->real = 0;
this->m = 0; this->im = 0;
this->y = 0;
std::cout << "Running constructor" << std::endl;
} }
Date::Date(int date, int month, int year) { Complex::Complex(int newReal, int newIm) {
this->d = date; this->real = newReal;
this->m = month; this->im = newIm;
this->y = year;
} }
void Date::out() { Complex::Complex(Complex &other) {
std::cout << this->d << "/" << this->m << "/" << this->y << std::endl; 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() { int main() {
Date aDate; Complex i1(1,2);
Date bDate(3, 2, 4); Complex i2;
aDate.out(); int a[3][5] = {0};
bDate.out(); int (*ptr)[5] = (int(*)[5]) a[3];
i2 = i1 + i1;
i2.display();
return 0; return 0;
} }