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

Вариант 9

Задание

Класс

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

Интерфейс

Point

x, y

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

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

Rect

dx, dy

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

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

//MAIN

#include "Point.h"

#include "Rect.h"

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

{

Point a(4, 6);

Point b;

Point c(a);

b = a;

Point q = Move(b, c);

cout << a<<b<<c<<q;

q = Assign(a);

if (q == a) cout<<"true"<< q;

Rect r1(a, 5, 6);

Rect r2(r1), r3;

cout << endl << r1;

cout << r1;

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

if(r1 == r2) cout <<"true"<< endl << endl;

else cout << "false" << endl << endl;

r2++;

cout << r2;

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

Point(const int , cosnt int);

Point operator ++ ();

Point operator = (const Point &t);

bool operator==(const Point &t);

friend Point Move(const Point &p, const Point& howFar);

friend Point Assign(const Point &p);

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

};

//Point.cpp

#include "Point.h"

#include "Rect.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 ++(){

x = x++;

y = y++;

return *this;

}

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

x = t.x;

y = t.y;

return *this;

}

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

if (x == t.x && y == t.y) return true;

return false;

}

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

{

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

return s;

}

Point Move(const Point& p, const Point& t){

Point result(p.x+t.x,p.y+t.y);

return result;

}

Point Assign(const Point& p){

return Point(p);

}

//Rect.h

#pragma once

#include <iostream>

using namespace std;

#include "Point.h"

class Rect : Point {

private:

int dx;

int dy;

public:

Rect();

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

Rect(const Rect& r);

int square();

Rect operator=(const Rect& a);

Rect operator ++ ();

bool operator==(const Rect& a);

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

};

//Rect.cpp

#include "Point.h"

#include <iostream>

#include "Rect.h"

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

s<< "Location:";

s<<" x ="<< r.x << " y = "<<r.y << endl;

s<< "Width: " << r.dx << endl;

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

return s;

}

Rect::Rect():Point(){

dx = 0;

dy = 0;

}

Rect::Rect(Point p1, int dx1, int dy1):Point(p1){

dx = dx1;

dy = dy1;

}

Rect::Rect(const Rect& r):Point(r){

dx = r.dx;

dy = r.dy;

}

int Rect::square(){

return dx*dy;

}

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

Point::Assign((Point)r);

dx = r.dx;

dy = r.dy;

return *this;

}

Rect Rect::operator++(){

dx ++;

dy ++;

return *this;

}

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

int sq = r.dx*r.dy;

if (square() == sq) return true;

return false;

}

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