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

Вариант 8

Класс

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

Интерфейс

Point

x, y

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

Rect

p1, p2 (типа Point)

Конструкторы, friend-функции move, square,

операции =, ==, <<

// Main

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

cout << b << endl;

cout << -b << endl;

c = a + -b;

cout << c << endl;

Rect r1(a, b);

Rect r2(c, d), r3;

cout << endl << r1 << endl << r2;

cout << endl << square(r2);

r3 = r1;

cout << endl << r3;

system("pause");

return 0;

}

// Point.h

#pragma once

#include <iostream>

using namespace std;

class Point {

protected:

int x;

int y;

public:

Point ();

Point (const Point &p);

Point (int a, int b);

Point operator - () const;

Point operator + (const Point&) const;

Point& operator = (const Point&);

friend ostream& operator << (ostream&, 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 Point::operator - () const{

return Point (-x, -y);

}

Point Point::operator + (const Point& p) const{

Point tmpPoint(x + p.x, y + p.y);

return tmpPoint;

}

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

x = p.x;

y = p.y;

return *this;

}

ostream& operator << (ostream& s, const Point& p){

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

return s;

}

int Point::getX() const{

return x;

}

int Point::getY() const{

return y;

}

//Rect.h

#pragma once

#include <iostream>

using namespace std;

#include "Point.h"

class Rect {

private:

Point p1;

Point p2;

public:

Rect ();

Rect (const Point&, const Point&);

Rect (const Rect&);

Rect& operator = (const Rect&);

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

friend int square (const Rect&);

bool operator == (const Rect&) const;

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

};

//Rect.cpp

#include "Point.h"

#include <iostream>

#include "Rect.h"

Rect::Rect():p1(), p2(){

}

Rect::Rect(const Point& point1, const Point& point2):p1(point1), p2(point2){

}

Rect::Rect(const Rect& r):p1(r.p1), p2(r.p2){

}

Rect& move(Rect& r, const Point& p){

r.p1 = p;

return r;

}

int square(const Rect& r){

return (abs(r.p1.getX() - r.p2.getX()) * abs(r.p1.getX() - r.p2.getX()));

}

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

p1 = r.p1;

p2 = r.p2;

return *this;

}

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

return (square(*this) == square(r));

}

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

s << "Rect: [" << r.p1 << ", " << r.p2 << "]";

return s;

}

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