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

Вариант 25

Класс

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

Интерфейс

Point

x, y

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

Rect

dx, dy

Конструкторы, функция 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;

b.print();

b.assign(c);

b.print();

b.move(1, 4);

Rect r1(a, 5, 7);

Rect r2(b, 6, 8), r3;

cout << endl << r1;

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

r3 = r1;

r3 += r2;

if (r1 < r3) cout << "true";

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

Point& move(int, int);

Point& move(Point &);

Point& assign(const int, const int);

Point& assign(Point &);

void print() 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::move(int dx, int dy){

x += dx;

y += dy;

return *this;

}

Point& Point::move(Point &p1){

x += p1.x;

y += p1.y;

return *this;

}

Point& Point::assign(const int x1, const int y1){

x = x1;

y = y1;

return *this;

}

Point& Point::assign(Point &p1){

x = p1.x;

y = p1.y;

return *this;

}

void Point::print() const{

cout << "(" << x << ", " << y << "),";

}

//Rect.h

#include <iostream>

using namespace std;

#include "Point.h"

class Rect : Point{

private:

int dx;

int dy;

public:

Rect ();

Rect (const Point&, const int, const int);

Rect (const Rect&);

int square () const;

Rect& operator = (const Rect& r);

void operator += (const Rect& r);

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

};

//Rect.cpp

#include "Point.h"

#include <iostream>

#include "Rect.h"

Rect::Rect():Point(){

dx = 0;

dy = 0;

}

Rect::Rect(const Point& p, const int dx1, const int dy1):Point(p){

dx = dx1;

dy = dy1;

}

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

dx = r.dx;

dy = r.dy;

}

int Rect::square() const{

return dx * dy;

}

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

Point::assign((Point)r);

dx = r.dx;

dy = r.dy;

return *this;

}

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

assign(r.x, r.y);

dx += r.dx;

dy += r.dy;

}

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

s << "Location: ";

r.print();

s << "\nWidth: " << r.dx << endl;

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

return s;

}

Вариант 26, 28

Написать тексты h-файлов и cpp-файлов для базового класса Point и производного класса Rect (прямоугольник). Описание классов:

Класс

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

Интерфейс

Point

x, y

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

Rect

dx, dy

Конструкторы, функция square, операции =,++, ==, <<

Привести примеры создания и использования объектов классов Point и Rect. Нарисовать диаграммы классов.

// Main.cpp

#include "Rect.h"

void main() {

Point a, b(1, 2), c(b);

assign(a, b);

c = Point(3, 4);

move(b, 3, 3);

b++;

cout << "a" << a << ", b" << b << ", c" << c << endl;

if (!(a == b) && a == Point(1, 2))

cout << "a != b && a == (1, 2)" << endl;

Rect rect1, rect2(a, 1, 6), rect3(rect1);

rect1 = Rect(c, 2, 3);

rect3++;

cout << "rect1 - " << rect1 << "\nrect2 - " << rect2 << "\nrect3 - " << rect3 << endl;

cout << "square rect1 = " << rect1.square() << endl;

if (rect1 == rect2)

cout << "rect1 == rect2" << endl;

system("pause");

}

//Point.h

#include <iostream>

using namespace std;

class Point {

private:

int x;

int y;

public:

Point();

Point(int, int);

Point(const Point&);

Point& operator =(const Point&);

Point& operator ++();

bool operator ==(const Point&) const;

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

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

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

};

//Point.cpp

#include "Point.h"

Point::Point() {

x = 0;

y = 0;

}

Point::Point(int a, int b) {

x = a;

y = b;

}

Point::Point(const Point& p) {

assign(*this, p);

}

Point& move(Point& p, int dx, int dy) {

p.x += dx;

p.y += dy;

return p;

}

Point& assign(Point& a, const Point& b) {

a.x = b.x;

a.y = b.y;

return a;

}

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

return assign(*this, p);

}

Point& Point::operator ++() {

x++;

y++;

return *this;

}

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

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

}

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

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

return s;

}

//Rect.h

#include "Point.h"

class Rect : Point {

private:

int dx;

int dy;

public:

Rect();

Rect(Point, int, int);

Rect(const Rect&);

int square() const;

Rect& operator =(const Rect&);

Rect& operator ++();

bool operator ==(const Rect&) const;

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

};

//Rect.cpp

#include "Rect.h"

Rect::Rect():Point() {

dx = 0;

dy = 0;

}

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

dx = dx1;

dy = dy1;

}

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

dx = r.dx;

dy = r.dy;

}

int Rect::square() const {

return dx * dy;

}

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

assign(*this, r);

dx = r.dx;

dy = r.dy;

return *this;

}

Rect& Rect::operator ++() {

dx++;

dy++;

return *this;

}

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

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

}

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

s << "Height: " << r.dy << ", Width: " << r.dx << ", Point: " << (Point)r;

return s;

}

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