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

Вариант 14

Задание:

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

Complex x(1.3,4.2), y(4.0, 8.1), z(y); z.assign(plus(x,y)); print(plus(y,z));

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

//Complex.h

#pragma once

class Complex {

private:

double re, im;

public:

Complex();

Complex(const Complex&);

Complex(double, double);

Complex& assign(const Complex&);

friend void print(const Complex&) ;

friend Complex plus(const Complex&, 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::assign(const Complex& a) {

re = a.re;

im = a.im;

return *this;

}

void print(const Complex& a) {

cout << "re = " << a.re << "im = " << a.im;

}

Complex plus(const Complex& a, const Complex& b) {

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

}

//Array.h

#pragma once

#include "Complex.h"

#define DEFAULT_SIZE 10

class Array {

Complex* arr;

int size;

public:

~Array();

Array();

Array(int);

Complex& operator[](const int);

};

//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];

}

Вариант 15

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

Complex x(1.3,4.2), y(4.0, 8.1), z=y; cout<< x.plus(y,z); z=x.plus(y);

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

//Complex.h

#pragma once

#include <iostream>

using std::ostream;

class Complex {

private:

double re, im;

public:

Complex();

Complex(const Complex&);

Complex(double, double);

Complex plus(const Complex&) const;

Complex plus(const Complex&, const Complex&) const;

Complex& operator=(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::plus(const Complex& a) const {

return Complex(re + a.re, im + a.im);

}

Complex Complex::plus(const Complex& a, const Complex& b) const {

return plus(a.plus(b));

}

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 = DEFAULT_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;

}

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