
- •Задания задание 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
Вариант 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;
}