You are on page 1of 6

Завдання А)

Задача: Зробіть клас Point3D дружнім класу Vector3D і реалізуйте метод


moveByVector() в класі Point3D.
Готовий код:
#include <iostream>

using namespace std;

class Vector3D {
private:
double m_x, m_y, m_z;

public:
Vector3D(double x = 0.0, double y = 0.0, double z = 0.0)
: m_x(x), m_y(y), m_z(z) {}

void print() {
cout << "Vector(" << m_x << " , " << m_y << " , " << m_z << ")\
n";
}

friend class Point3D; };

class Point3D {
private:
double m_x, m_y, m_z;

public:
Point3D(double x = 0.0, double y = 0.0, double z = 0.0)
: m_x(x), m_y(y), m_z(z) {}

void print() {
cout << "Point(" << m_x << " , " << m_y << " , " << m_z << ")\n";
}

void moveByVector(const Vector3D& v) {


m_x += v.m_x;
m_y += v.m_y;
m_z += v.m_z;
}
};

int main() {
Point3D p(3.0, 4.0, 5.0);
Vector3D v(3.0, 3.0, -2.0);

p.print();
p.moveByVector(v);
p.print();

return 0;
}

Результат:

Завдання Б)
Задача: Замість того, щоб клас Point3D був дружнім класу Vector3D,
зробіть метод Point3D::moveByVector() дружнім класу Vector3D.
Готовий код:
#include <iostream>

using namespace std;

class Vector3D;

class Point3D {
private:
double m_x, m_y, m_z;
public:
Point3D(double x = 0.0, double y = 0.0, double z = 0.0)
: m_x(x), m_y(y), m_z(z) {}

void print() {
cout << "Point(" << m_x << " , " << m_y << " , " << m_z << ")\n";
}

void moveByVector(const Vector3D& v);


};

class Vector3D {
private:
double m_x, m_y, m_z;

public:
Vector3D(double x = 0.0, double y = 0.0, double z = 0.0)
: m_x(x), m_y(y), m_z(z) {}

void print() {
cout << "Vector(" << m_x << " , " << m_y << " , " << m_z << ")\
n";
}

friend void Point3D::moveByVector(const Vector3D& v);


};

void Point3D::moveByVector(const Vector3D& v) {


m_x += v.m_x;
m_y += v.m_y;
m_z += v.m_z;
}

int main() {
Point3D p(3.0, 4.0, 5.0);
Vector3D v(3.0, 3.0, -2.0);
p.print();
p.moveByVector(v);
p.print();

return 0;
}

Результат:

Завдання С)
Задача: Переробіть свою відповідь із завдання b, використовуючи 5
окремих файлів: Point3D.h, Point3D.cpp, Vector3D.h, Vector3D.cpp і
main.cpp.
Готовий код:
Main.cpp
#include "Point3D.h"
#include "Vector3D.h"

int main()
{
Point3D p(3.0, 4.0, 5.0);
Vector3D v(3.0, 3.0, -2.0);

p.print();
p.moveByVector(v);
p.print();

return 0;
}
Point3D.cpp
#include <iostream>
#include "Vector3D.h"
#include "Point3D.h"

void Point3D::moveByVector(const Vector3D& v) {


m_x += v.m_x;
m_y += v.m_y;
m_z += v.m_z;
}

void Point3D::print() {
std::cout << "Point(" << m_x << " , " << m_y << " , " << m_z << ")\n";
}
Point3D.h
#ifndef POINT3D_H
#define POINT3D_H

class Vector3D;

class Point3D {
private:
double m_x;
double m_y;
double m_z;

public:
Point3D(double x = 0.0, double y = 0.0, double z = 0.0) : m_x(x), m_y(y),
m_z(z) {}

void print();
void moveByVector(const Vector3D& v);
};

#endif

Vector3D.cpp
#include <iostream>
#include "Vector3D.h"

void Vector3D::print() {
std::cout << "Vector(" << m_x << " , " << m_y << " , " << m_z << ")\n";
}
Vector3D.h
#ifndef VECTOR3D_H
#define VECTOR3D_H

#include "Point3D.h"

class Vector3D {
private:
double m_x;
double m_y;
double m_z;

public:
Vector3D(double x = 0.0, double y = 0.0, double z = 0.0) : m_x(x), m_y(y),
m_z(z) {}

void print();
friend void Point3D::moveByVector(const Vector3D& v);
};

#endif

Результат:

You might also like