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

Вариант 3

Задание

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

Point p1(10,20), p2(40,25), p3=p1; p3.assign(p2); (print(move(p2,40,20)); mul(p3,2);

Array a1(10), a2(10); (a1[0]).assign(p1); (a1[1]).assign(p2); a2.assign(a1); print(a2);

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

/*Point.h*/

#pragma once

#include <iostream>

using namespace std;

class Point {

protected:

int x;

int y;

public:

Point();

Point(const Point &t);

Point(int a, int b);

Point operator = (const Point &t);

Point assign(const Point &t);

friend Point move(Point&, const int& dx, const int& dy);

friend void mul(Point &t, const int& a);

friend void print(Point& p);

};

/*Array.h*/

#pragma once

#include "Point.h"

#include <iostream>

#include "Point.h"

using namespace std;

class Array

{

private:

Point* p;

int size = 0;

/*size can't be null*/

Array();

public:

Array(const int a);

Array(const Array& arr);

Point& Array::operator[](int index);

Array& assign(const Array& arr);

friend void print(Array& arr);

};

/*Point.cpp*/

#include "Point.h"

#include <iostream>

Point::Point(){

x = 0;

y = 0;

}

Point::Point(const Point &t){

x = t.x;

y = t.y;

}

Point::Point(int a, int b){

x = a;

y = b;

}

Point Point::operator=(const Point& q){

x = q.x;

y = q.y;

return *this;

}

void print(Point& p)

{

cout << "x=" << p.x << " y=" << p.y << endl;

}

void mul(Point& p, const int& a)

{

p.x *= a;

p.y *= a;

}

Point move(Point &p,const int& dx, const int& dy){

p.x += dx;

p.y += dy;

return p;

}

Point Point::assign(const Point &p){

x = p.x;

y = p.y;

return *this;

}

/*Array.cpp*/

#include "Point.h"

#include <iostream>

#include "Array.h"

Array::Array()

{

p = NULL;

size = 0;

}

Array::Array(const int size)

{

this->size = size;

p = new Point[size];

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

{

p[i] = Point();

}

}

Array::Array(const Array& arr)

{

p = arr.p;

size = arr.size;

}

Point& Array::operator[](int index)

{

if (index >= 0) return this->p[index];

return Point(-1, -1);

}

void print(Array& arr)

{

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

{

cout << "ArrayElem[" << i << "]:> ";

print(arr[i]);

cout<<endl;

}

}

Array& Array::assign(const Array& a) {

size = a.size;

delete[] p;

p = new Point[size];

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

p[i] = a.p[i];

return *this;

}

Вариант 4

Задание:

Класс

Элементы данных

Интерфейс

Complex

re, im

Конструкторы, операции –(унар) +,+=, =, <<(friend)

Array

pa, size

Конструкторы, функции(friend) assign, add, print

/*main.cpp*/

#include "Complex.h"

int _tmain(int argc, _TCHAR* argv[])

{

Complex a(5, 7);

Complex b = a;

Complex c = -a;

Complex d = a + c;

b += a;

Array arr(5);

arr[0] = b;

arr[1] = a;

assign(arr, c, 3);

add(arr, a, 0);

std::cout << "class Complex: " << endl;

std::cout << "a: " << a;

std::cout << "b: " << b;

std::cout << "c: " << c;

std::cout << "d: " << d;

std::cout << endl;

std::cout << "class Array: " << endl;

print(arr);

system("pause");

}

/*Complex.h*/

class Complex

{

private:

int re;

int im;

public:

Complex();

Complex(const int, const int);

//friend Complex operator -(const Complex &);

//friend Complex operator +(const Complex &, const Complex &);

Complex operator-() const;

Complex operator+(const Complex&) const;

Complex& operator =(const Complex&);

Complex& operator +=(const Complex &);

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

};

/*Complex.cpp*/

#include "Complex.h"

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;

}

Complex Complex::operator-() const {

return Complex(-re, -im);

}

Complex Complex::operator+(const Complex& c) const {

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

}

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

this->re += a.re;

this->im += a.im;

return *this;

}

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

this->re = a.re;

this->im = a.im;

return *this;

}

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

s << "Complex: " << 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:

Array();

Array(const int);

Complex & operator[](const int);

friend void assign(Array &, const Complex &, const int);

friend void add(Array &, const Complex &, const int);

friend void print(Array &);

};

/*Array.cpp*/

#include "Array.h"

#include <iostream>

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) {

if (index < 0 || index > this->size) {

std::cout << "Error";

exit(1);

}

else

return pa[index];

}

void assign(Array & arr, const Complex & a, const int index) {

if (index < 0 || index > arr.size) {

std::cout << "Error";

exit(1);

}

else

arr[index] = a;

}

void add(Array & arr, const Complex & a, const int index) {

if (index < 0 || index > arr.size) {

std::cout << "Error";

exit(1);

}

else

arr[index] += a;

}

void print(Array & arr) {

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

{

std::cout<<"array["<<i+1<<"]: "<<arr[i];

}

}

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