
- •Задания задание 1
- •Задание 2
- •Задание 3
- •3. Разработать классы Point и Array, позволяющие использовать их в следующей программе:
- •Задание 4
- •Задание 5
- •Задание 6
- •6. Разработать классы Point и Array, позволяющие использовать их в следующей программе:
- •Задание 7
- •Задание 8
- •Задание 9
- •Задание 10
- •Задание 11
- •Задание 12
- •Задание 13
- •17. Разработать классы Point и Array, позволяющие использовать их в следующей программе:
- •Задание 19
- •19. Разработать классы Point и Array, позволяющие использовать их в следующей программе:
- •Задание 20
- •Задание 21
- •Задание 22
- •22. Разработать классы Point и Array, позволяющие использовать их в следующей программе:
- •Задание 23
- •Задание 24
- •Задание 25
- •Задание 26
- •Задание 27
- •Задание 28
- •Задание 29
- •Задание 30
- •Решения Вариант 1
- •Вариант 2
- •Вариант 3
- •Вариант 4
- •Вариант 5
- •Вариант 7
- •Вариант 8
- •Вариант 9
- •Вариант 10
- •Вариант 11
- •Вариант 12
- •Вариант 13
- •Вариант 14
- •Вариант 15
- •Вариант 16
- •Вариант 6, 17, 19
- •Вариант 18
- •Вариант 20
- •Вариант 21
- •Вариант 22
- •Вариант 23
- •Вариант 24
- •Вариант 25
- •Вариант 26, 28
- •Вариант 27, 29.
- •Вариант 30
Вариант 8
Класс |
Элементы данных |
Интерфейс |
Point |
x, y |
Конструкторы, операции –(унар),+, =, << |
Rect |
p1, p2 (типа Point) |
Конструкторы, friend-функции move, square, операции =, ==, << |
// Main
#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;
cout << b << endl;
cout << -b << endl;
c = a + -b;
cout << c << endl;
Rect r1(a, b);
Rect r2(c, d), r3;
cout << endl << r1 << endl << r2;
cout << endl << square(r2);
r3 = r1;
cout << endl << 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 operator - () const;
Point operator + (const Point&) const;
Point& operator = (const Point&);
friend ostream& operator << (ostream&, const Point&);
int getX() const;
int getY() 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::operator - () const{
return Point (-x, -y);
}
Point Point::operator + (const Point& p) const{
Point tmpPoint(x + p.x, y + p.y);
return tmpPoint;
}
Point& Point::operator = (const Point& p){
x = p.x;
y = p.y;
return *this;
}
ostream& operator << (ostream& s, const Point& p){
s << "(" << p.x << ", " << p.y << "),";
return s;
}
int Point::getX() const{
return x;
}
int Point::getY() const{
return y;
}
//Rect.h
#pragma once
#include <iostream>
using namespace std;
#include "Point.h"
class Rect {
private:
Point p1;
Point p2;
public:
Rect ();
Rect (const Point&, const Point&);
Rect (const Rect&);
Rect& operator = (const Rect&);
friend Rect& move (Rect&, const Point&);
friend int square (const Rect&);
bool operator == (const Rect&) const;
friend ostream & operator<< (ostream&, const Rect);
};
//Rect.cpp
#include "Point.h"
#include <iostream>
#include "Rect.h"
Rect::Rect():p1(), p2(){
}
Rect::Rect(const Point& point1, const Point& point2):p1(point1), p2(point2){
}
Rect::Rect(const Rect& r):p1(r.p1), p2(r.p2){
}
Rect& move(Rect& r, const Point& p){
r.p1 = p;
return r;
}
int square(const Rect& r){
return (abs(r.p1.getX() - r.p2.getX()) * abs(r.p1.getX() - r.p2.getX()));
}
Rect& Rect::operator = (const Rect& r){
p1 = r.p1;
p2 = r.p2;
return *this;
}
bool Rect::operator == (const Rect& r) const{
return (square(*this) == square(r));
}
ostream & operator << (ostream& s, const Rect r){
s << "Rect: [" << r.p1 << ", " << r.p2 << "]";
return s;
}