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

Вариант 27, 29.

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

Класс

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

Интерфейс

Point

x, y

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

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

Rect

dx, dy

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

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

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

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

b=a;

a.print();

b.print();

cout << (a == b);

Rect r1(a, 5, 6);

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

cout << endl << r1;

cout << r1.move(c);

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

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

r3 = r2;

cout << r3;

system("pause");

return 0;

}

//Point.h

#pragma once

#include <iostream>

using namespace std;

class Point {

private:

int x;

int y;

public:

Point();

Point (const Point &p);

Point (int a, int b);

void print()const;

Point& move(int, int);

Point& move(const Point &);

Point& operator = (const Point &p);

bool operator == (const Point &p);

};

//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(const Point &p){

x += p.x;

y += p.y;

return *this;

}

void Point::print()const{

cout << "\n(" << x << ", " << y << ")" << endl;

}

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

}

//Rect.h

#pragma once

#include <iostream>

using namespace std;

#include "Point.h"

class Rect : Point {

public:

private:

int dx;

int dy;

public:

Rect ();

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

Rect (const Rect& r);

Rect& move (const Point& p);

int square ()const;

Rect& operator = (const Rect& r);

bool operator < (Rect& r);

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:";

r.print();

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

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

return s;

}

Rect::Rect():Point(){

dx=0;

dy=0;

}

Rect::Rect(const 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;

}

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

Point::move(p);

return *this;

}

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

(Point)*this = r;

dx = r.dx;

dy = r.dy;

return *this;

}

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

return (square() < r.square());

}

int Rect::square()const{

return dx * dy;

}

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