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

Вариант 10

Класс

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

Интерфейс

Point

x, y

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

Rect

dx, dy

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

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

//MAIN

#include "Point.h"

#include "Rect.h"

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

{

Point a(4, 6);

Point b,q;

Point c(a);

b = a;

cout << a << b << c << a + b;

q = -a;

cout<< q;

Rect r1(a, 5, 6);

Rect r2(r1), r3;

cout << endl << r1;

cout << r1;

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

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

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

cout << Move(r1,15,10);

system("pause");

return 0;

}

//Point.h

#pragma once

#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 operator - ();

Point operator = (const Point &t);

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 +(const Point &t){

Point sum;

sum.x = x+t.x;

sum.y = y+t.y;

return sum;

}

Point Point::operator -(){

Point result;

result.x = -x;

result.y = -y;

return result;

}

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

x = q.x;

y = q.y;

return *this;

}

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

{

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

return s;

}

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

friend int square(const Rect& r);

Rect operator=(const Rect& a);

bool operator==(const Rect& a);

bool operator<(const Rect& a);

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

friend Rect Move(Rect& r, int dx1, int dy1);

};

//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 Move(Rect& r, int x, int y){

r.x +=x;

r.y += y;

return r;

}

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 square(const Rect& r){

return r.dx*r.dy;

}

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

dx = r.dx;

dy = r.dy;

return *this;

}

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

int sq = r.dx*r.dy;

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

return false;

}

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

int sq = r.dx*r.dy;

if (square(*this) < sq) return true;

return false;

}

Вариант 11

Класс

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

Интерфейс

Point

x, y

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

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

Rect

dx, dy

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

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

// MAIN

#include "Point.h"

#include "Rect.h"

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

{

Point a(4, 6);

Point b,q;

Point c(a);

b = a;

if(a == b)print(a);

q = a;

print(q);

Rect r1(a, 5, 6);

Rect r2(r1), r3;

cout << endl << r1;

cout << r1;

r2.Move(10,15)

cout << r2 << endl << endl;

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

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

cout << r1.square();

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

Point(int a, int b);

Point operator = (const Point &t);

bool operator == (const Point &t);

Point Move(const Point&);

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

friend void print(const 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;

}

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

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

return false;

}

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

x = q.x;

y = q.y;

return *this;

}

void print(const Point& p){

cout << "\nx=" << p.x << ";" << endl;

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

}

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

p.x+=t.x;

p.y+=t.y;

return p;

}

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

x += p.x;

y += p.y;

return *this;

}

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

bool operator==(const Rect& a);

bool operator<(const Rect& a);

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

Rect Move(int dx1, int dy1);

Rect Move(const Point&);

};

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

}

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

dx = r.dx;

dy = r.dy;

return *this;

}

Rect Rect::Move(int xd, int yd){

x +=xd;

y += yd;

return *this;

}

Rect Rect::Move(const Point &p){

Point::Move(p);

return *this;

}

int Rect::square(){

return dx*dy;

}

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

int sq = r.dx*r.dy;

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

return false;

}

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

int sq = r.dx*r.dy;

if (square() < sq) return true;

return false;

}

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