0

Как Перегрузить оператор извлечения >> и вставки << для объектов данного класса.

  #include <iostream>
#include <math.h>

using namespace std;

class Vector
{
public:
    double X;
    double Y;
    double Z;

    Vector()
    {
        this->X = 0;
        this->Y = 0;
        this->Z = 0;
    }

    Vector(double x, double y, double z)
    {
        this->X = x;
        this->Y = y;
        this->Z = z;
    }

    double GetLength()
    {
        return sqrt(X * X + Y * Y + Z * Z);
    }

    void Print()
    {
        cout << endl;
        cout << "X=" << X;
        cout << endl;
        cout << "Y=" << Y;
        cout << endl;
        cout << "Z=" << Z;
        cout << endl;
    }

    Vector operator +(Vector r)
    {
        return Vector(this->X + r.X, this->Y + r.Y, this->Z + r.Z);
    }

    Vector operator -(Vector r)
    {
        return Vector(this->X - r.X, this->Y - r.Y, this->Z - r.Z);
    }

    double operator *(Vector r)
    {
        return (this->X * r.X + this->Y * r.Y + this->Z * r.Z);
    }
};
int main()
{
    int n1;
    cout << "Vvedite kolishstvo elementov massiva: ";
    cin >> n1;
    Vector* vc=new Vector[n1]();//создаём динамический массив 
    for (size_t i = 0; i < n1; i++)//заполняем массив
    {
        double x1, y1, z1;
        cout << "Vvedite  X[" << i << "]" << " Y[" << i << "]" << " Z[" << i << "]" << endl;
        cin >> x1 >> y1 >> z1;
        vc[i].X = x1;
        vc[i].Y = y1;
        vc[i].Z = z1;
    }
    Vector sum;
    Vector raz=vc[0];
    for (size_t i = 0; i < n1; i++)
    {
        sum =sum+ vc[i];
        if (i == 0)
        {
            continue;
        }
        raz = raz - vc[i];
    }
    cout << "Summa" << endl;
    cout << sum.X << " " << sum.Y << " " << sum.Z << endl;
    cout << "Raznost" << endl;
    cout << raz.X << " " << raz.Y << " " << raz.Z << endl;
    delete[] vc;

    system("pause");
}

1 ответ 1

0

Оператор извлечения >> и вставки << объявляются как свободные функции, обычно как friend функции для класса.

#include <iostream>
#include <math.h>
#include <ostream>
#include <istream>

using namespace std;

class Vector {
public:
  double X;
  double Y;
  double Z;

  Vector() {
    this->X = 0;
    this->Y = 0;
    this->Z = 0;
  }

  Vector(double x, double y, double z) {
    this->X = x;
    this->Y = y;
    this->Z = z;
  }

  double GetLength() { return sqrt(X * X + Y * Y + Z * Z); }

  void Print() {
    cout << endl;
    cout << "X=" << X;
    cout << endl;
    cout << "Y=" << Y;
    cout << endl;
    cout << "Z=" << Z;
    cout << endl;
  }

  Vector operator+(Vector r) {
    return Vector(this->X + r.X, this->Y + r.Y, this->Z + r.Z);
  }

  Vector operator-(Vector r) {
    return Vector(this->X - r.X, this->Y - r.Y, this->Z - r.Z);
  }
  double operator*(Vector r) {
    return (this->X * r.X + this->Y * r.Y + this->Z * r.Z);
  }

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

  friend istream& operator>>(istream&, Vector&);
};

ostream& operator<<(ostream& os, const Vector& v) {
  os << v.X << " " << v.Y << " " << v.Z;
  return os;
}

istream& operator>>(istream& is, Vector& v) {
  is >> v.X >> v.Y >> v.Z;
  return is;
}

int main() {
  int n1;
  cout << "Vvedite kolishstvo elementov massiva: ";
  cin >> n1;
  Vector* vc = new Vector[n1]();  //создаём динамический массив

  for (size_t i = 0; i < n1; i++)  //заполняем массив
  {
    cout << "Vvedite  X[" << i << "]"
      << " Y[" << i << "]"
      << " Z[" << i << "]" << endl;
    cin >> vc[i];
  }

  Vector sum;
  Vector raz = vc[0];
  for (size_t i = 0; i < n1; i++) {
    sum = sum + vc[i];
    if (i == 0) {
      continue;
    }
    raz = raz - vc[i];
  }

  cout << "Summa: " << sum << endl;

  cout << "Raznost: " << raz << endl;

  delete[] vc;

  system("pause");
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.