
- •Задания задание 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
Вариант 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;
}