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

Вариант 22

Разработать классы Point и Array, позволяющие использовать их в следующей программе:

Point p1(10,20), p2(40,25), p3(p1); cout<<p2.move(40,20)<<(p1=p2); p3*=2;

Array a1(10), a2(10); a1[0]=a1[1]=p2; a2=a1; cout<<p2;

Main.cpp

#include <iostream>

#include "Point.h"

#include "Array.h"

using namespace std;

int main(void)

{

Point p1(10,20), p2(40,25), p3(p1);

cout<< p2.move(40,20) << (p1=p2);

p3*=2;

cout << p3;

Array a1(10), a2(10);

a1[0]=a1[1]=p2;

a2=a1;

cout<<p2;

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(const int, const int);

Point& operator = (const Point&);

Point& operator *= (const int);

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

};

//Point.cpp

#include <iostream>

#include "Point.h"

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

x = p.x;

y = p.y;

return *this;

}

Point& Point::operator *= (const int z){

x *= z;

y *= z;

return *this;

}

ostream& operator << (ostream& s, const Point& p){

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

return s;

}

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

x += dx;

y += dy;

return *this;

}

// Array.h

#pragma once

#include "Point.h"

#define DEFAULT_SIZE 10

class Array {

private:

Point* pa;

int size;

public:

Array();

Array(int);

~Array();

Point& operator [] (const int);

Array& operator = (const Array&);

};

// Array.cpp

#include "Array.h"

Array::Array(const int s){

if (s < 0) { throw -1; }

size = s;

pa = new Point[s];

}

Array::Array(){

size = DEFAULT_SIZE;

pa = new Point[size];

}

Array::~Array(){

delete []pa;

}

Point& Array::operator [] (const int index){

if(index < 0 || index >= size){

throw -1;

} else {

return pa[index];

}

}

Array& Array::operator = (const Array& arr){

size = arr.size;

delete[] pa;

pa = new Point[size];

for (int i = 0; i < size; i++){

pa[i] = arr.pa[i];

}

return *this;

}

Вариант 23

Класс

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

Интерфейс

Point

x, y

Конструкторы, функции(friend) move, assign

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

Rect

p (типа Point), 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);

b = a;

cout << b;

cout << (a == b);

c++;

assign(b, c);

cout << b;

Rect r1(a, 5, 6);

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

cout << endl << r1;

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

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

r3 = r2;

r3++;

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

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

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

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

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

Point& operator = (const Point &p);

Point& operator ++ (); //префиксный вариант

Point& operator ++ (int); //постфиксный вариант

bool operator == (const Point &p);

friend ostream& operator << (ostream&, 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& move(Point& p, int dx, int dy){

p.x += dx;

p.y += dy;

return p;

}

Point& move(Point& p, Point &p1){

p.x += p1.x;

p.y += p1.y;

return p;

}

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

p.x = x1;

p.y = y1;

return p;

}

Point& assign(Point& p, Point &p1){

p.x = p1.x;

p.y = p1.y;

return p;

}

ostream& operator << (ostream& s, const Point& p){

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

return s;

}

Point& Point::operator ++ (){

x++;

y++;

return *this;

}

Point& Point::operator ++ (int){

Point tmp(*this);

operator++();

return tmp;

}

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 {

private:

Point p;

int dx;

int dy;

public:

Rect ();

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

Rect (const Rect& r);

int square ()const;

Rect& operator = (const Rect& r);

Rect& operator++(); // префикс

Rect& operator++(int); // постфикс

bool operator == (Rect& r);

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

};

//Rect.cpp

#include "Point.h"

#include <iostream>

#include "Rect.h"

Rect::Rect():p(){

dx=0;

dy=0;

}

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

dx=dx1;

dy=dy1;

}

Rect::Rect(const Rect& r):p(r.p){

dx=r.dx;

dy=r.dy;

}

int Rect::square()const{

return dx * dy;

}

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

p = r.p;

dx = r.dx;

dy = r.dy;

return *this;

}

Rect& Rect::operator ++ (){

p++;

dx++;

dy++;

return *this;

}

Rect& Rect::operator ++ (int){

Rect tmp(*this);

operator++();

return tmp;

}

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

return (p == r.p) && (dx == dy);

}

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

s << "Location:" << r.p << endl;

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

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

return s;

}

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