- •Задания задание 1
- •Задание 2
- •Задание 3
- •3. Разработать классы Point и Array, позволяющие использовать их в следующей программе:
- •Задание 4
- •Задание 5
- •Задание 6
- •6. Разработать классы Point и Array, позволяющие использовать их в следующей программе:
- •Задание 7
- •Задание 8
- •Задание 9
- •Задание 10
- •Задание 11
- •Задание 12
- •Задание 13
- •17. Разработать классы Point и Array, позволяющие использовать их в следующей программе:
- •Задание 19
- •19. Разработать классы Point и Array, позволяющие использовать их в следующей программе:
- •Задание 20
- •Задание 21
- •Задание 22
- •22. Разработать классы Point и Array, позволяющие использовать их в следующей программе:
- •Задание 23
- •Задание 24
- •Задание 25
- •Задание 26
- •Задание 27
- •Задание 28
- •Задание 29
- •Задание 30
- •Решения Вариант 1
- •Вариант 2
- •Вариант 3
- •Вариант 4
- •Вариант 5
- •Вариант 7
- •Вариант 8
- •Вариант 9
- •Вариант 10
- •Вариант 11
- •Вариант 12
- •Вариант 13
- •Вариант 14
- •Вариант 15
- •Вариант 16
- •Вариант 6, 17, 19
- •Вариант 18
- •Вариант 20
- •Вариант 21
- •Вариант 22
- •Вариант 23
- •Вариант 24
- •Вариант 25
- •Вариант 26, 28
- •Вариант 27, 29.
- •Вариант 30
Вариант 20
Класс |
Элементы данных |
Интерфейс |
Complex |
re, im |
Конструкторы, операции +,+=(friend), =, << |
Array |
pa, size |
Конструкторы, функции assign, add, print, операции =,==, <, << |
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 Complex&) const;
bool operator!=(const Complex&) const;
friend Complex& operator+=(Complex&, 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 Complex& a) const {
return Complex(re + a.re, im + a.im);
}
Complex& operator+=(Complex &a, const Complex& b) {
a.re += b.re;
a.im += b.im;
return 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;
}
bool Complex::operator!=(const Complex& a) const {
return re != a.re || im != a.im;
}
Array.h
#pragma once
#include <iostream>
#include "Complex.h"
#define DEFAULT_SIZE 10
class Array {
Complex* pa;
int size = DEFAULT_SIZE;
public:
~Array();
Array();
Array(int);
Array& assign(const int, const Complex&);
Array& assign(const Array&);
void add(const Complex&);
void print() const;
Array& operator=(const Array&);
bool operator==(const Array&) const;
bool operator<(const Array&) const;
friend ostream& operator<<(ostream&, const Array&);
};
Array.cpp
#include <iostream>
#include "Array.h"
Array::~Array() {
delete[]pa;
pa = nullptr;
}
Array::Array(int s) {
if (s <= 0)
throw - 1;
size = s;
pa = new Complex[size];
}
Array::Array() {
size = DEFAULT_SIZE;
arr = new Complex[DEFAULT_SIZE];
}
Array& Array::assign(const Array& a) {
size = a.size;
delete[] pa;
pa = new Complex[size];
for (int i = 0; i < size; i++)
pa[i] = a.pa[i];
return *this;
}
Array& Array::assign(const int index, const Complex& a) {
if (index < 0 || index >= size)
throw - 1;
pa[index] = a;
return *this;
}
void Array::print() const {
for (int i = 0; i < size; i++)
std::cout << pa[i];
}
void Array::add(const Complex& a) {
Complex* temp = new Complex[size + 1];
for (int i = 0; i < size; i++)
temp[i] = pa[i];
temp[size] = a;
delete[] pa;
pa = temp;
size++;
}
Array& Array::operator=(const Array& a) {
size = a.size;
delete[] pa;
pa = new Complex[size];
for (int i = 0; i < size; i++)
pa[i] = a.pa[i];
return *this;
}
bool Array::operator==(const Array& a) const {
if (size != a.size)
return false;
for (int i = 0; i < size; i++) {
if (pa[i] != a.pa[i])
return false;
}
return true;
}
bool Array::operator<(const Array& a) const {
return size < a.size;
}
ostream& operator<<(ostream& s, const Array& a) {
a.print();
return s;
}
