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

Вариант 12

Класс

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

Интерфейс

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,q;

Point c(a);

b = a;

if(a == b)cout<< a<<q<<c;

cout<<Assign(q);

q++;

cout << q;

Rect r1(a, 5, 6);

Rect r2(r1), r3;

cout << endl << r1;

cout << r1++;

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

Point operator ++ ();

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;

}

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;

}

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

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

return result;

}

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

{

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

return s;

}

Point Point::operator ++(){

x = x++;

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

Rect operator++();

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;

}

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

dx = r.dx;

dy = r.dy;

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;

}

Rect Rect::operator++(){

dx++;

dy++;

return *this;

}

Вариант 13

Класс

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

Интерфейс

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

q = -c;

cout<< a<<q<<c;

Rect r1(a, 5, 6);

Rect r2(r1), r3;

r3 = r2;

if (r1 == r2)

cout << endl << r1;

cout << Move(r1,24,25);

cout << square(r1);

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

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

}

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

x += q.x;

y += q.y;

return *this;

}

Point Point::operator -(){

return Point (-x, -y);

}

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

friend Rect Move(Rect& a,int dx,int dy);

Rect operator=(const Rect& a);

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;

}

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

dx = r.dx;

dy = r.dy;

return *this;

}

int square(const Rect &r){

return r.dx*r.dy;

}

Rect Move(Rect& r, int x, int y){

r.x += x;

r.y += y;

return r;

}

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

int sq = r.dx*r.dy;

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

return false;

}

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