Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ТП Задачи.docx
Скачиваний:
3
Добавлен:
01.07.2025
Размер:
221.76 Кб
Скачать

Вариант 30

Разработать классы complex и Array, позволяющие использовать их в следующей программе:

complex x, y(4.0, 8.1), z(5.2, 6.0); x=2*y; y= -(z);

Array a1(10); a1[0]=y; a1[1]=z; cout<<x<<a1;

Написать тексты h-файлов и cpp-файлов для классов complex и Array. Нарисовать диаграммы классов.

Complex.h

#pragma once

#include <iostream>

using std::ostream;

class Complex {

private:

double re, im;

public:

Complex();

Complex(const Complex&);

Complex(double, double);

Complex& operator=(const Complex&);

Complex operator-() const;

friend Complex operator*(const double, const Complex&);

friend ostream& operator<<(ostream&, const Complex&);

};

Complex.cpp

#include <iostream>

#include "Complex.h"

using std::cout;

Complex::Complex(double a, double b) {

re = a;

im = b;

}

Complex::Complex() : Complex(0, 0) {

}

Complex::Complex(const Complex& a) : Complex(a.re, a.im) {

}

Complex Complex::operator-() const {

return Complex(-re, -im);

}

Complex operator*(const double a, const Complex& b) {

return Complex(b.re * a, b.im * a);

}

Complex& Complex::operator=(const Complex &a) {

re = a.re;

im = a.im;

return *this;

}

ostream& operator<<(ostream& s, const Complex& a) {

s << "Re = " << a.re << " Im = " << a.im << std::endl;

return s;

}

Array.h

#pragma once

#include <iostream>

#include "Complex.h"

#define DEFAULT_SIZE 10

class Array {

Complex* arr;

int size;

public:

~Array();

Array();

Array(int);

Complex& operator[](const int);

friend ostream& operator<<(ostream&, const Array&);

};

Array.cpp

#include <iostream>

#include "Array.h"

Array::~Array() {

delete[]arr;

arr = nullptr;

}

Array::Array(int s) {

if (s <= 0)

throw - 1;

size = s;

arr = new Complex[size];

}

Array::Array() {

size = DEFAULT_SIZE;

arr = new Complex[DEFAULT_SIZE];

}

Complex& Array::operator[](const int i) {

if (i < 0 || i >= size)

throw - 1;

else

return arr[i];

}

ostream& operator<<(ostream& s, const Array& a) {

for (int i = 0; i < a.size; i++)

s << a.arr[i];

s << std::endl;

return s;

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]