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

Вариант 5

Задание

Написать тексты h-файлов и cpp-файлов для классов Complex и Array (массив комплексных чисел). Описание классов:

Класс

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

Интерфейс

Complex

re, im

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

Array

pa, size

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

Привести примеры создания и использования объектов классов Complex и Array. Нарисовать диаграммы классов.

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&);

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

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 operator+(const Complex& a, const Complex& b) {

return Complex(a.re + b.re, a.im + b.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;

}

Array.h

#pragma once

#include <iostream>

#include "Complex.h"

#define DEFAULT_SIZE 10

class Array {

Complex* pa;

int size;

public:

~Array();

Array();

Array(int);

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

Array& assign(const Array&);

void add(const Complex&);

void print() const;

};

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++;

}

Вариант 7

Класс

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

Интерфейс

Point

x, y

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

Rect

p (типа Point), dx, dy

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

#include <iostream>

#include "Point.h"

#include "Rect.h"

using namespace std;

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

Point a(4,6);

Point b;

Point c(a), d(11, 13);

b = a;

print(b);

assign(b, d);

print(b);

move(b, 1, 4);

print(b);

Rect r1(a, 5, 7);

Rect r2(b, 6, 8), r3;

cout << endl << r1;

cout << -r1 << endl;

cout << r1+r2 << endl;

r3 = r1;

cout << r3;

system("pause");

return 0;

}

// Point.h

#include <iostream>

using namespace std;

class Point {

protected:

int x;

int y;

public:

Point ();

Point (const Point &p);

Point (int a, int b);

friend Point& move(Point&, int, int);

friend Point& move(Point&, Point &);

friend Point& assign(Point&, const int, const int);

friend Point& assign(Point&, const Point &);

friend void print(const Point&);

int getX() const;

int getY() const;

};

// point.cpp

#include "Point.h"

#include "Rect.h"

#include <iostream>

Point::Point(){

x = 0;

y = 0;

}

Point::Point (const Point &p){

x = p.x;

y = p.y;

}

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

x = a;

y = b;

}

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

p.x += dx;

p.y += dy;

return p;

}

Point& move(Point& p, Point &p1){

p.x += p1.x;

p.y += p1.y;

return p;

}

Point& assign(Point& p, const int x1, const int y1){

p.x = x1;

p.y = y1;

return p;

}

Point& assign(Point& p, const Point &p1){

p.x = p1.x;

p.y = p1.y;

return p;

}

void print(const Point& p){

cout << "(" << p.x << ", " << p.y << "),";

}

int Point::getX() const{

return x;

}

int Point::getY() const{

return y;

}

// Rect.h

using namespace std;

#include "Point.h"

class Rect {

private:

Point p;

int dx;

int dy;

public:

Rect ();

Rect (const Point& p, int dx1, int dy1);

Rect (const Rect& r);

Rect operator - () const;

Rect& operator = (const Rect& r);

Rect operator + (const Rect& r) const;

friend ostream & operator<< (ostream& s, const Rect r);

};

// Rect.cpp

#include "Point.h"

#include <iostream>

#include "Rect.h"

Rect::Rect():p(){

dx=0;

dy=0;

}

Rect::Rect(const Point& p1, int dx1, int dy1):p(p1){

dx=dx1;

dy=dy1;

}

Rect::Rect(const Rect& r):p(r.p){

dx=r.dx;

dy=r.dy;

}

Rect& Rect::operator = (const Rect& r){

assign(p, r.p);

dx = r.dx;

dy = r.dy;

return *this;

}

Rect Rect::operator - () const{

Point tmpPoint(-p.getX(), -p.getY());

Rect tmpRect(tmpPoint, dx, dy);

return tmpRect;

}

Rect Rect::operator + (const Rect& r) const{

Point tmpPoint(p.getX() + r.p.getX(), p.getY() + r.p.getY());

Rect tmpRect(tmpPoint, dx + r.dx, dy + r.dy);

return tmpRect;

}

ostream & operator << (ostream& s, const Rect r){

s << "Location: ";

print(r.p);

s << "\nWidth: " << r.dx << endl;

s << "Height: " << r.dy << endl;

return s;

}

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