- Реализовать класс
Complex
:
// complex.hpp | // main.cpp
|
class Complex { | #include <complex.hpp>
|
public: | int main() {
|
typedef double type; | Complex c1{};
| Complex c2{3};
Complex(); | Complex c3{1, 4};
|
Complex(type a); | cout << c1 << endl;
| cout << c2 << endl;
Complex(type a, type b); | cout << c3 << endl;
|
Complex(const Complex& c); | Complex c4{c3};
| Complex c5;
auto swap(Complex& c) -> void; | c5 = c4;
|
auto operator=(const Complex& c) -> Complex&; | cout << c4 << endl;
| cout << c5 << endl;
auto operator+(const Complex& c) -> Complex; |
| cout << c2 + c5 << endl;
auto operator-(const Complex& c) -> Complex; | cout << c2 - c5 << endl;
| cout << c2 * c5 << endl;
auto operator*(const Complex& c) -> Complex; | cout << c2 / c5 << endl;
|
auto operator/(const Complex& c) -> Complex; | cout << boolalpha;
| cout << (c2 != c5) << endl;
auto operator!() -> Complex; | cout << (c3 == c5) << endl;
|
auto operator==(const Complex& c) -> bool; | cout << static_cast<double>(c5) << endl;
| cout << c5[0] << "," << c5[1] << endl;
auto operator!=(const Complex& c) -> bool; |
| cin >> c1;
explicit operator type(); |
| ofstream output{ "complex.bin" };
auto operator[](size_t index) -> type; | output << c1;
|
friend | ifstream input{ "complex.bin" };
auto operator>>(istream&, Complex& c) -> istream&; | input >> c1;
|
friend | auto c6 = Complex::from_string("(1,2)");
auto operator<<(ostream&, const Complex& c) -> ostream&; | cout << c6 << endl;
|
static Complex from_string(const std::string& string); | c1.swap(c6);
| cout << c1 << endl;
~Complex(); |
| cout << !c1 << endl;
private: |
std::pair<type, type>* pair; | cout << c5[2] << endl;
}; | }
- Заполнить отчет
$ git clone https://github.com/<username>/complex
$ cd complex
$ g++ main.cpp complex.cpp -I. -std=c++11 -o complex-example
$ ./complex-example
...
- Класс
Complex
сделать шаблонным - Пометить все необходимые методы квалификатором
const
- Использовать списки инициализации, как для полей, так и для конструкторов
- Избежать дублирование кода