
- •Задания задание 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
Вариант 2
Задание
Написать тексты h-файлов и cpp-файлов для классов Complex и Array (массив комплексных чисел). Описание классов:
Класс |
Элементы данных |
Интерфейс |
Complex |
re, im |
Конструкторы, функции assign, add, plus, операция << |
Array |
pa, size |
Конструкторы, операции +,+=(friend), =, [], << |
Привести примеры создания и использования объектов классов Complex и Array. Нарисовать диаграммы классов.
/*main.cpp*/
#include "Array.h"
int _tmain(int argc, _TCHAR* argv[])
{
Complex a(1, 1);
Complex b(2, -2);
Complex c(0, 0);
Complex d;
Complex e;
c.add(b);
d.assign(a);
e = a.plus(c);
Array arr(2);
Array arr1(2);
arr[0] = a;
arr[1] = b;
arr1[0] = c;
arr1[1] = d;
arr += arr1;
Array arr2 = arr + arr1;
std::cout << "Class Complex: " << endl;
std::cout << "a: " << a;
std::cout << "b: " << b;
std::cout << "c: " << c;
std::cout << "d: " << d;
std::cout << "e: " << e;
std::cout << "\nClass Array: " << endl;
std::cout << arr << endl;
std::cout << arr1 << endl;
std::cout << arr2 << endl;
system("pause");
return 0;
}
/*Complex.h*/
#include <iostream>
using namespace std;
class Complex
{
private:
int re;
int im;
public:
Complex();
Complex(const int, const int);
void assign(const Complex &);
void add(const Complex &);
Complex plus(const Complex &) const;
friend ostream& operator <<(ostream& , const Complex &);
};
/*Complex.h*/
#include "Complex.h"
#include <iostream>
using namespace std;
Complex::Complex() {
this->re = 0;
this->im = 0;
}
Complex::Complex(const int re, const int im) {
this->re = re;
this->im = im;
}
void Complex::assign(const Complex & a) {
this->re = a.re;
this->im = a.im;
}
void Complex::add(const Complex & a) {
this->re += a.re;
this->im += a.im;
}
Complex Complex::plus(const Complex & b) const {
return Complex(this->re + b.re, this->im + b.im);
}
ostream & operator<<(ostream & s, const Complex & a) {
s << a.re;
if (a.im >= 0)
s << "+" << a.im << "j";
else
s << a.im << "j";
s << endl;
return s;
}
/*Array.h*/
#include "Complex.h"
class Array : Complex
{
private:
Complex* pa;
int size;
public:
int getSize() {
return size;
}
Array();
Array(const int);
Complex & operator[](const int) const;
Array operator +(const Array &) const;
friend Array & operator +=(Array &, const Array &);
void operator =(Array &);
friend ostream& operator <<(ostream &, const Array &);
};
/*Array.cpp*/
#include "Array.h"
Array::Array() {
pa = NULL;
size = 0;
}
Array::Array(const int size) {
this->size = size;
if (this->size > 0)
pa = new Complex[this->size];
for (int i = 0; i < this->size; i++)
pa[i] = Complex();
}
Complex & Array::operator[](const int index) const {
if (index < 0 || index > this->size) {
std::cout << "Error";
exit(1);
}
else
return pa[index];
}
Array Array::operator+(const Array & a) const {
int newSize = this->size + a.size;
Array newArr(newSize);
for (int i = 0; i < newSize; i++)
newArr.pa[i] = i < size ? pa[i] : a.pa[i - size];
return newArr;
}
Array & operator +=(Array & a, const Array & b) {
int newSizeTempArr = a.size + b.size;
Complex *tempArr = new Complex[newSizeTempArr];
for (int i = 0; i < newSizeTempArr; i++)
tempArr[i] = (i < a.size) ? a[i] : b[i - b.size];
delete[] a.pa;
a.size = newSizeTempArr;
a.pa = new Complex[a.size];
for (int i = 0; i < a.size; i++)
a[i] = tempArr[i];
delete[] tempArr;
return a;
}
void Array::operator=(Array & a) {
this->pa = a.pa;
}
ostream & operator<<(ostream & s, const Array & arr) {
for (int i = 0; i < arr.size; i++)
{
s << "array[" << i << "]" << arr[i];
}
return s;
}