复数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>
class Date {
private:
int d, m, y;
class Complex {
protected:
int real, im;
public:
Date() throw();
Complex();
Complex(int real, int im);
Complex(Complex &other);
Date(int, int, int);
~Date();
void out();
void operator=(Complex &other);
Complex& operator+(Complex &other);
void display() {
std::cout << this->real << "+" << this->im << "i" << std::endl;
}
};
Date::Date() throw() {
this->d = 0;
this->m = 0;
this->y = 0;
std::cout << "Running constructor" << std::endl;
Complex::Complex() {
this->real = 0;
this->im = 0;
}
Date::Date(int date, int month, int year) {
this->d = date;
this->m = month;
this->y = year;
Complex::Complex(int newReal, int newIm) {
this->real = newReal;
this->im = newIm;
}
void Date::out() {
std::cout << this->d << "/" << this->m << "/" << this->y << std::endl;
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() {
Date aDate;
Date bDate(3, 2, 4);
aDate.out();
bDate.out();
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;
}