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