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

Вариант 24

Класс

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

Интерфейс

Point

x, y

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

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

Rect

p1, p2 (типа Point)

Конструкторы, функция square,

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

// Main.cpp

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

cout << (a == b);

c++;

assign(b, c);

cout << b;

Rect r1(a, b);

Rect r2(b, d), r3;

cout << endl << r1;

cout << r1.square() << endl;

cout << (r1 == r2) << endl;

r3 = r2;

r3++;

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

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

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

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

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

Point& operator = (const Point &p);

Point& operator ++ (); //префиксный вариант

Point& operator ++ (int); //постфиксный вариант

bool operator == (const Point &p);

friend ostream& operator << (ostream&, const Point& p);

int getX();

int getY();

};

//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, Point &p1){

p.x = p1.x;

p.y = p1.y;

return p;

}

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

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

return s;

}

Point& Point::operator ++ (){

x++;

y++;

return *this;

}

Point& Point::operator ++ (int){

Point tmp(*this);

operator++();

return tmp;

}

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

x = p.x;

y = p.y;

return *this;

}

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

return (x == p.x) && (y == p.y);

}

int Point::getX(){

return x;

}

int Point::getY(){

return y;

}

//Rect.h

#pragma once

#include <iostream>

using namespace std;

#include "Point.h"

class Rect {

private:

Point p1;

Point p2;

public:

1Rect ();

Rect (const Point&, const Point&);

Rect (const Rect&);

int square () const;

Rect& operator = (const Rect& r);

Rect& operator++(); // префикс

Rect& operator++(int); // постфикс

bool operator == (const Rect&);

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

};

//Rect.cpp

#include "Point.h"

#include <iostream>

#include "Rect.h"

#include <math.h>

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

}

Rect::Rect(const Point& _p1, const Point& _p2):p1(_p1), p2(_p2){

}

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

}

int Rect::square()const{

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

}

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

p1 = r.p1;

p2 = r.p2;

return *this;

}

Rect& Rect::operator ++ (){

p1++;

p2++;

return *this;

}

Rect& Rect::operator ++ (int){

Rect tmp(*this);

operator++();

return tmp;

}

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

return (square() == r.square());

}

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

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

return s;

}

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